Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
406 views
in Technique[技术] by (71.8m points)

c# - Problem with Circular dependency in ASP.NET Core

I have to create services for my project, that implement other services, therefore I created many Interfaces and for some services I inject the interface to use the methods, but when I tried to compile my project I become an error, this is driving me nuts, can someone please tell me what am I doing wrong?

the interface:

   public interface IGoogleExtensions
   {
      dynamic GetEntetiesTypeorWorth(WebhookRequest request, string entetie, bool wertHohlen);
      bool HasDisplay(WebhookRequest request);
      string StringBuilder(int position, string text, string texttoinsert);

   }

The Service

  public class GoogleExtensionsService : IGoogleExtensions

  {
      private readonly IGoogleVitalwertResponseHelper _googleVitalwertResponseHelper;

  public GoogleExtensionsService(IGoogleVitalwertResponseHelper googleVitalwertResponseHelper)
  {
     _googleVitalwertResponseHelper = googleVitalwertResponseHelper;
  }

  public dynamic GetEntetiesTypeorWorth(WebhookRequest request, string entetie, bool wertHohlen)
  {
     var entetieType = request.QueryResult.Parameters.Fields;
     var type = TextResponses.FehlerMeldung2;
    [...]
        type = TextResponses.Alle;
     }
     return type;
  }

  public bool HasDisplay(WebhookRequest request)
  {
     string capability = request.OriginalDetectIntentRequest.Payload.Fields[ProtobufFields.Surface].StructValue.Fields[ProtobufFields.Capabilities].ToString();
     if (capability.Contains(ProtobufFields.Screen_output))
        return true;
     return false;
  }

  public string StringBuilder(int position, string text, string texttoinsert)
  {
     StringBuilder sb = new StringBuilder(text);
     sb.Insert(position, texttoinsert);
     return sb.ToString();
  }

References in Start.up

     services.AddTransient<IGoogleResponseService, GoogleResponseService>();
     services.AddTransient<IGoogleService, GoogleRequestService>();
     services.AddTransient<IGoogleExtensions, GoogleExtensionsService>();
     services.AddTransient<IGoogleVitalwertResponseHelper, GoogleVitalwertResponseService>();
     services.AddTransient<IGoogleVitalwertRequest, GoogleVitalwertRequest>();
     services.AddTransient<IGoogleMenuePlanRequest, GoogleMenuePlanRequestService>();
     services.AddTransient<IGoogleMenuePlanResponseHelper, GoogleMenuePlanResponseService>();
     services.AddTransient<IGoogleMedikamentRequest, GoogleMedikamentRequestService>();
     services.AddTransient<IGoogleMedikamentResponse, GoogleMedikamentResponseService>();
     services.AddTransient<IGoogleBerichtResponse, GoogleBerichResponseService>();

I′m dealing with the following error:

Host terminated unexpectedly
System.AggregateException: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: XXX.Microservices.Xxx.Api.Contract.Services.IGoogleService Lifetime: Transient ImplementationType: XXX.Microservices.Xxx.Api.Services.GoogleRequestService': A circular dependency was detected for the service of type 'XXX.Microservices.Xxx.Api.Contract.Services.IGoogleExtensions'.
XXX.Microservices.Xxx.Api.Contract.Services.IGoogleService(XXX.Microservices.Xxx.Api.Services.GoogleRequestService) -> XXX.Microservices.Xxx.Api.Contract.Services.IGoogleExtensions(XXX.Microservices.Xxx.Api.Services.GoogleExtensionsService) -> XXX.Microservices.Xxx.Api.Contract.Services.IGoogleVitalwertResponseHelper(XXX.Microservices.Xxx.Api.Services.GoogleVitalwertResponseService) -> XXX.Microservices.Xxx.Api.Contract.Services.IGoogleExtensions) (Error while validating the service descriptor 'ServiceType: XXX.Microservices.Xxx.Api.Contract.Services.IGoogleExtensions Lifetime: Transient ImplementationType: XXX.Microservices.Xxx.Api.Services.GoogleExtensionsService': A circular dependency was detected for the service of type 'XXX.Microservices.Xxx.Api.Contract.Services.IGoogleExtensions'.
XXX.Microservices.Xxx.Api.Contract.Services.IGoogleExtensions(XXX.Microservices.Xxx.Api.Services.GoogleExtensionsService) -> XXX.Microservices.Xxx.Api.Contract.Services.IGoogleVitalwertResponseHelper(XXX.Microservices.Xxx.Api.Services.GoogleVitalwertResponseService) -> XXX.Microservices.Xxx.Api.Contract.Services.IGoogleExtensions)

Edit

public class GoogleVitalwertResponseService : IGoogleVitalwertResponseHelper
   {
      private readonly IGoogleResponseService _googleResponseService;
      private readonly IGoogleExtensions _googleExtensions;
      private readonly IGoogleVitalwertResponseHelper _googleVitalwertResponseHelper;

      public GoogleVitalwertResponseService(IGoogleResponseService googleResponseService, IGoogleExtensions googleExtensions, IGoogleVitalwertResponseHelper googleVitalwertResponseHelper)
      {
         _googleResponseService = googleResponseService;
         _googleExtensions = googleExtensions;
         _googleVitalwertResponseHelper = googleVitalwertResponseHelper;
      }

      public WebhookResponse VitalwertListResponse(WebhookRequest request, List<VitalwertOutput> vitalList, string vitalwertReference)
      {
         var session = new List<int>();
         var items = new List<Value>();
         int counter = 0;
         ...
      }

      public WebhookResponse VitalwertUebersicht(WebhookRequest request, ICollection<VitalwertOutput> vitalList, ICollection<VitalwertKategorie> Definitionen)
      {
         var items = new List<Value>();
         ....
         return _googleResponseService.BuildListResponse(request, items, "Ich habe " + items.Count + " Vitalwerte gefunden", 3, SessionNames.SessionVitalwertUebersicht, session, null, null, null);
      }

      public string GetAktuellTag(string tag, DayOfWeek dayname)
      {
         DateTime today = DateTime.Parse(tag);
         ...
      }

      public List<KeyValuePair<string, int>> GenerateImageLinks()
      {
         var list = new List<KeyValuePair<string, int>>();
         list.Add(new KeyValuePair<string, int>(ImagesURLS.Stethoscope, 1));
         return list;
      }

      public string GenerateLink(List<KeyValuePair<string, int>> list, int DefinitionId)
     ...
         var sub = vitalart.Substring(1, vitalart.Length - 2);
         return sub;
      }
question from:https://stackoverflow.com/questions/65869936/problem-with-circular-dependency-in-asp-net-core

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your GoogleExtensionsService uses GoogleVitalwertResponseService which uses GoogleExtensionsService, etc, etc. Your concrete implementations cannot have circular dependencies.

It looks like GoogleExtensionsService gets GoogleVitalwertResponseService injected, but doesn't actually use it. Remove that reference from the GoogleExtensionsService and you should be set.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...