如何在 IIS 托管的 WCF 服务中将 Autofac 与 CustomServiceHostFactory 一起使用?
How to use Autofac with a CustomServiceHostFactory in an IIS hosted WCF service?
假设我有一个简单的服务合同:
[ServiceContract(Namespace = Constants.MyNamespace)]
public interface IAccountService
{
[OperationContract]
Account GetByAccountNumber(string accountNumber);
}
这是服务:
[ServiceBehavior(Namespace = Constants.MyNamespace)]
public class AccountService : IAccountService
{
private readonly IUnitOfWorkAsync _uow;
private readonly IRepositoryAsync<Account> _repository;
public AccountService(IDataContextAsync dataContext)
{
_uow = new UnitOfWork(dataContext);
_repository = new Repository<Account>(dataContext, _uow);
}
public Account GetByAccountNumber(string accountNumber)
{
return _repository.GetByAccountNumber(accountNumber);
}
}
这是 CustomServiceHostFactory:
public class CustomServiceHostFactory : ServiceHostFactory
{
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
var builder = new ContainerBuilder();
builder.RegisterType<MyDbContext>().As<IDataContextAsync>();
builder.Register(c => new AccountService(c.Resolve<IDataContextAsync>())).As<IAccountService>();
using (var container = builder.Build())
{
var host = new CustomServiceHost(serviceType, baseAddresses);
host.AddDependencyInjectionBehavior<IAccountService>(container);
return host;
}
}
}
..其中 CustomServiceHost 以编程方式创建所有 bindings/behaviors。我正在使用无文件激活,所以我的 .config 文件只有这样的部分:
<serviceHostingEnvironment>
<serviceActivations>
<add service="Company.Project.Business.Services.AccountService"
relativeAddress="Account/AccountService.svc"
factory="Company.Project.WebHost.CustomServiceHostFactory"/>
</serviceActivations>
</serviceHostingEnvironment>
我发布到 IIS,可以在浏览器中查看网站。它说 "you have created a service"。但是,我尝试从我的客户端应用程序对该服务进行的任何调用都会出现以下错误:
Instances cannot be resolved and nested lifetimes cannot be created from this LifetimeScope as it has already been disposed.
如何将 Autofac 与 WCF 和 CustomServiceHostFactory 一起使用?
我现在可以使用 poor man's DI 作为解决方法,但希望它能正常工作。我似乎无法在网上找到任何好的例子。谢谢。
不要丢弃容器。不要使用 using 语句,而是让容器保持活动状态。它需要和宿主一样长寿。
您会注意到在默认的 Autofac WCF 内容中,容器是在应用程序生命周期内存在的全局静态 - 这就是原因。
假设我有一个简单的服务合同:
[ServiceContract(Namespace = Constants.MyNamespace)]
public interface IAccountService
{
[OperationContract]
Account GetByAccountNumber(string accountNumber);
}
这是服务:
[ServiceBehavior(Namespace = Constants.MyNamespace)]
public class AccountService : IAccountService
{
private readonly IUnitOfWorkAsync _uow;
private readonly IRepositoryAsync<Account> _repository;
public AccountService(IDataContextAsync dataContext)
{
_uow = new UnitOfWork(dataContext);
_repository = new Repository<Account>(dataContext, _uow);
}
public Account GetByAccountNumber(string accountNumber)
{
return _repository.GetByAccountNumber(accountNumber);
}
}
这是 CustomServiceHostFactory:
public class CustomServiceHostFactory : ServiceHostFactory
{
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
var builder = new ContainerBuilder();
builder.RegisterType<MyDbContext>().As<IDataContextAsync>();
builder.Register(c => new AccountService(c.Resolve<IDataContextAsync>())).As<IAccountService>();
using (var container = builder.Build())
{
var host = new CustomServiceHost(serviceType, baseAddresses);
host.AddDependencyInjectionBehavior<IAccountService>(container);
return host;
}
}
}
..其中 CustomServiceHost 以编程方式创建所有 bindings/behaviors。我正在使用无文件激活,所以我的 .config 文件只有这样的部分:
<serviceHostingEnvironment>
<serviceActivations>
<add service="Company.Project.Business.Services.AccountService"
relativeAddress="Account/AccountService.svc"
factory="Company.Project.WebHost.CustomServiceHostFactory"/>
</serviceActivations>
</serviceHostingEnvironment>
我发布到 IIS,可以在浏览器中查看网站。它说 "you have created a service"。但是,我尝试从我的客户端应用程序对该服务进行的任何调用都会出现以下错误:
Instances cannot be resolved and nested lifetimes cannot be created from this LifetimeScope as it has already been disposed.
如何将 Autofac 与 WCF 和 CustomServiceHostFactory 一起使用?
我现在可以使用 poor man's DI 作为解决方法,但希望它能正常工作。我似乎无法在网上找到任何好的例子。谢谢。
不要丢弃容器。不要使用 using 语句,而是让容器保持活动状态。它需要和宿主一样长寿。
您会注意到在默认的 Autofac WCF 内容中,容器是在应用程序生命周期内存在的全局静态 - 这就是原因。