WCF-即使在提供DataContract和DataMember之后也出现序列化异常
即使我已经指定了Datacontract和Datamember,我仍会收到以下异常。你能帮我了解一下问题是什么吗WCF-即使在提供DataContract和DataMember之后也出现序列化异常,wcf,Wcf,即使我已经指定了Datacontract和Datamember,我仍会收到以下异常。你能帮我了解一下问题是什么吗 “类型”MyService语言。公司徽标不能序列化。考虑使用DATACONTractAttor属性标记它,并标记要用DATAMEMBAREATION属性序列化的所有成员。“” 注意:运行服务主机时会发生异常。我甚至还没有创建一个客户端 using System.ServiceModel; using System.ServiceModel.Dispatcher; using Syst
“类型”MyService语言。公司徽标不能序列化。考虑使用DATACONTractAttor属性标记它,并标记要用DATAMEMBAREATION属性序列化的所有成员。“
” 注意:运行服务主机时会发生异常。我甚至还没有创建一个客户端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您的
CompanyLogo
类必须具有默认的无参数构造函数,否则无法取消序列化
编辑:创建了一个新项目,复制粘贴了代码,一切似乎都很正常。请确保您的服务库被正确引用,并且没有使用没有该属性的旧版本。似乎无法确认这些问题-在我的机器上效果很好。我可以让服务主机启动并运行,没有问题。我可以从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>
http://Lijo.Samples/IElementaryService/GetLogo
对此的回应是:
<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>
从我的观点来看:这项服务很有效(虽然做的不多,但很有效)
双重和三重检查您的所有参数-某些参数必须以某种方式关闭…Serialization.dll的版本有问题。很抱歉给你带来了困惑。感谢您的及时支持