WCF 温莎城堡的生活方式?
我在 ASP.NET 应用程序中使用带有实体框架的 WCF 工具。 目标是将 dbcontext 保留在 IoC 容器中,请参阅示例:
1)Global.asax
protected void Application_Start(object sender, EventArgs e)
{
Container = new WindsorContainer();
Container.AddFacility<WcfFacility>();
Container.Register(
Component.For<IDBContext>().ImplementedBy<DBContext>().LifeStyle.PerWcfOperation()
);
}
2)CustomerService.cs 公共类客户服务:ICustomerService { 私有只读 ICustomerBl customerBl;
public CustomerService(ICustomerBl customerBl)
{
this.customerBl = customerBl;
}
public Customer GetById(int Id)
{
Customer customer = customerBl.GetById(5);
return customer;
}
}
3)CustomerBl.cs
public class CustomerBl : ICustomerBl
{
private ICustomerRepository _repository;
public CustomerBl(ICustomerRepository customerRepository)
{
_repository = customerRepository;
}
public Customer GetById(int Id)
{
return _repository.GetById(5);
}
}
4)CustomerRepository.cs
public class CustomerRepository: ICustomerRepository
{
public IDBContext _dbContext;
public CustomerRepository(IDBContext dbContext)
{
_dbContext = dbContext;
}
public Customer GetById(int Id)
{
_dbContext.ContextCounter = 1;
return new Customer
{
Id = 5,
FirstName = "Joe",
LastName = "Blogg",
Age = 45
};
}
}
5)TestServiceClient
protected void Button1_Click(object sender, EventArgs e)
{
ServiceReference1.CustomerServiceClient customer = new ServiceReference1.CustomerServiceClient();
customer.GetById(5);
}
我正在执行以下操作:
1)从 CustomerGetById() 调用 wcf 方法,dbcontext 在这里实例化 _dbContext.ContextCounter = 0
2) 再次调用并实例化 dbContext - _dbContext.ContextCounter = 1
目标是在每次单个 wcf 方法调用后获得 dbContext 的新实例。 我怎样才能做到这一点?
谢谢!
I'm using WCF Facility with entity framework in ASP.NET application.
The goal is to keep dbcontext in IoC container see example:
1)Global.asax
protected void Application_Start(object sender, EventArgs e)
{
Container = new WindsorContainer();
Container.AddFacility<WcfFacility>();
Container.Register(
Component.For<IDBContext>().ImplementedBy<DBContext>().LifeStyle.PerWcfOperation()
);
}
2)CustomerService.cs
public class CustomerService : ICustomerService
{
private readonly ICustomerBl customerBl;
public CustomerService(ICustomerBl customerBl)
{
this.customerBl = customerBl;
}
public Customer GetById(int Id)
{
Customer customer = customerBl.GetById(5);
return customer;
}
}
3)CustomerBl.cs
public class CustomerBl : ICustomerBl
{
private ICustomerRepository _repository;
public CustomerBl(ICustomerRepository customerRepository)
{
_repository = customerRepository;
}
public Customer GetById(int Id)
{
return _repository.GetById(5);
}
}
4)CustomerRepository.cs
public class CustomerRepository: ICustomerRepository
{
public IDBContext _dbContext;
public CustomerRepository(IDBContext dbContext)
{
_dbContext = dbContext;
}
public Customer GetById(int Id)
{
_dbContext.ContextCounter = 1;
return new Customer
{
Id = 5,
FirstName = "Joe",
LastName = "Blogg",
Age = 45
};
}
}
5)TestServiceClient
protected void Button1_Click(object sender, EventArgs e)
{
ServiceReference1.CustomerServiceClient customer = new ServiceReference1.CustomerServiceClient();
customer.GetById(5);
}
I'm doing following:
1)Call wcf method from CustomerGetById(), dbcontext is instantiated here
_dbContext.ContextCounter = 0
2)Call again and has instantiated dbContext - _dbContext.ContextCounter = 1
The goal is to have new instance of dbContextafter each single wcf method call.
How I can achieve this?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下代码将组件注册为 WindsorContainer 中的 PerWebRequest 生活方式。
要将生活方式设置为 PerWebRequest,您必须在 Web.config 中添加以下内容
The following code will register component as PerWebRequest life style in WindsorContainer.
To make Life style as PerWebRequest you have to add following in Web.config
尝试:
对我有用,但我不知道如何告诉 Windsor 使用我在服务中定义的服务行为
Try:
Works for me, but I cant figure out how I tell Windsor to use the Servicebehavior I defined in the Service