简体   繁体   English

与Unity共享WCF服务合同

[英]Sharing WCF Service Contracts with Unity

I am fairly new with WCF, I'm looking to share a service contract and data contracts between service in one solution and a service consumer in another solution. 我对WCF相当陌生,我希望在一个解决方案中的服务与另一个解决方案中的服务使用者之间共享服务合同和数据合同。 I have created a seperate project which is shared between both solutions that contains both the service contracts and the data contracts. 我创建了一个单独的项目,该项目在包含服务合同和数据合同的两个解决方案之间共享。 When i reference my service The data contracts are being shared but not the service contract. 当我引用我的服务时共享数据合同,但不共享服务合同。 My contract looks like the following: 我的合同如下所示:

[ServiceContract]
public interface IDistributionService
{
    [OperationContract]
    [ServiceKnownType(typeof (Distribution))]
    bool AddDistributions(Distribution[] oDistributions);
}

When I look at the reference.cs file I is using the Distribution objects but it's generating the IDistributionService contract again, here is an excerpt of the reference.cs file: 当我查看reference.cs文件时,我正在使用Distribution对象,但是它再次生成IDistributionService合同,这是reference.cs文件的摘录:

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(ConfigurationName="eDistributorService.IDistributionService")]
public interface IDistributionService {
   [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IDistributionService/AddDistributions", ReplyAction="http://tempuri.org/IDistributionService/AddDistributionsResponse")]
    bool AddDistributions(mhcb.syd.eDistribution.WebServices.Contracts.DTO.Distribution[] oDistributions);
    }

Is there any way of getting the consumer to use the IDistributionService interface without generating the concrete instance in the reference.cs file? 有什么方法可以使使用者使用IDistributionService接口,而无需在reference.cs文件中生成具体实例?

The main reason I want to know is so that I can use dependency injection (unity). 我想知道的主要原因是可以使用依赖项注入(统一性)。

.RegisterType<IDistributionService, DistributionServiceClient>(
                new Interceptor<InterfaceInterceptor>(),
                new InterceptionBehavior(container.Resolve<IInterceptionBehavior>()))

Thanks again! 再次感谢!

Firstly check that you are instructing the service reference tool to 'reuse types'. 首先,检查您是否指示服务参考工具“重用类型”。 When you go to 'Add Service Reference...', click 'Advanced' and confirm that the 'Reuse types in referenced assemblies' option is checked. 当您转到“添加服务引用...”时,单击“高级”并确认已选中“在引用的程序集中复用类型”选项。

If this is checked (which I believe is the default behaviour), then you might not be able to do what you want by adding adding a 'service reference'. 如果选中此选项(我相信这是默认行为),则可能无法通过添加“服务引用”来完成您想要的操作。

Sharing a contract is the preferred WCF pattern (in my opinion), but the implementation I normally take is to use the ChannelFactory class to instantiate the client connection rather than use the generated proxy. 共享合同是首选的WCF模式(我认为),但是我通常采用的实现是使用ChannelFactory类实例化客户端连接,而不是使用生成的代理。 The generated proxy is fine - it's quick and easy - but it is clunky and requires maintenance. 生成的代理很好-快速又容易-但是笨重并且需要维护。

Review this MSDN link on Using ChannelFactory vs Proxies in WCF for a discussion, and this CodeProject link for sample usage. 复审有关在WCF中使用ChannelFactory和Proxies的 MSDN链接以进行讨论,并查看此CodeProject链接以获取示例用法。

The code artefacts svcutil produces suck. 代码伪像svcutil产生吮吸。 But they are by far the easiest (ie cheapest) way to use the decoupling that WCF provides. 但是,到目前为止,它们是使用WCF提供的去耦的最简单(即最便宜)的方式。 Reusing contracts and entities is tight coupling of service and consumer. 重用合同和实体是服务和消费者之间的紧密耦合

If you want your container to generate client-side proxies for WCF services you can use an extension for Unity that does the trick. 如果您想让容器为WCF服务生成客户端代理,则可以使用Unity扩展来解决问题 It supports the WCF4 discovery features as well. 它还支持WCF4发现功能。

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

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