c# - How to add IHttpContextAccessor in the Startup class in the DI in ASP.NET Core 1.0? -


in asp.net core rc 1 used following code retrieve value of context (full address of page). recorded value in configuration.

public class startup {         public iconfigurationroot configuration { get; set; }         private ihostingenvironment currentenvironment { get; set; }         private ihttpcontextaccessor httpcontextaccessor { get; set; }         public startup(ihostingenvironment env,                 ihttpcontextaccessor httpcontextaccessor)             {                                 var builder = new configurationbuilder()                     .setbasepath(env.contentrootpath)                     .addjsonfile("appsettings.json", optional: true, reloadonchange: true)                     .addjsonfile($"appsettings.{env.environmentname}.json", optional: true);                  if (env.isdevelopment())                 {                     builder.addusersecrets();                 }                 builder.addenvironmentvariables();                 configuration = builder.build();                 currentenvironment = env;                 httpcontextaccessor = httpcontextaccessor;             }         public void configureservices(iservicecollection services)         {         ...          services.addoptions();         services.configure<webappsettings>(configuration);         services.configure<webappsettings>(settings =>         {             ...             settings.webrootpath = currentenvironment.webrootpath;             settings.domainurl = httpcontextaccessor.httpcontext.request.host.touricomponent();         });         }    } 

now started update project on asp.net core 1.0. during launch of site following error:

system.invalidoperationexception unable resolve service type 'microsoft.aspnetcore.http.ihttpcontextaccessor' while attempting activate 'myproject.startup'.

at microsoft.extensions.internal.constructormatcher.createinstance(iserviceprovider provider) @ microsoft.extensions.internal.activatorutilities.createinstance(iserviceprovider provider, type instancetype, object[] parameters) @ microsoft.extensions.internal.activatorutilities.getserviceorcreateinstance(iserviceprovider provider, type type) @ microsoft.extensions.dependencyinjection.activatorutilities.getserviceorcreateinstance(iserviceprovider provider, type type) @ microsoft.aspnetcore.hosting.internal.startuploader.loadmethods(iserviceprovider services, type startuptype, string environmentname) @ microsoft.aspnetcore.hosting.<>c__displayclass1_0.b__1(iserviceprovider sp) @ microsoft.extensions.dependencyinjection.servicelookup.factoryservice.invoke(serviceprovider provider) @ microsoft.extensions.dependencyinjection.scopedcallsite.invoke(serviceprovider provider) @ microsoft.extensions.dependencyinjection.singletoncallsite.invoke(serviceprovider provider) @ microsoft.extensions.dependencyinjection.<>c__displayclass12_0.b__0(serviceprovider provider) @ microsoft.extensions.dependencyinjection.serviceprovider.getservice(type servicetype) @ microsoft.extensions.dependencyinjection.serviceproviderserviceextensions.getrequiredservice(iserviceprovider provider, type servicetype) @ microsoft.extensions.dependencyinjection.serviceproviderserviceextensions.getrequiredservice(iserviceprovider provider) @ microsoft.aspnetcore.hosting.internal.webhost.ensurestartup() @ microsoft.aspnetcore.hosting.internal.webhost.ensureapplicationservices() @ microsoft.aspnetcore.hosting.internal.webhost.buildapplication()

.net framework x64 v4.0.30319.42000 | microsoft.aspnetcore.hosting version 1.0.0-rtm-21431 | microsoft windows 6.1.7601 s

how new version ihttpcontextaccessor during application startup?

it not longer default service. have configure in startup.cs

services.tryaddsingleton<ihttpcontextaccessor, httpcontextaccessor>(); services.tryaddsingleton<iactioncontextaccessor, actioncontextaccessor>(); 

update: in asp.net core 2.1, the addhttpcontextaccessor helper extension method added correctly register ihttpcontextaccessor correct lifetime (singleton). so, in asp.net core 2.1 , above, code should be

services.addhttpcontextaccessor(); services.tryaddsingleton<iactioncontextaccessor, actioncontextaccessor>(); 

source: https://github.com/aspnet/hosting/issues/793


Comments

Popular posts from this blog

Help with Missing Filename Error?

more info - Joomla! Forum - community, help and support

Using phone battery to power a plane.