WCF 服务引用生成自己的契约接口,不会重用我的 [英] WCF Service Reference generates its own contract interface, won't reuse mine

查看:27
本文介绍了WCF 服务引用生成自己的契约接口,不会重用我的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的第一个问题所以希望它是合适的:

My first question so hope it is suitable:

共享接口程序集 - 我有一个共享"程序集,它有一个接口,我们称之为 IDocRepository.它用 [ServiceContract] 标记,并且有几个 [OperationContract] 标记的方法.

Shared interface assembly - I have a 'shared' assembly which has an interface, let's call it IDocRepository. It's marked with [ServiceContract] and there are several [OperationContract]-marked methods.

WCF 实现程序集 - 我有两个 WCF 服务项目,每个项目都引用共享程序集,每个项目都将该接口实现为 WCF 服务.

WCF implementation assemblies - I have two WCF service projects, each referencing the shared assembly, each implementing that interface as a WCF service.

消费者程序集 - 最后,我有一个客户端"项目,也引用了共享程序集,并引用了两个 WCF 服务中的每一个.

Consumer assembly - Finally, I have a 'client' project, also referencing the shared assembly, with a reference to each of the two WCF services.

但是,在消费者程序集中生成的服务引用源自自动生成的接口版本:

However, the service references generated in the consumer assembly derive from an auto-generated version of the interface:

public partial class ExampleClient : System.ServiceModel.ClientBase<SomeNamespace.ExampleSvcRef.IDocRepository>, SomeNamespace.ExampleSvcRef.IDocRepository {

我所期望的
我本来希望两个引用都会自动继承我定义的接口,消费者/客户端程序集也在引用.有点像重用它为参数和返回类型提供的类,但用于服务接口.

What I expected
I would have hoped that both references would instead automatically inherit the interface I defined, that the consumer/client assembly is also referencing. Kind of like the re-use of classes that it provides for parameter and return types, but for the service interface.

为什么
这样我就可以创建任一服务引用代理的实例并将其转换为我的接口类型.

Why
So that I can create an instance of either service reference proxy and cast it to my interface type.

所以我每次都可以手动修改生成的代码,但应该有更好的方法......?

So I could modify the generated code by hand each time, but there should be better way...?

(我确实为两个服务引用选择了在引用程序集中重用类型"和在所有引用程序集中重用类型"选项)

(edit: I do have 'Reuse types in referenced assemblies' and 'Reuse types in all referenced assemblies' options selected for both service references)

推荐答案

在引用的程序集中重用类型"仅允许您重用数据协定,而不是服务协定.如果您想共享服务合同,则根本不需要使用添加服务引用".您可以直接使用 ChannelFactory.

"Reuse types in referenced assemblies" only allows you to reuse Data Contracts, not Service Contracts. If you want to share Service Contracts, you don't need to use "Add Service Reference" at all. You can just use ChannelFactory directly.

// Supply the binding and address in code
Binding binding = new BasicHttpBinding();
EndpointAddress address = new EndpointAddress("http://tempuri.org/address");
IServiceContract channel = ChannelFactory<IServiceContract>.CreateChannel(binding, address);

// Or read them from the config file
ChannelFactory<IServiceContract> channelFactory = new ChannelFactory<IServiceContract>();
IServiceContract channel = channelFactory.CreateChannel();

频道对象还将实现 ICommunicationObject,因此,如果您需要调用 Open() 或 Close() 等方法,则可以对其进行转换.

The channel object will also implement ICommunicationObject, so you can cast it if you need to call methods like Open() or Close().

这篇关于WCF 服务引用生成自己的契约接口,不会重用我的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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