如何在注入WCF依赖当组合物根是在客户端上 [英] How to inject dependencies in WCF when the Composition Root is on the client

查看:104
本文介绍了如何在注入WCF依赖当组合物根是在客户端上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我开始我必须说,我可能咬比我更嚼不烂,但我是一个绝望的学习横冲直撞,我需要很多帮助。



我我的编码练习,从两本书取样品:结果
1.马克塞曼结果
依赖注入净2.专业的ASP.NET设计模式由布赖恩·伊根和史蒂夫·巴伦苏埃拉



这次演习是落实使用WCF为服务层使用依赖从客户端应用程序组成的根射出的请求/响应消息传递模式。



从2:结果
在演习中,为服务层我有五个类库:结果
- 合同:与接口为服务的合同结果
- 数据合同:与所有的装饰与DataContractAttribute结果
的对象 - HttpHost:主机为WCF服务。这个库包含了所有的SVC文件结果
- ServiceProxy:该库手动实现了服务为客户proxys使用结果
- 服务:包含服务的实现。



从1:结果
我想既是一个控制台和ASP.NET的MVC客户来测试这个锻炼,组成根Main方法,为第二个第一个和在Global.asax和自定义控制器工厂组合



所以我的问题是:




  1. 如果正在客户端上实现的成分根,我必须还提供了ServiceHostFactory,ServiceHost的一个自定义实现,并在IInstanceProvider WCF?会不会使我有两个组成根源是什么?

  2. 如果(希望)我只需要组成扎根于客户,我在哪里创建具有依赖性的建设者?在服务的实现或服务的代理或两者兼而有之?

  3. 应如何对象层次进行配置?我想先用穷人的DI和这一次的运行纳入结构图作为IoC容器。



感谢你这么多的帮助。



这是我的代码到目前为止(我不包括服务合同,也不是数据合同):



服务实现:

 命名空间Exercise.Services 
{
公共类PurchaseOrderService:IPurchaseOrderService
{

私人只读IPurchaseOrderFacade PurchaseOrderFacade;

公共PurchaseOrderService(IPurchaseOrderFacade purchaseOrderFacade)
{
PurchaseOrderFacade = purchaseOrderFacade;
}

公共PurchaseOrderResponse CreatePurchaseOrder(PurchaseOrderRequest purchaseOrderRequest)
{
VAR purchaseOrder的= PurchaseOrderFacade.CreatePurchaseOrder(purchaseOrderRequest.ToPurchaseOrder());

VAR purchaseOrderResponse = purchaseOrder.ToPurchaseOrderResponse();
purchaseOrderResponse.IsSuccessful = TRUE;

返回purchaseOrderResponse;
}

公共PurchaseOrderResponse UpdateState(PurchaseOrderRequest purchaseOrderRequest)
{
VAR purchaseOrder的= PurchaseOrderFacade.UpdateState(purchaseOrderRequest.ToPurchaseOrder());

VAR purchaseOrderResponse = purchaseOrder.ToPurchaseOrderResponse();
purchaseOrderResponse.IsSuccessful = TRUE;

返回purchaseOrderResponse;
}
}
}

这是为代理客户端:

 命名空间Exercise.ServiceProxy 
{
公共类PurchaseOrderProxy:ClientBase< IPurchaseOrderService>中IPurchaseOrderService
{
公共PurchaseOrderResponse CreatePurchaseOrder(purchaseOrderRequest purchaseOrderRequest)
{
返回base.Channel.CreatePurchaseOrder(purchaseOrderRequest);
}

公共PurchaseOrderResponse UpdateState(PurchaseOrderRequest purchaseOrderRequest)
{
返回base.Channel.UpdateState(purchaseOrderRequest);
}
}
}


解决方案

每个应用程序都有其自身的构成根。 WCF服务是对自己的应用程序,并且会自动获得自己的根组成。还是让我换种方式,客户端不应该规定该服务将如何建立自己的对象图,很难想象,因为该服务将生活在自己的进程和最经常一个单独的机器上。



这并不意味着多个应用程序(比如在相同的溶液)不能共享DI结构的一部分,但是在客户端的对象图和服务将甚至可能没有什么共同之处(它们不共享几乎相同的代码库)。您将看到的配置更加分享,当你建立这两个WCF服务,并使用相同的业务层,例如Windows服务。但是,即使在这种情况下,虽然他们也许分享DI配置的很大一部分,我们仍然会说,他们有自己的根组成。


