Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

DI in webapi: InvalidOperationException: Unable to resolve service for type X while attempting to activate Y

$
0
0

Am I doing this right? Looking at the source, it seems that the configure extension function calls ServiceCollection.AddInstance so I'd be able to use the class for DI?

startup.cs:

publicvoid ConfigureServices(IServiceCollection services)

{

services.Configure<SiteSettings>(settings =>{

settings.BlobStorageConnectionString = Configuration.Get("BlobStorageConnectionString");

settings.BlobEndpoint = Configuration.Get("BlobEndpoint");

});

....

Controller:

namespace GearUp.Controllers.Controllers

{

[Route("api/[controller]")]

publicclassUploadImageController : Controller

{ 

privatereadonlyCloudStorageAccount _storageAccount;

public UploadImageController(SiteSettings settings)

{

this._storageAccount = CloudStorageAccount.Parse(settings.BlobStorageConnectionString);

}

.....

when I make an ajax call to the webapi controller I get a 500 error with the message:

InvalidOperationException: Unable to resolve service for type &#39;GearUp.SiteSettings&#39; while attempting to activate &#39;GearUp.Controllers.Controllers.UploadImageController&#39;

Thanks!


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>