如何解析传递给我的wcf服务操作的soap + xml消息?

时间:2011-05-20 16:56:27

标签: 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

}

1 个答案:

答案 0 :(得分:3)

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

根据您是否使用包装/解包邮件,可能存在差异,但基本方案,对于您描述的来自客户端的soap消息,您的服务接口将如下(假设您的响应为{{1} }}):

string

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

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

并且您在服务端的[ServiceContract] public interface IMyService { [OperationContract] public bool CheckCustomerExists(Customer Customer); } 课程需要定义为Customer

DataContract

这会使soap请求看起来如下:

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