“反向"WCF 服务(根据客户端定义构建服务器) [英] "Reverse" WCF Service (Building a server from a client definition)

查看:21
本文介绍了“反向"WCF 服务(根据客户端定义构建服务器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.Net 提供了一些很好的机制来定义远程服务,然后自动"创建一个客户端来连接到它,隐藏了很多讨厌的布线和大惊小怪.但是有没有类似的路线可以走另一条路?

.Net provides some wonderful mechanisms for defining a remote service and then "automagically" creating a client to connect to it, hiding away much of the nasty wiring and fuss. But is there a similar route for going the other way?

我最近的工作任务是创建一系列服务,这些服务将在身份验证和搜索查询等方面相互通信.一个要求是我们的核心服务能够调用其他服务节点",这些节点可能是内部创建的,也可能不是,但都需要实现一个通用接口.我可以构建此服务的参考实现,从中创建 WSDL,并自动生成客户端而不会出现问题.然而,似乎定义服务的唯一方法基本上是将某人指向我创建的 WSDL 并说实现看起来像那样的东西".鉴于 WSDL 很少被设计为可供人类阅读,这条路线似乎没有吸引力.

My most recent task at work is to create a series of services that will communicate with one another for things like authentication and search queries. One requirement is the ability for our core service to call out to other "service nodes", which may or may not be created in house but all need to implement a common interface. I can build a reference implementation of this service, create the WSDL from that, and automatically generate the client side without issue. It seems that the only way to define the service, however, is to basically point someone at the WSDL I create and say "Implement something that looks like that." Seeing as how WSDLs are rarely designed to be read by human beings, this route seems less than attractive.

那么有没有一种我不知道的从 WSDL 或类似描述符生成服务接口的方法?主要看这里的 .NET 3.5,使用 C# 和 WCF.谢谢!

So is there a way that I'm not aware of to generate a service interface from a WSDL or similar descriptor? Primarily looking at .NET 3.5 here, using C# and WCF. Thanks!

推荐答案

如果您有一个 WSDL 来描述您的客户端想要调用的方法,以及一个 XSD(XML 架构),它描述了它将期望发送的数据元素和接收,您拥有从它创建服务所需的一切.这被称为构建 WCF 服务的合同优先"方法,尤其在具有互操作性要求的环境(如 Java 客户端和 .NET 服务,反之亦然)中非常流行.

If you have a WSDL which describes the methods your client wants to call, plus a XSD (XML Schema) which describes what data elements it will expect to send and receive, you have all it takes to create a service from it. This is called the "Contract First" approach to building WCF services and is fairly popular especially in environments with interoperability requirements (like Java client and .NET services or vice-versa).

使用svcutil.exe,你可以生成一个服务接口类——你的服务描述.这将包含服务合同、运营合同和数据合同.

Using svcutil.exe, you can generate a service interface class - your service description. This will contain service contract, operation contracts, and data contracts.

svcutil yourMethods.wsdl yourDataSchema.xsd /language:C# /out:YourServiceInterface.cs
(or /language:VB, if you prefer VB.NET)

这将创建一个 YourServiceInterface.cs(或 YourServiceInterface.vb)文件,这是您的服务器代码的基础.

This will create a YourServiceInterface.cs (or YourServiceInterface.vb) file, which is the basis for your server code.

由此,您可以创建一个实际的服务实现 - 您的服务类执行实际工作.

From this, you can then create an actual service implementation - your service class that does the real work.

最后但并非最不重要的一点是,您需要决定如何托管您的服务 - 在 IIS 中或在控制台应用程序或 NT 服务中自托管,或其他方式.

And last but not least, you'll need to decide how to host your service - in IIS or self-hosted in a console app or an NT service or some other means.

马克

这篇关于“反向"WCF 服务(根据客户端定义构建服务器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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