2016-04-09 79 views
0

我正在使用我們的票務系統OTRS中的SOAP web服務。所以Webservice並不是真的在我的控制之下。 該請求的工作很好,但我從來沒有得到我的代碼中的答案。答案始終爲空。 (var response = client.SessionCreate(session);) 奇怪的是,那個票務系統的wireshark和webservice控制檯說我應該收到一個有效的答案。 由於我對這個web服務的東西很新,所以我絕對不知道從哪裏開始在這種情況下。所以這裏是對我的描述。任何建議真的很感激。WCF Webservice總是返回null

首先,我創建了一個正常的C#項目,並添加了只能在OTRS項目的GitHub站點網站上找到的WSDL文件。我將它添加爲Service Reference,並添加了看起來像這樣的C#代碼。

//用於調試 System.Net.ServicePointManager.Expect100Continue = false; Thread.CurrentThread.CurrentUICulture = new CultureInfo(「en-us」);在服務器端

try 
{ 
    OTRS.OTRS_Error err = new OTRS.OTRS_Error(); 
    OTRS.GenericTicketConnector_PortTypeClient client = new OTRS.GenericTicketConnector_PortTypeClient("GenericTicketConnector_Port"); 

    OTRS.SessionCreate session = new OTRS.SessionCreate(); 
    session.Item = "someUserNameGoesHere"; 
    session.ItemElementName = OTRS.ItemChoiceType8.UserLogin; 
    session.Password = "SomePasswordGoesHere"; 

    var response = client.SessionCreate(session); 

    Console.WriteLine(response.SessionID); 
    Console.WriteLine(response.Error); 
} 
catch (Exception exep) 
{ 
    Console.WriteLine(exep.Message); 
    Console.WriteLine(exep.InnerException); 
} 
finally 
{ 
    Console.ReadLine(); 
} 

傳入消息

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing"> 
<s:Header> 
    <a:Action s:mustUnderstand="1">http://www.otrs.org/TicketConnector/SessionCreate</a:Action> 
    <a:MessageID>urn:uuid:14750529-3de2-4618-8db4-8ac18b681c18</a:MessageID> 
    <a:ReplyTo> 
     <a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address> 
    </a:ReplyTo> 
    <a:To s:mustUnderstand="1">http://SomeServer/otrs/nph-genericinterface.pl/Webservice/GenericTicketConnector</a:To> 
</s:Header> 
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <SessionCreate xmlns="http://www.otrs.org/TicketConnector/"> 
     <UserLogin xmlns="">someUserName</UserLogin> 
     <Password xmlns="">somePassword</Password> 
    </SessionCreate> 
</s:Body> 

在服務器端

傳出消息

<?xml version="1.0" encoding="UTF-8"?> 
<soap:Envelope soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding" 
xmlns:soap="http://www.w3.org/2003/05/soap-envelope" 
xmlns:soapenc="http://www.w3.org/2003/05/soap-encoding" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <soap:Body> 
     <SessionCreateResponse xmlns="http://www.otrs.org/TicketConnector/"> 
      <SessionID>SomeSessionID</SessionID> 
     </SessionCreateResponse> 
    </soap:Body> 
</soap:Envelope> 

Wireshark的HTTP/XML包從服務器將我的客戶

<?xml version="1.0" encoding="UTF-8"?> 
<soap:Envelope soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding" 
xmlns:soap="http://www.w3.org/2003/05/soap-envelope" 
xmlns:soapenc="http://www.w3.org/2003/05/soap-encoding" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <soap:Body> 
     <SessionCreateResponse xmlns="http://www.otrs.org/TicketConnector/"> 
      <SessionID>SomeSessionID</SessionID> 
     </SessionCreateResponse> 
    </soap:Body> 
</soap:Envelope> 

Refernce.cs https://gist.github.com/HansVader/1ba3847d918ee15ef16703c8ada6c9bf

WSDL https://gist.github.com/HansVader/dd849e49f4a1584397cd21b0e430b301

我目前只需要SessionnCreate和TicketUpdate功能。其他操作在這個時間點是無關緊要的。如果您需要其他信息,請告訴我。

更新:

下面是像yildizm85跟蹤工具的痕跡,意見建議: Outgoing SOAP message

Incoming SOAP message

我想這也是值得注意的是,我所創造的refernce.cs 我自己與svcutil工具,因爲我有一個IsWrapped的問題。看看這個問題,回答: XmlSerializer attribute not valid in Web Service using WCF

+0

您是否嘗試過WCF跟蹤? https://msdn.microsoft.com/en-us/library/ms751526.aspx –

+0

它看起來像你發送的消息是正確的,你收到的消息。我檢查了一下命名空間,看起來好像一見鍾情。但是,反序列化似乎並不奏效,所以可能是WSDL產生錯誤,或者WSDL錯誤。您是否嘗試多次添加服務引用(添加和刪除),或者在reference.cs中更改了任何內容? – Cosmin

+0

建議你看看ServiceStack.net - 非常棒的web服務庫。他們的下載頁面底部有一個較舊的開源版本。 –

回答

1

WSDL沒有定義將elementFormDefault:

<xsd:schema targetNamespace="http://www.otrs.org/TicketConnector/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 

由於不合格的是默認的,這使得每一個與表= System.Xml.Schema裝飾性能。 XmlSchemaForm.Unqualified在生成的參考文件中。

但是,由於爲每個複雜對象指定了默認名稱空間,因此XML序列化程序不會處理未限定的屬性。

溶液

更改架構元件:

<xsd:schema targetNamespace="http://www.otrs.org/TicketConnector/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> 

此外,對於某些屬性,該命令是錯誤的,所以它們需要被改變爲(例如TickerCreateResponse) :

<xsd:element name="TicketCreateResponse"> 
    <xsd:complexType> 
     <xsd:sequence> 
      <xsd:element maxOccurs="1" minOccurs="1" name="ArticleID" type="xsd:positiveInteger"/> 
      <xsd:element maxOccurs="1" minOccurs="1" name="TicketID" type="xsd:positiveInteger"/> 
      <xsd:element minOccurs="1" name="TicketNumber" type="xsd:string"/> 
      <xsd:element maxOccurs="1" minOccurs="0" name="Error" type="tns:OTRS_Error"/> 
     </xsd:sequence> 
    </xsd:complexType> 
</xsd:element> 

這些更改後,使用SvcUtil工具創建代理:

svcutil.exe的/串行器:XmlSerializer的GenericTicketConnectorSOAP.wsdl /o:OtrsConnector.cs /包裝/ N:*,OTRS

而且使用的配置:

<system.serviceModel> 
<bindings> 
    <customBinding> 
    <binding name="GenericTicketConnector_Binding"> 
     <textMessageEncoding messageVersion="Soap12" writeEncoding="utf-8" /> 
     <httpTransport /> 
    </binding> 
    </customBinding> 
</bindings> 
<client> 
    <endpoint address="http://localhost/otrs/nph-genericinterface.pl/Webservice/GenericTicketConnector" 
    binding="customBinding" bindingConfiguration="GenericTicketConnector_Binding" 
    contract="otrs.GenericTicketConnector_PortType" name="GenericTicketConnector_Port" /> 
</client> 
</system.serviceModel> 

應該後上班,至少它爲我做了。

希望它也適用於你!