【问题标题】:Castle Windsor LifeStyle for WCF?WCF 的温莎城堡生活方式?
【发布时间】: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


【解决方案1】:

以下代码会将组件注册为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);                                 
     }

要将 Life style 设为 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>

【讨论】:

  • 我尝试使用以下方法:Component.For().ImplementedBy().LifeStyle.PerWebRequest 但它不起作用
【解决方案2】:

试试:

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

对我有用,但我不知道如何告诉 Windsor 使用我在服务中定义的服务行为

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多