Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to resolve service for type 'System.String' while attempting to activate 'MyService'

I'm seeing the following exception in my Service Fabric Stateless ASP.NET Core app.

System.InvalidOperationException: Unable to resolve service for type 'System.String' while attempting to activate 'MyService'.    at Microsoft.Extensions.DependencyInjection.ServiceLookup.Service.PopulateCallSites(ServiceProvider provider, ISet`1 callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound)    at Microsoft.Extensions.DependencyInjection.ServiceLookup.Service.CreateCallSite(ServiceProvider provider, ISet`1 callSiteChain)    at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetResolveCallSite(IService service, ISet`1 callSiteChain)    at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetServiceCallSite(Type serviceType, ISet`1 callSiteChain) 

How can it not be able to resolve System.String? How do I debug this further?

like image 248
spottedmahn Avatar asked Apr 13 '18 20:04

spottedmahn


1 Answers

The default DI framework in Asp.Net Core is limited in what features are available. Assuming a class constructor like

public MyService(string dependencyString) {     //... } 

There is no way for the container to know what value to use for the dependent string to be injected into the service.

In the above case the missing string can be provided using one of the overloads when adding the service to the collection.

//...  services.AddScoped<IMyService>(_ => new MyService("value here"));  //... 

That way when the service is being activated it can be properly returned.


Documentation Reference: Dependency injection in ASP.NET Core -> Constructor injection behavior (v2.1)

like image 118
Nkosi Avatar answered Oct 03 '22 11:10

Nkosi