Как получить Iconfiguration и IHostingenvironment в .net 4.7? - PullRequest
0 голосов
/ 10 сентября 2018

Я продолжаю находить, как это сделать для ядра .net, но я работаю над библиотекой для netstandard, и если я хочу использовать эту библиотеку в .net 4.7, а также как получить доступ к конфигурации значков и среде ihosting, когда я я в .net 4.7. Я продолжаю находить информацию, что вам нужно сделать внедрение зависимости, чтобы заставить его работать, но никто не показывает примеры кода того, как это делается.

Я нахожу эти строки кода во всей информации, которую я читал, и говорю что-то о введении их там:

    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)
    {   
    }
}

как мне получить .net core iconfiguration / ihostingenvironment эквивалент в .net 4.7 +?

1 Ответ

0 голосов
/ 10 сентября 2018

Код запуска здесь, как правило, .NET Core ... для 4.7 с MVC, вы обычно вызываете DependencyResolver.SetResolver из Global.asax (Application_Start() метод), используя любую среду DI, которую вы хотите (Unity, SimpleInjector, Autofac и т. Д.).

Вот рекомендуемый фрагмент от 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));
}

От: https://simpleinjector.readthedocs.io/en/latest/mvcintegration.html?highlight=mvc

UPDATE

IHostingConfiguration и IConfiguration являются интерфейсами, реализованными только в .NET Core, и недоступны в .NET 4.7.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...