儘管我已指定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
正是有了Serialization.dll的版本問題。對困惑感到抱歉。感謝您的及時支持 – Lijo 2010-06-06 08:22:34