2

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;
      }
5
  • 1
    Your GoogleExtensionsService uses GoogleVitalwertResponseService which uses GoogleExtensionsService, etc, etc. Your concrete implementations cannot have circular dependencies. Commented Jan 24, 2021 at 11:39
  • What would be a solution for this? I really have to use methods from other services :/ @JessedeWit Commented Jan 24, 2021 at 11:40
  • 1
    Share the implementation of GoogleVitalwertResponseService here. GoogleExtensionsService and GoogleVitalwertResponseService could probably both get a third class injected with the common functionality. Commented Jan 24, 2021 at 11:44
  • @JessedeWit I already edited the question Commented Jan 24, 2021 at 11:51
  • Is my answer of any help to you Felipe? Commented Jan 25, 2021 at 10:03

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.