我们有一个现有的MVC3项目,并使用Autofac进行DI。
要提供RESTful服务,我们打算使用ServiceStack。我们如何使ServiceStack使用初始化autofac的现有Bootstrapper.c?
我们现有的Bootrapper.cs:
public static class Bootstrapper
{
public static ContainerBuilder builder;
public static void Initialise()
{
builder = new ContainerBuilder();
builder.RegisterControllers(typeof(MvcApplication).Assembly);
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
}
答案 0 :(得分:1)
ServiceStack文档提供了一个示例here。您只需要实现一个适配器来转发解析为Autofac。
<强>更新强>
您可以在注册服务后返回容器实例:
public static IContainer Initialise()
{
builder = new ContainerBuilder();
builder.RegisterControllers(typeof(MvcApplication).Assembly);
Register types here builder.Register(x => new FormsAuthWrapper()).As<IFormsAuthentication>();
builder.Register(x => new ServiceDbContext()).As<DbContext>().As<ServiceDbContext>().InstancePerHttpRequest();
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
return container;
}
然后您可以使用此实例创建适配器。
IContainerAdapter adapter = new AutofacIocAdapter(Bootstrapper.Initialise());
container.Adapter = adapter;