从 WCF 服务转换类型
我确信这一定是一个很常见的问题。我有两个 WCF 服务,它们基本上公开了对一项较大服务的标准访问权限和管理访问权限。
每个服务上都有一个类似的方法Foo
。 Foo
采用在我的服务中定义的 TemplateInfo
类型的参数。
在我的客户端上,我引用了 Client
和 AdminClient
。 Client.Foo(TemplateInfo)
需要 Client.TemplateInfo
,而 AdminClient.Foo(TemplateInfo)
需要 AdminClient.TemplateInfo
。我知道一旦到达服务 Client.TemplateInfo
和 AdminClient.TemplateInfo
是同一件事。
我试图弄清楚如何实例化 TemplateInfo 和/或将其转换为与客户端和管理方法一起使用。有相当多的代码来创建这个对象,所以我希望不要为每种类型重复它。
这些类型是在共享程序集中定义的,我在代理中“重用类型”。但我得到了两种不同的类型,因为我有两个服务参考。
做类似 (AdminClient)TemplateInfo 的事情会很棒,但也许我会创建一个克隆方法来转换类型。
I'm sure this must be a pretty common problem. I have two WCF services, that basically expose standard access and admin access to one larger service.
There is a similar method Foo
on each service. Foo
takes an argument of type TemplateInfo
which is defined in my service.
On my client I have a reference to Client
and AdminClient
. Client.Foo(TemplateInfo)
expects Client.TemplateInfo
while AdminClient.Foo(TemplateInfo)
expects AdminClient.TemplateInfo
. I know that once the service is reached Client.TemplateInfo
and AdminClient.TemplateInfo
are the same thing.
I'm trying to figure out how to instantiate TemplateInfo and/or cast it to work with both client and admin methods. There is a decent amount of code to create this object so I was hoping not to duplicate that for each type.
The types are defined in shared assemblies and I am "Reusing types" in my proxy. But I get the two different types because I have two service references.
It would be great to do something like (AdminClient)TemplateInfo, but perhaps I'll create a clone method to convert the types.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
没有办法做到这一点。它们是不同的类型。
您可以将该类型放入可供服务和客户端引用的类库中。您可以告诉“添加服务引用”重用类型(这是默认值),然后它将使用类库中的类型。
请注意,客户端应用程序还需要引用与这两个服务相同的类库。
There's no way to do that. They are different types.
You can put that type into a class library that is references by both services and by the client. You can tell "Add Service Reference" to reuse types (it's the default), and then it will be the type from the class library that is used.
Note that the client application will also need to reference the same class library as the two services.
除了完全生成服务引用之外,您还可以将服务的类型放入客户端和服务之间的共享程序集中,并在生成客户端类时选中“重用引用程序集中的类型”或类似的内容。
Either than fully generate a service reference, you can put types of the service in a shared assembly between client and service, and check "Reuse types in referenced assemblies" or something like this while generating the client class.
您可以这样做,但通常不被认为是一个好主意。原因是它将您的客户和服务紧密耦合在一起。仅当您控制客户端和两项服务并且准备好应对以下事实时才考虑这样做:当您对其中一项服务进行更改时,可能会破坏客户端。
如果你使用的是 Visual Studio,理查德已经给了我一些关于如何做到这一点的提示。
但
面向服务架构的主要原则之一是“服务共享模式和契约,而不是类”。
有很多好文章比我更好地解释了这一点。维基百科是一个好的开始,或者早在 WCF 有一个很酷的名字时就有这篇文章。 http://msdn.microsoft.com/en-us/magazine/cc164026.aspx
You can do it but it is generally not considered a good idea. The reason being that it tightly couples together your client and services. Only consider doing this if you are in control of the client and both services and are prepared for the fact that when you roll out a change to one of the services you could break the client.
Richard has beat me to it with some hints on how to do this if you are using visual studio.
But
One of the main principles of service oriented architecture is "Services share schema and contract, not class".
There are many good articles explaining this better than I can. Wikipedia is a good start or there is this article from way back when WCF had a cool name. http://msdn.microsoft.com/en-us/magazine/cc164026.aspx