共享WCF服务和客户端之间的接口(标记为W / ServiceContract) [英] Sharing Interfaces Between a WCF Service and Client (marked w/ ServiceContract)

查看:245
本文介绍了共享WCF服务和客户端之间的接口(标记为W / ServiceContract)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VS2010和.NET 4.0。

I'm using VS2010 and .NET 4.0.

我在这里查了一下(共享在WCF服务中实现的接口)但不同之处在于我正在尝试完成的工作涉及的接口是标有ServiceContract。

I've checked here (Sharing Interfaces that are implemented in WCF Service) but it differs in that what I'm trying to accomplish involves an interface that is marked with ServiceContract.

即在服务中,我有一个接口A(标记为ServiceContract)和一个扩展接口B(也标记为ServiceContract)。 B由WCF服务实现。

I.e. in the service, I have an interface A (marked as a ServiceContract) and an extending interface B (also marked as a ServiceContract). B is implemented by the WCF service.

确切的扩展名如下:

public interface A<T>

public interface B : A<SpecificType>

(SpecificType标记为DataContract。)

("SpecificType" is marked as a DataContract.)

显然,B在代理生成过程中暴露给WCF客户端;但是,我还需要公开接口A(我正在实现一个半通用的pub / sub系统,而发布者需要能够检查/依赖接口A)。

Obviously, B is exposed to the WCF client in the proxy-generating process; however, I need to expose interface A, as well (I'm implementing a semi-generic pub/sub system and the "publisher" needs to be able to check/rely on interface A).

我尝试解决这个问题的第一种方法是创建一个包含接口的独立共享程序集,并且可以由服务和客户端使用;然而,最终效果不好,因为在发布者中,它需要确保B的实例实际上从A正确扩展。这种隐式转换失败,可能是因为服务引用似乎并不完全jive与共享程序集。

The first way I tried to solve this was by creating a separate, "shared" assembly that contained the interface and could be used by both the service and the client; however, that ended up not working so well because, in the publisher, it needs to ensure that an instance of B is, in fact, properly extended from A. This implicit conversion fails, likely because the service reference doesn't seem to entirely jive with the "shared" assembly.

为了解决这个问题,我手动编辑了Re​​ference.cs文件,它最终工作了(我添加了接口A的定义和确保接口B正确引用它。但这带来了一个很大的问题,每次我更新服务引用时,这段代码都会被删除。

To get around this, I manually edited the Reference.cs file and it ended up working (I added the definition of interface A and made sure interface B referred to it properly). But this poses a large problem in that every time I update the service reference, this code will get wiped out.

看过这个网站和其他网站上的其他WCF响应,我似乎无法找到确切的答案(也许我只是没有完成所有这些和他们的回复)。

Having looked at other WCF responses on this site and others, I can't quite seem to find an exact answer (perhaps I just didn't get through all of them and their responses).

如果有人可以指出我的正确的方向,我很感激。

If someone could point me in the right direction, I'd appreciate it.

谢谢。

推荐答案

提供对客户端(程序集或链接到.cs文件)的原始服务合同接口的访问权限。然后你有两个选择:

Provide access to 'raw' service contracts interfaces to client (assembly or link to .cs file). Then you have two options:

1)将自动生成的代理调用包装到你自己的类方法调用中(根据需要实现接口)。服务参考更新不会对您造成伤害。

1) Wrap autogenerated proxy calls into your own class method calls (implement interfaces as you want). Service reference updating won't hurt you.

2)不要使用自动生成代理。创建自己的代理(实现接口):

2) Don't use autogenerated proxy. Create your own proxy (implement your interfaces):

var baseAddress = "net.tcp://localhost:4503/MyService"; 
var channelFactory = new DuplexChannelFactory<IMyService>(new InstanceContext(new MyNotificationReceiver()), //MyNotificationReceiver - WCF callback implementation class
    myNetTcpBinding,
    new EndpointAddress(baseAddress));

var proxy = channelFactory.CreateChannel();
proxy.MyMethod();

这篇关于共享WCF服务和客户端之间的接口(标记为W / ServiceContract)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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