2012-01-08 69 views
0

我在ASP.NET应用程序中使用WCF Facility和实体框架。 的目标是保持在的DbContext IoC容器见例如:WCF城堡温莎生活风格?

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方法调用之后,dbContexta的新实例。 我怎么能做到这一点?

谢谢!

+0

您可以重新解释一下:*问题是在wcf方法调用inst DBContext的保存被保存。我想每个单独的wcf方法调用后都有新的DBContext实例。我怎么能做到这一点?*真的不清楚你在问什么,你现在看到什么行为 – 2012-01-08 22:40:32

+0

我同意以前的评论。从我阅读你的问题的方式来看,它应该做你想做的事。 PerWcfOperation表示IDBContext实例的作用域为当前的WCF请求/方法。 – Sneal 2012-01-08 22:46:02

+0

例如:1)调用wcf方法CustomerGetById(),实例化dbcontext 2)调用wcf方法ProductGetById(),现在我具有与以前相同的dbcontext实例。目标是有一个新的。 谢谢! – mirt 2012-01-08 23:01:02

回答

0

以下代码将在WindsorContainer中将组件注册为PerWebRequest生活方式。

public void Register(Component component) 
    { 
     WindsorContainer container = new WindsorContainer(); 
     IKernel kernel = container.Kernel; 
     kernel.AddComponent(component.Name, component.Service, component.Impl,LifestyleType.PerWebRequest);         
    } 

让生活风格PerWebRequest你必须添加在Web.config中下面

<system.web> 
    <httpModules> 
    <add name="PerRequestLifestyle" type="Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule, Castle.MicroKernel" /> 
    </httpModules> 
</system.web> 

<system.webServer> 
<modules> 
    <add name="PerRequestLifestyle" type="Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule, Castle.MicroKernel" /> 
</modules> 
</system.webServer> 
+0

我试着用下面的代码:Component.For ().ImplementedBy ().LifeStyle.PerWebRequest但它不起作用 – mirt 2012-01-15 20:29:00

0

尝试:

Container.Register(
     Component.For<IDBContext>().ImplementedBy<DBContext>().LifeStyleTransient().AsWcfService() 
     ); 

对我的作品,但我不能弄清楚如何判断温莎使用我在服务中定义的Servicebehavior