当前位置:  开发笔记 > 运维 > 正文

为什么我从WSDL调用返回System.InvalidOperationException但不是从同一个调用到另一个服务?

如何解决《为什么我从WSDL调用返回System.InvalidOperationException但不是从同一个调用到另一个服务?》经验,为你挑选了1个好方法。

我已经创建了服务来获取各种客户的国家详细信息,但在托管服务时我得到了这个例外.我正在使用基本的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 GetCountry()
        { return objDAL.GetCountry(); }

        public List GetState(int CountryId)
        { return objDAL.GetState(CountryId); }

        public List GetCity(int StateId)
        { return objDAL.GetCity(StateId); }
    }
}

为什么这与另一个WCF服务正常工作,但是给我一个错误的另一个服务?



1> marc_s..:

嗯,错误很清楚:

System.Runtime.Serialization.InvalidDataContractException:无法序列化类型'Pariwaar.BusinessObject.CountryBO' .请考虑使用DataContractAttribute 属性对其进行标记,并使用DataMemberAttribute属性标记要序列化的所有成员.有关其他受支持的类型,请参阅Microsoft .NET Framework文档.

您显然使用数据类型Pariwaar.BusinessObject.CountryBO作为您的某个WCF服务方法的参数或返回值,但该类没有[DataContract]属性.

请参阅有关使用数据协定的MSDN文档,以了解数据协定以及如何使您的对象可由WCF使用.请参阅此博客文章 - WCF基础:数据合同,了解同一主题的另一个视图.

所有复杂类型(例如类)都应使用[DataContract]标记,所有要包含在[DataMember]的WCF消息中的字段:

[DataContract]
class Pariwaar.BusinessObject.CountryBO
{
   [DataMember]
   string CountryName { get; set; }

   [DataMember]
   string CountryCurrency { get; set; }

   [DataMember]
   string CountryISOCode { get; set; }

   ........
}

推荐阅读
手机用户2402852387
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有