I keep finding how to do this for .net core, but I am working on a library for netstandard and if i want to use this library in .net 4.7 as well how can I access the iconfiguration and the ihosting environment when I am in .net 4.7. I keep finding info that you need to do dependency injection to get it to work, but no one shows code examples of how it is done.
I keep finding these lines of code on all the info that I been reading, saying something about injecting them there:
public class Startup
{
public void Configuration(IAppBuilder app)
{
var services = new ServiceCollection();
ConfigureServices(services);
var resolver = new DefaultDependencyResolver(services.BuildServiceProvider());
DependencyResolver.SetResolver(resolver);
}
public void ConfigureServices(IServiceCollection services)
{
}
}
how do I get the .net core iconfiguration/ihostingenvironment equivalent in .net 4.7+?
The startup code here is usually the .NET Core...for 4.7 with MVC, you usually call the DependencyResolver.SetResolver
from the Global.asax ( Application_Start()
method) using whatever DI framework you want (Unity, SimpleInjector, Autofac, etc).
Here is the suggested snippet from SimpleInjector:
// You'll need to include the following namespaces
using System.Web.Mvc;
using SimpleInjector;
using SimpleInjector.Integration.Web;
using SimpleInjector.Integration.Web.Mvc;
public class WebApiApplication : System.Web.HttpApplication
// This is the Application_Start event from the Global.asax file.
protected void Application_Start(object sender, EventArgs e) {
// Create the container as usual.
var container = new Container();
container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();
// Register your types, for instance:
container.Register<IUserRepository, SqlUserRepository>(Lifestyle.Scoped);
// This is an extension method from the integration package.
container.RegisterMvcControllers(Assembly.GetExecutingAssembly());
container.Verify();
DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
}
From: https://simpleinjector.readthedocs.io/en/latest/mvcintegration.html?highlight=mvc
UPDATE
The IHostingConfiguration and IConfiguration are interfaces only implemented with .NET Core and are unavailable in .NET 4.7.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.