我已创建服务以获取各种客户的国家/地区详细信息,但在托管服务时,我收到此异常。我正在使用基本的http绑定。
An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is:
System.InvalidOperationException: An exception was thrown in a call to a
WSDL export extension:
System.ServiceModel.Description.DataContractSerializerOperationBehavior
contract: http://tempuri.org/:IReferenceDataService ---->
System.Runtime.Serialization.InvalidDataContractException:
Type 'Pariwaar.BusinessObject.CountryBO' cannot be serialized.
Consider marking it with the DataContractAttribute attribute,
and marking all of its members you want serialized with the
DataMemberAttribute attribute. See the Microsoft .NET Framework
documentation for other supported types.
at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.ThrowInvalidDataContractException(String message, Type type)
at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.CreateDataContract(Int32 id, RuntimeTypeHandle typeHandle, Type type)
at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.GetDataContractSkipValidation(Int32 id, RuntimeTypeHandle typeHandle, Type type)
at System.Runtime.Serialization.DataContract.GetDataContract(RuntimeTypeHandle typeHandle, Type type, SerializationMode mode)
at System.Runtime.Serialization.DataContractSet.GetDataContract(Type clrType)
at System.Runtime.Serialization.DataContractSet.GetItemTypeDataContract(CollectionDataContract collectionContract)
at System.Runtime.Serialization.DataContractSet.AddCollectionDataContract(CollectionDataContract collectionDataContract)
at System.Runtime.Serialization.DataContractSet.Add(XmlQualifiedName name, DataContract dataContract)
at System.Runtime.Serialization.DataContractSet.Add(Type type)
at System.Runtime.Serialization.XsdDataContractExporter.Export(Type type)
at System.ServiceModel.Description.MessageContractExporter.ExportType(Type type, String partName, String operationName, XmlSchemaType& xsdType)
at System.ServiceModel.Description.DataContractSerializerMessageContractExporter.ExportBody(Int32 messageIndex, Object state)
at System.ServiceModel.Description.MessageContractExporter.ExportMessage(Int32 messageIndex, Object state)
at System.ServiceModel.Description.MessageContractExporter.ExportMessageContract()
at System.ServiceModel.Description.DataContractSerializerOperationBehavior.System.ServiceModel.Description.IWsdlExportExtension.ExportContract(WsdlExporter exporter, WsdlContractConversionContext contractContext)
at System.ServiceModel.Description.WsdlExporter.CallExtension(WsdlContractConversionContext contractContext, IWsdlExportExtension extension)
--- End of inner ExceptionDetail stack trace ---
at System.ServiceModel.Description.WsdlExporter.CallExtension(WsdlContractConversionContext contractContext, IWsdlExportExtension extension)
at System.ServiceModel.Description.WsdlExporter.CallExportContract(WsdlContractConversionContext contractContext)
at System.ServiceModel.Description.WsdlExporter.ExportContract(ContractDescription contract)
at System.ServiceModel.Description.WsdlExporter.ExportEndpoint(ServiceEndpoint endpoint, XmlQualifiedName wsdlServiceQName)
at System.ServiceModel.Description.WsdlExporter.ExportEndpoints(IEnumerable`1 endpoints, XmlQualifiedName wsdlServiceQName)
at System.ServiceModel.Description.ServiceMetadataBehavior.MetadataExtensionInitializer.GenerateMetadata()
at System.ServiceModel.Description.ServiceMetadataExtension.EnsureInitialized()
at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.InitializationData.InitializeFrom(ServiceMetadataExtension extension)
at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.GetInitData()
at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.TryHandleDocumentationRequest(Message httpGetRequest, String[] queries, Message& replyMessage)
at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.ProcessHttpRequest(Message httpGetRequest)
at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.Get(Message message)
at SyncInvokeGet(Object , Object[] , Object[] )
at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
我在另一个项目中有businessobject[datamember]
。我在这里提到了它的dll。
您可以查看我的课程
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Pariwaar.BusinessObject;
using Pariwaar.DataAccessLayer;
namespace Pariwaar.WCFServices.SVC
{
// NOTE: If you change the class name "ReferenceDataService" here, you must also update the reference to "ReferenceDataService" in Web.config.
public class ReferenceDataService : IReferenceDataService
{
ReferenceData objDAL = new ReferenceData();
public List<CountryBO> GetCountry()
{ return objDAL.GetCountry(); }
public List<StateBO> GetState(int CountryId)
{ return objDAL.GetState(CountryId); }
public List<CityBO> GetCity(int StateId)
{ return objDAL.GetCity(StateId); }
}
}
为什么这可以正常使用另一个WCF服务,但是给我一个其他服务的错误?
答案 0 :(得分:7)
嗯,错误很明显:
System.Runtime.Serialization.InvalidDataContractException: 类型 'Pariwaar.BusinessObject.CountryBO' 无法序列化。考虑标记 它与 DataContractAttribute 属性,并标记其所有 你想用序列化的成员 DataMemberAttribute属性。看到 Microsoft .NET Framework 其他支持的文档 类型。
您显然使用数据类型Pariwaar.BusinessObject.CountryBO
作为其中一个WCF服务方法的参数或返回值,但该类没有[DataContract]
属性。
请参阅Using Data Contracts上的MSDN文档,了解数据合同以及如何使您的对象可供WCF使用。有关同一主题的其他观点,请参阅此blog post - WCF Basics: Data Contracts。
所有复杂类型(例如类)都应标有[DataContract],所有要包含在[DataMember]的WCF消息中的字段:
[DataContract]
class Pariwaar.BusinessObject.CountryBO
{
[DataMember]
string CountryName { get; set; }
[DataMember]
string CountryCurrency { get; set; }
[DataMember]
string CountryISOCode { get; set; }
........
}
马克
答案 1 :(得分:4)
您好我遇到了同样的问题,原因是对象CountryBO上没有默认构造函数。
答案 2 :(得分:0)
如果我们错误地使用了未保留合同且可能已在List&lt;&gt;中使用的类型,则可能会发生此错误。或另一个DataContract的公共变量
HydTechie