Web APIプロジェクトがあり、IoCコンテナとしてAutofac Web API統合を使用しています。私達は私達の種類のすべてを登録するために使用したコードは次のとおりです。Autofacの自動登録機能を使用する場合、1つのタイプをInstancePerRequestにする方法を指定するには
public class CompositionRootConfigurator
{
public static AutofacWebApiDependencyResolver Configure(Assembly servicesAssembly)
{
var container = BuildContainer(servicesAssembly);
var resolver = new AutofacWebApiDependencyResolver(container);
return resolver;
}
public static IContainer BuildContainer(Assembly servicesAssembly)
{
/*TO DELETE ONCE THE REFERENCES ISSUE IS RESOLVED!*/
var dummy = new EmployeesBL(new ContextFactory(new DBContextFactory(new RoleBasedSecurity(), new Identity())));
var builder = new ContainerBuilder();
if (servicesAssembly != null) // this is a temporary workaround, we need a more solid approach here
{
builder.RegisterApiControllers(servicesAssembly);
}
/* Registers all interfaces and their implementations from the following assemblies in the IoC container
* 1. CB.CRISP.BL
* 2. CB.CRISP.BL.CONTRACTS
* 3. CB.CRISP.DAL
* 4. CB.CRISP.DAL.CONTRACTS
* The current assembly is excluded because the controllers were registered with the builder.RegisterApiControllers expression above.
*/
var appAssemblies = AppDomain.CurrentDomain
.GetAssemblies()
.Where(a => a.ToString().StartsWith("CB.CRISP"))
.ToArray();
builder.RegisterAssemblyTypes(appAssemblies).AsImplementedInterfaces();
if (servicesAssembly != null)
{
builder.RegisterAssemblyTypes(servicesAssembly).AsImplementedInterfaces();
}
return builder.Build();
}
}
は、今、私たちはIMyTypeを実装がMyTypeを持っており、これはリクエストごとに単一のインスタンスでなければならない唯一のものであると仮定階層に沿っていくつかのオブジェクトに挿入されます。
私はこの既存のコード内でこれを指定する方法を忘れています。私は先に行くと、また、1件の登録は、他のものを上書きします他のすべてに登録されますので、ちょうど彼らが複製され、
builder.RegisterType<MyType>()
.As<IMyType>()
.InstancePerRequest();
をすれば、そこに潜在的な問題がありますか?
ご理解いただきありがとうございます。