c# - 如何解析传递到我的 wcf 服务操作中的soap+xml 消息?

标签 c# wcf soap

我从客户端收到以下 Soap 请求,基本上我必须提取名称,然后发回“Hello Test”

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"   
xmlns:ns1="http://tempuri.org/">
<SOAP-ENV:Body>
    <ns1:Customer>
    <ns1:Name>Test</ns1:Name>
    </ns1:Customer>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

如果我有一个客户定义的类,如下所示:

public class Customer
{
    public string Name {get;set;}
}

我不确定如何将soap请求传递到我的wcf服务操作,该操作将接收从xsd生成的客户请求对象?

在我的wcf服务操作收到soap请求后,我不确定如何从中获取Name属性并将响应发送回客户端,例如“Hello Test”

注意:客户端不会发送 Customer 对象,他们将发送 xml 请求,我必须将其解析为 Customer 对象。我希望这能解决问题。

我是否必须执行类似的操作,将 XDocument 传递给我的 wcf 服务操作:

private static void ParsSoapDocument(XDocument soapDocument)
{
   //Parse XDocument for elements/attributes

}

最佳答案

您不必解析任何内容,这就是 WCF 为您处理的内容。

根据您是否使用包装/未包装消息,可能会有所不同,但基本情况是,对于您描述的来自客户端的 SOAP 消息,您的服务接口(interface)将如下所示(假设您的响应是 字符串):

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    public string Customer(string Name);
}

更有可能的是,您实际上正在尝试执行一项接收客户的操作。例如,要检查客户是否存在,您可能需要:

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    public bool CheckCustomerExists(Customer Customer);
}

并且服务端的 Customer 类需要定义为 DataContract:

[DataContract]
public class Customer
{
    public string Name{get;set;}
}

这将使 SOAP 请求如下所示:

<?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/"> <SOAP-ENV:Body>
    <ns1:CheckCustomerExists>
    <ns1:Customer>
    <ns1:Name>Test</ns1:Name>
    </ns1:Customer>
    </ns1:CheckCustomerExists> </SOAP-ENV:Body> </SOAP-ENV:Envelope>

关于c# - 如何解析传递到我的 wcf 服务操作中的soap+xml 消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6075232/

相关文章:

c# - 当它只有一个 App.config 时将 WCF 服务部署到 IIS

wcf - 什么是默认的 WCF 绑定(bind)?

javascript - Node-soap 中的数组

c# - 异步 asp.net 表

c# - form1.designer.cs 和 form1.resx 的目的

c# - 重命名服务引用的一部分

soap - 如果我更改 WSDL 的 targetNamespace,是否会破坏现有的使用者?

用于从 wsdl 生成 SOAP 请求的 Java 类

c# - 需要从空格分隔的字符串中的特定位置删除特定引号,同时保留其他引号

c# - 如何传递 WebAPI Controller 数据,以便发布的模型可以在绑定(bind)/反序列化或验证时访问