2010-01-18 120 views
1

我有一個對象通過另一個系統使用的Web服務公開。該系統也使用相同的WSDL將需求返回給我們。我想知道是否有可能採取同一個對象並將其傳回原始對象?翻譯服務調用對象返回到原始對象

我試圖做到以下幾點,它實際上不會將對象填充回來......任何幫助都會很棒!

謝謝!

注意下面列出的方法的代碼是基於關閉的http://www.dotnetjohn.com/articles.aspx?articleid=173

public void ConvertBack() 
    { 
     ThirdParty.Animal animal; 
     using (var svc = new ThirdPartySoapClient()) 
      thirdPartyAnimal = svc.GetAnimal("Identifier"); 

     var xml = SerializeObject(thirdPartyAnimal, typeof(ThirdParty.Animal)); 

     var originalAnimal = (OriginalNamespace.Animal)DeserializeObject(xml, typeof(OrginalNamespace.Animal)); 

     Assert.AreEqual(originalAnimal.Name, animal.Name); 
    } 


    /// <summary> 
    /// Method to convert a custom Object to XML string 
    /// </summary> 
    /// <param name="pObject">Object that is to be serialized to XML</param> 
    /// <param name="typeOfObject">typeof() object that is being passed to be serialized to XML</param> 
    /// <returns>XML string</returns> 
    public string SerializeObject(object pObject, Type typeOfObject) 
    { 
     try 
     { 
      var memoryStream = new MemoryStream(); 
      var xs = new XmlSerializer(typeOfObject); 
      var xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8); 

      xs.Serialize(xmlTextWriter, pObject); 
      memoryStream = (MemoryStream) xmlTextWriter.BaseStream; 
      var xmlizedString = UTF8ByteArrayToString(memoryStream.ToArray()); 
      return xmlizedString; 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e); 
      return null; 
     } 
    } 

    /// <summary> 
    /// Method to reconstruct an Object from XML string 
    /// </summary> 
    /// <param name="pXmlizedString">XML To Be Converted to an Object</param> 
    /// <param name="typeOfObject">typeof() object that is being passed to be serialized to XML</param> 
    /// <returns></returns> 
    public object DeserializeObject(string pXmlizedString, Type typeOfObject) 
    { 
     var xs = new XmlSerializer(typeOfObject); 
     var memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString)); 
     new XmlTextWriter(memoryStream, Encoding.UTF8); 
     return xs.Deserialize(memoryStream); 
    } 

    /// <summary> 
    /// To convert a Byte Array of Unicode values (UTF-8 encoded) to a complete String. 
    /// </summary> 
    /// <param name="characters">Unicode Byte Array to be converted to String</param> 
    /// <returns>String converted from Unicode Byte Array</returns> 
    private static string UTF8ByteArrayToString(Byte[] characters) 
    { 
     var encoding = new UTF8Encoding(); 
     var constructedString = encoding.GetString(characters); 
     return (constructedString); 
    } 

    /// <summary> 
    /// Converts the String to UTF8 Byte array and is used in De serialization 
    /// </summary> 
    /// <param name="pXmlString"></param> 
    /// <returns></returns> 
    private static Byte[] StringToUTF8ByteArray(string pXmlString) 
    { 
     var encoding = new UTF8Encoding(); 
     var byteArray = encoding.GetBytes(pXmlString); 
     return byteArray; 
    } 

回答

0

在生成從WSDL代理有一個選項叫做"/sharetypes"應該解決這個問題

你可以用它從命令行工具或在「添加Web服務」對話框的選項區域

+0

謝謝,但我實際上正在尋找的是WSDL類型並將其帶回原始類型。假設我有EntityObject.Name.cs,Services.WebService1.asmx - 它有一個名爲GetName的方法,返回一個NameObject,但當然是以WSDL形式,BusinessLayer - 有一個方法接受EnityObject.Name在第四個項目中,您有一個參考服務,業務和實體你想調用服務來獲得名稱對象,並將其低音給業務,這將如何完成。對不起,我最初的問題有任何含糊之處,並感謝您的幫助! – Nic 2010-02-05 12:15:19