简体   繁体   English

如何在.net 4.7中获取Iconfiguration和IHostingenvironment?

[英]How to get Iconfiguration and IHostingenvironment in .net 4.7?

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. 我一直在寻找如何为.net核心执行此操作,但是我正在为netstandard开发一个库,如果我想在.net 4.7中使用此库,以及在.net 4.7中如何访问iconfiguration和ihosting环境。净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+? 如何获得.net 4.7+中的.net核心iconfiguration / ihostingenvironment等效项?

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). 这里的启动代码通常是.NET Core ...对于带有MVC的4.7,您通常使用所需的任何DI框架(Unity,SimpleInjector,Autofac等Application_Start()从Global.asax( Application_Start()方法)中调用DependencyResolver.SetResolver 。 )。

Here is the suggested snippet from SimpleInjector: 这是来自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 来自: 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. IHostingConfiguration和IConfiguration是仅使用.NET Core实现的接口,在.NET 4.7中不可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM