2010-06-05 40 views
1

儘管我已指定Datacontract和Datamember,但仍會收到以下異常事件。你能幫我理解這個問題是什麼嗎?即使在提供DataContract和DataMember後,WCF - 序列化異常

「類型'MyServiceLibrary.CompanyLogo'不能被序列化,考慮使用DataContractAttribute屬性標記它,並用DataMemberAttribute屬性標記其想要序列化的所有成員。

注意:運行服務主機時發生異常。我甚至沒有創建客戶端。

using System.ServiceModel; 
using System.ServiceModel.Dispatcher; 
using System.ServiceModel.Channels; 
using System.ServiceModel.Description; 
using System.Runtime.Serialization; 
using MyServiceLibrary; 

namespace MySelfHostConsoleApp 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      System.ServiceModel.ServiceHost myHost = new ServiceHost(typeof(NameDecorator)); 
      myHost.Open(); 
      Console.ReadLine(); 
     } 
    } 

} 

//The Service is 

using System.ServiceModel; 
using System.Runtime.Serialization; 

namespace MyServiceLibrary 
{ 
    [ServiceContract(Namespace = "http://Lijo.Samples")] 
    public interface IElementaryService 
    { 
     [OperationContract] 
     CompanyLogo GetLogo(); 
    } 

    public class NameDecorator : IElementaryService 
    { 
     public CompanyLogo GetLogo() 
     { 
      Shape cirlce = new Shape(); 
      CompanyLogo logo = new CompanyLogo(cirlce); 
      return logo; 
     } 
    } 

    [DataContract] 
    public class Shape 
    { 
     public string SelfExplain() 
     { 
      return "sample"; 
     } 

    } 

    [DataContract] 
    public class CompanyLogo 
    { 
     private Shape m_shapeOfLogo; 

     [DataMember] 
     public Shape ShapeOfLogo 
     { 
      get 
      { 
       return m_shapeOfLogo; 
      } 
      set 
      { 
       m_shapeOfLogo = value; 
      } 
     } 

     public CompanyLogo(Shape shape) 
     { 
      m_shapeOfLogo = shape; 
     } 
     public CompanyLogo() 
     { 

     } 
    } 

} 

//和主機配置是

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 

    <system.serviceModel> 
    <services> 

     <service name="MyServiceLibrary.NameDecorator" 
       behaviorConfiguration="WeatherServiceBehavior"> 

     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:8017/ServiceModelSamples/FreeServiceWorld"/> 
      </baseAddresses> 
     </host> 

     <endpoint address="" 
        binding="basicHttpBinding" 
        contract="MyServiceLibrary.IElementaryService" /> 
     <endpoint address="mex" 
        binding="mexHttpBinding" 
        contract="IMetadataExchange" /> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="WeatherServiceBehavior"> 
      <serviceMetadata httpGetEnabled="true"/> 
      <serviceDebug includeExceptionDetailInFaults="False"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 
</configuration> 

感謝

Lijo

回答

1

CompanyLogo類必須有一個默認參數的構造函數,否則就不能去系列化。

編輯:創建一個新項目,複製粘貼你的代碼,一切似乎工作正常。確保您的服務庫被正確引用,並且不使用沒有該屬性的舊版本。

1

似乎無法證實這些問題 - 就像我的機器上的魅力一樣。我可以讓服務主機正常運行,沒問題。我可以從WCF服務測試客戶端連接到它,並請求如下所示:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
    <s:Header> 
    <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://Lijo.Samples/IElementaryService/GetLogo</Action> 
    </s:Header> 
    <s:Body> 
    <GetLogo xmlns="http://Lijo.Samples" /> 
    </s:Body> 
</s:Envelope> 

和反應是這樣的:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
    <s:Header /> 
    <s:Body> 
    <GetLogoResponse xmlns="http://Lijo.Samples"> 
     <GetLogoResult xmlns:a="http://schemas.datacontract.org/2004/07/SerializeLogo" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
     <a:ShapeOfLogo> 
      <a:ShapeName i:nil="true" /> 
     </a:ShapeOfLogo> 
     </GetLogoResult> 
    </GetLogoResponse> 
    </s:Body> 
</s:Envelope> 

從我的觀點:這項服務的工作(不做很多 - 但它的工作原理)。

雙和三重檢查所有的參數 - 這是必須關閉莫名其妙....

+0

正是有了Serialization.dll的版本問題。對困惑感到抱歉。感謝您的及時支持 – Lijo 2010-06-06 08:22:34