繁体   English   中英

WCF城堡温莎堡生活方式酒店?

[英]Castle Windsor LifeStyle for 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公共类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的新实例。 我该如何实现?

谢谢!

尝试:

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

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

以下代码将在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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM