【发布时间】:2012-01-08 22:36:28
【问题描述】:
我在 ASP.NET 应用程序中使用带有实体框架的 WCF Facility。 目标是将 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 公共类 CustomerService : 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 实例。 我怎样才能做到这一点?
谢谢!
【问题讨论】:
-
你能改写一下吗:问题是wcf方法调用后DBContext的实例被保存了。我想在每个单独的 wcf 方法调用之后都有 DBContext 的新实例。我如何才能做到这一点? 真的不清楚你在要求什么以及你现在看到什么行为
-
我同意前面的评论。从我阅读您的问题的方式来看,它应该做您想做的事情。 PerWcfOperation 意味着 IDBContext 实例的范围是当前 WCF 请求/方法。
-
例如:1)调用wcf方法CustomerGetById(),实例化dbcontext 2)调用wcf方法ProductGetById(),现在我有了和之前一样的dbcontext实例。目标是拥有一个新的。谢谢!
标签: c# asp.net wcf entity-framework castle