简体   繁体   English

“反向”WCF服务(从客户端定义构建服务器)

[英]“Reverse” WCF Service (Building a server from a client definition)

.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. .Net提供了一些很好的机制来定义远程服务,然后“自动”创建一个客户端连接到它,隐藏了大量令人讨厌的布线和大惊小怪。 But is there a similar route for going the other way? 但另一种方式是否有类似的路线?

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. 我可以构建此服务的引用实现,从中创建WSDL,并自动生成客户端而不会出现问题。 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." 然而,似乎定义服务的唯一方法是基本上将某人指向我创建的WSDL并说“实现看起来像那样的东西”。 Seeing as how WSDLs are rarely designed to be read by human beings, this route seems less than attractive. 看到WSDL很少被设计为人类阅读,这条路线似乎不那么有吸引力。

So is there a way that I'm not aware of to generate a service interface from a WSDL or similar descriptor? 那么有一种我不知道从WSDL或类似描述符生成服务接口的方法吗? Primarily looking at .NET 3.5 here, using C# and WCF. 主要在这里查看.NET 3.5,使用C#和WCF。 Thanks! 谢谢!

是的 ,您绝对可以从WSDL文件创建WCF服务合同(接口):使用svcutil.exe

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. 如果您有一个描述客户端想要调用的方法的WSDL,还有一个XSD(XML Schema),它描述了它希望发送和接收的数据元素,您可以从中创建服务。 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). 这被称为构建WCF服务的“契约优先”方法,并且在具有互操作性要求的环境(如Java客户端和.NET服务,反之亦然)中相当普遍。

Using svcutil.exe , you can generate a service interface class - your service description. 使用svcutil.exe ,您可以生成服务接口类 - 您的服务描述。 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)

This will create a YourServiceInterface.cs (or YourServiceInterface.vb ) file, which is the basis for your server code. 这将创建一个YourServiceInterface.cs (或YourServiceInterface.vb )文件,该文件是服务器代码的基础。

From this, you can then create an actual service implementation - your service class that does the real work. 然后,您可以创建一个实际的服务实现 - 实现真正工作的服务类。

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

Marc

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

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