Before I start I must say that I maybe biting more than I can chew but I am in a desperate learning rampage and I need a lot of help.

I am coding an exercise taking the samples from two books:
1. Dependency Injection in .Net by Mark Seemann
2. Professional ASP .Net Design Patterns by Brian Egan and Steve Valenzuela

The exercise is implementing the Request/Response messaging pattern using WCF as the service layer AND using Dependency Injection from a Composition Root in the client application.

From 2:
In the exercise, for the service layer I have five class libraries:
- Contracts: with the interfaces for the services' contracts.
- Data Contracts: with all the objects that are decorated with the DataContractAttribute
- HttpHost: the host for the WCF services. This library contains all the svc files
- ServiceProxy: this library manually implements the services' proxys for the clients to use
- Services: contains the implementations of the services.

From 1:
I want to test this exercise with both a console and an ASP .Net MVC clients so the composition roots are the Main method for the first one and the Global.asax and custom controller factory combination for the second one.

So my questions are:

  1. If the composition root is being implemented on the client, do I have to provide yet a custom implementation for the ServiceHostFactory, ServiceHost, and IInstanceProvider in WCF? Wouldn't that make me have two composition roots?
  2. If (hopefully) I only need the composition root in the client, where do I create the constructors with the dependencies? In the service's implementation or in the service's proxy or in both?
  3. How should the object hierarchy be configured? I want to first use Poor Man's DI and once that's running to incorporate Structure Map as the IoC container.

Thank you so much for your help.

This is the code I have so far (I'm not including the service contract nor the data contracts):

Service Implementation:

namespace Exercise.Services
{
    public class PurchaseOrderService : IPurchaseOrderService
    {

        private readonly IPurchaseOrderFacade PurchaseOrderFacade;

        public PurchaseOrderService(IPurchaseOrderFacade purchaseOrderFacade)
        {
            PurchaseOrderFacade = purchaseOrderFacade;
        }

        public PurchaseOrderResponse CreatePurchaseOrder(PurchaseOrderRequest purchaseOrderRequest)
        {
            var purchaseOrder = PurchaseOrderFacade.CreatePurchaseOrder(purchaseOrderRequest.ToPurchaseOrder());

            var purchaseOrderResponse = purchaseOrder.ToPurchaseOrderResponse();
            purchaseOrderResponse.IsSuccessful = true;

            return purchaseOrderResponse;
        }

        public PurchaseOrderResponse UpdateState(PurchaseOrderRequest purchaseOrderRequest)
        {
            var purchaseOrder = PurchaseOrderFacade.UpdateState(purchaseOrderRequest.ToPurchaseOrder());

            var purchaseOrderResponse = purchaseOrder.ToPurchaseOrderResponse();
            purchaseOrderResponse.IsSuccessful = true;

            return purchaseOrderResponse;
        }
    }
}

This is the proxy for the client:

namespace Exercise.ServiceProxy
{
    public class PurchaseOrderProxy : ClientBase<IPurchaseOrderService>, IPurchaseOrderService
    {
        public PurchaseOrderResponse CreatePurchaseOrder(PurchaseOrderRequest purchaseOrderRequest)
        {
            return base.Channel.CreatePurchaseOrder(purchaseOrderRequest);
        }

        public PurchaseOrderResponse UpdateState(PurchaseOrderRequest purchaseOrderRequest)
        {
            return base.Channel.UpdateState(purchaseOrderRequest);
        }
    }
}

解决方案

Every application has its own Composition Root. The WCF service is an application on its own, and will automatically get its own Composition Root. Or let me put it differently, the clients shouldn't dictate how the service is going to build up its object graphs and it’s hard to imagine since the service will live in its own process and most often on a separate machine.

This doesn't mean multiple applications (in the same solution for instance) can't share a part of the DI configuration, but the object graphs of the client and the service will probably even have little in common (they don’t share much of the same code base). You will see much more sharing of the configuration, when you're building both a WCF service and a Windows Service that use the same business layer, for instance. But even in that case, although they perhaps share a great part of the DI configuration, we'll still say that they have their own Composition Root.

这篇关于如何在注入WCF依赖当组合物根是在客户端上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