簡體   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