简体   繁体   English

实施SOAP Web服务

[英]Implementing a SOAP web service

I have a WSDL definition for a SOAP service and I have successfully generated *.cs file from it using SvcUtil. 我具有SOAP服务的WSDL定义,并且已经使用SvcUtil从中成功生成* .cs文件。

Implementing client is quite straightforward - I just need to call the necessary functions from the generated *.cs and that's it. 实现客户端非常简单-我只需要从生成的* .cs中调用必要的函数即可。

Implementing server seems more complicated. 实施服务器似乎更加复杂。 As I understand I need to implement an interface from the generated *.cs and then use some magic to turn it into the web server. 据我了解,我需要从生成的* .cs实现一个接口,然后使用一些魔术方法将其转换为Web服务器。

But I don't need a new web server, I already have a web server written in C# which already has many functionality unrelated to the SOAP service that I need to implement. 但是我不需要新的Web服务器,我已经有一个用C#编写的Web服务器,该服务器已经具有许多与我需要实现的SOAP服务无关的功能。 I don't want to create another web server but I want my SOAP service to be just a part of my existing application (server), that is my server can answer eg requests http://example.com/request1 , http://example.com/request2 etc. and I want this SOAP service to be just http://example.com/request3 . 我不想再创建一个Web服务器,但我希望我的SOAP服务只是一个我现有的应用程序(服务器)的一部分,这是我的服务器可以回答如请求http://example.com/request1http://example.com/request2等。我希望此SOAP服务只是http://example.com/request3

Since HTTP is already handled by my server I don't need .NET to handle it for me, basically my server can accept client connections and call the necessary handler based on the URL. 由于HTTP已经由我的服务器处理过,因此我不需要.NET来为我处理它,基本上我的服务器可以接受客户端连接并根据URL调用必要的处理程序。 I have a handler for SOAP request which looks approximately like this: 我有一个用于SOAP请求的处理程序,看起来像这样:

MyResponse HandleSOAPRequest(MyRequest request)
{
    // 1. parse soap message from request.body
    // 2. process it
    // 3. generate response, serialize it in SOAP format and return it
}

The question is - can I rely on WSDL definition and .NET libraries to do it? 问题是-我可以依靠WSDL定义和.NET库吗?

Currently I'm parsing SOAP request using XDocument and manually extract fields from it and serialize using simple string concatenation. 目前,我正在使用XDocument解析SOAP请求,并从中手动提取字段并使用简单的字符串连接进行序列化。 Using .NET built-in functions to serialize or parse XML doesn't work. 使用.NET内置函数来序列化或解析XML是行不通的。 That is if I try to serialize response from an object of the class defined in the generated *.cs file then produced XML is different from what is expected by the protocol, similarly, if I try to parse request as an object of the class defined in the generated *.cs file I get error because XML parser expects different format. 也就是说,如果我尝试序列化生成的* .cs文件中定义的类的对象的响应,则生成的XML与协议期望的不同,类似地,如果我尝试将请求解析为定义的类的对象在生成的* .cs文件中,我得到了错误,因为XML解析器期望使用不同的格式。 This applies to both the SoapFormatter and XmlSerializer. 这适用于SoapFormatter和XmlSerializer。

Since .NET can implement client this means that everything that is necessary to parse and serialize SOAP messages is already implemented, I just need to figure out a way how to use this functionality. 由于.NET可以实现客户端,这意味着解析和序列化SOAP消息所需的一切都已实现,因此我只需要找出一种使用此功能的方法即可。

The documentation for ServiceModel wasn't very helpful. ServiceModel的文档不是很有帮助。

The easiest way would be to start the service via the ServiceHost : 最简单的方法是通过ServiceHost启动服务:

ServiceHost host = new ServiceHost(typeof(YourService));
host.Open();

(I assumed here the configuration will come from the app.config and will not be setup in code like in the linked example below.) (我假设这里的配置来自app.config ,不会像下面的链接示例中那样在代码中进行设置。)

How to: Expose a Contract to SOAP and Web Clients 如何:将合同公开给SOAP和Web客户端

The only drawback of this is that the application has to run with admin rights or otherwise a weird cofiguration is necessary. 这样做的唯一缺点是应用程序必须以管理员权限运行,否则必须进行奇怪的配置。

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

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