2016-12-04 36 views
0

私はASP.Coreを初めて使っています。私たちはプロジェクトを持っており、Dependency InjectionとStructure Mapを使用する必要があります。次のメソッドでStartUp.csファイルにコンテナを配置するのが正しい方法かどうか疑問に思っています。そして、それは以下のような構成でIHttpContextAccessorを入れてsafeある場合:ASP.NETコアのベース依存コンテナを変更する場合ASP.Core StructureMap Container

public void ConfigureServices(IServiceCollection services) { 

     Container container = new Container(expr => { 
      expr.For<IDataContextService>().Use<DataContextService>(); 
      expr.For<IHttpContextAccessor>().Use<HttpContextAccessor>(); 
      expr.For<ISessionService>().Use<SessionService>(); 
     }); 
     services.AddSingleton<IContainer>(container); 
    } 

答えて

2

、ConfigureServicesは彼にIServiceProvider

Andewロックのインスタンスを返す必要がありますブログpostはそれを実行する方法を示しています。彼のブログ記事からの引用

、それはあなたがAutofacを使用してに興味が史上場合は、その対象

ASP.NET documentationを参照することができますのStructureMap

public IServiceProvider ConfigureServices(IServiceCollection services) 
{ 
    // Add framework services. 
    services.AddMvc() 
     .AddControllersAsServices(); 

    return ConfigureIoC(services); 
} 

public IServiceProvider ConfigureIoC(IServiceCollection services) 
    { 
     var container = new Container(); 

     container.Configure(config => 
     { 
      // Register stuff in container, using the StructureMap APIs... 
      config.Scan(_ => 
      { 
       _.AssemblyContainingType(typeof(Startup)); 
       _.WithDefaultConventions(); 
       _.AddAllTypesOf<IGamingService>(); 
       _.ConnectImplementationsToTypesClosing(typeof(IValidator<>)); 
      }); 

      config.For(typeof(IValidator<>)).Add(typeof(DefaultValidator<>)); 
      config.For(typeof(ILeaderboard<>)).Use(typeof(Leaderboard<>)); 
      config.For<IUnitOfWork>().Use(_ => new UnitOfWork(3)).ContainerScoped(); 

      //Populate the container using the service collection 
      config.Populate(services); 
     }); 

     return container.GetInstance<IServiceProvider>(); 

    } 
} 

ため、このようなものになるだろう