WCF 温莎城堡的生活方式?

发布于 2024-12-25 14:44:13 字数 2143 浏览 0 评论 0原文

我在 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 公共类客户服务: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 的新实例。 我怎样才能做到这一点?

谢谢!

I'm using WCF Facility with entity framework in ASP.NET application.
The goal is to keep dbcontext in IoC container see example:

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
public class CustomerService : ICustomerService
{
private readonly 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);

    }

I'm doing following:

1)Call wcf method from CustomerGetById(), dbcontext is instantiated here
_dbContext.ContextCounter = 0

2)Call again and has instantiated dbContext - _dbContext.ContextCounter = 1

The goal is to have new instance of dbContextafter each single wcf method call.
How I can achieve this?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌吟 2025-01-01 14:44:13

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

The following code will register component as PerWebRequest life style in WindsorContainer.

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

To make Life style as PerWebRequest you have to add following in 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>
原野 2025-01-01 14:44:13

尝试:

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

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

Try:

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

Works for me, but I cant figure out how I tell Windsor to use the Servicebehavior I defined in the Service

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文