2011-05-20 23 views
2

我有以下的SOAP請求我從客戶端,在那裏基本上我要提取的名稱,然後發回的「Hello測試」如何解析傳入我的wcf服務操作的soap + xml消息?

<?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測試」

注意:客戶端不會發送一個Customer對象,它們將發送一個xml請求,並且我必須將它解析爲一個Customer對象。我希望這可以清除事情。

我必須做這樣的事情,我在的XDocument傳遞給我WCF服務操作:

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

} 

回答

3

你不應該有任何分析,這是WCF爲你處理。

根據您是否使用包裝/解包消息,但基本情況,您描述的肥皂消息來自客戶端,您的服務接口如下所示(假設您的響應是string) :

[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> 
+0

Ethan,謝謝你。我有客戶向我發送請求,所以我不控制請求,我只是控制我收到請求時的操作,如將客戶添加到數據庫中。你知道我如何查看.net中的肥皂請求/響應(例如 - 使用Console.WriteLine(...)) – Xaisoft 2011-05-20 19:12:14

+1

-1:爲什麼使用'MessageContract'? – 2011-05-20 20:09:50

+0

@john我提到在兩個地方使用'MessageContract',所以不完全確定你問的是哪一個。首先,我提到如果操作使用Customer對象而不是簡單的數據類型,則需要使用'MessageContract'。我想你也可以使用'DataContract',但是我通常使用消息(到達第二個'MessageContract'引用)。其次,我建議使用請求/響應,如果操作可以控制消息的設計以及消息是否相當複雜。 – 2011-05-20 20:32:17