WCF客户端不使用Web Service中的用户定义数据类型(包括SCVTrace)

时间:2013-11-25 21:34:00

标签: c# wpf web-services rest soap

根据要求 - 这是我从SVCTraceViewer.exe获取的跟踪

<E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"> <EventID>524340</EventID> <Type>3</Type><SubType Name="Error">0</SubType><Level>2</Level> <TimeCreated SystemTime="2013-11-26T01:35:30.8037467Z" /> <Source Name="System.ServiceModel" /><Correlation ActivityID="{6a4de494-31a3-49ab-935f-419911742e44}" /> <Execution ProcessName="WebDev.WebServer40" ProcessID="13564" ThreadID="13" /><Channel /><Computer>CONNOR-PC</Computer></System> <ApplicationData><TraceData><DataItem><TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Error"> <TraceIdentifier>http://msdn.microsoft.com/en-GB/library/System.ServiceModel.ServiceOperationExceptionOnReply.aspx</TraceIdentifier> <Description>Replying to an operation threw a exception.</Description><AppDomain>caf40de1-1-130299029702291231</AppDomain> <Source>System.ServiceModel.Dispatcher.ImmutableDispatchRuntime/11419366</Source><Exception> <ExceptionType>System.ObjectDisposedException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType> <Message>The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.</Message> <StackTrace> at System.Data.Objects.ObjectContext.EnsureConnection() at System.Data.Objects.ObjectQuery 1.GetResults(Nullable 1 forMergeOption) at System.Data.Objects.ObjectQuery 1.Execute(MergeOption mergeOption)        在System.Data.Objects.DataClasses.EntityCollection 1.Load(List 1集合,MergeOption mergeOption)        在System.Data.Objects.DataClasses.EntityCollection 1.Load(MergeOption mergeOption) at System.Data.Objects.DataClasses.RelatedEnd.Load() at System.Data.Objects.DataClasses.RelatedEnd.DeferredLoad() at System.Data.Objects.DataClasses.EntityCollection 1.get_Count()        在WriteArrayOfProjectToXml(XmlWriterDelegator,Object,XmlObjectSerializerWriteContext,CollectionDataContract)        在System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter,Object obj,XmlObjectSerializerWriteContext context)        在System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract,XmlWriterDelegator xmlWriter,Object obj,RuntimeTypeHandle declaredTypeHandle)        在System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract,XmlWriterDelegator xmlWriter,Object obj,RuntimeTypeHandle declaredTypeHandle)        在System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter,Object obj,Boolean isDeclaredType,Boolean writeXsiType,Int32 declaredTypeID,RuntimeTypeHandle declaredTypeHandle)        在WriteUserToXml(XmlWriterDelegator,Object,XmlObjectSerializerWriteContext,ClassDataContract)        在System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter,Object obj,XmlObjectSerializerWriteContext context)        在System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract,XmlWriterDelegator xmlWriter,Object obj,RuntimeTypeHandle declaredTypeHandle)        在System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract,XmlWriterDelegator xmlWriter,Object obj,RuntimeTypeHandle declaredTypeHandle)        在System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter,Object obj,Boolean isDeclaredType,Boolean writeXsiType,Int32 declaredTypeID,RuntimeTypeHandle declaredTypeHandle)        在WriteArrayOfUserToXml(XmlWriterDelegator,Object,XmlObjectSerializerWriteContext,CollectionDataContract)        在System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter,Object obj,XmlObjectSerializerWriteContext context)        在System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract,XmlWriterDelegator xmlWriter,Object obj,RuntimeTypeHandle declaredTypeHandle)        在System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract,XmlWriterDelegator xmlWriter,Object obj,RuntimeTypeHandle declaredTypeHandle)        在System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer,Object graph,DataContractResolver dataContractResolver)        在System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer,Object graph,DataContractResolver dataContractResolver)        在System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer,Object graph,DataContractResolver dataContractResolver)        在System.Runtime.Serialization.XmlObjectSerializer.WriteObject(XmlDictionaryWriter writer,Object graph)        在System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameterPart(XmlDictionaryWriter writer,PartInfo part,Object graph)        在System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameter(XmlDictionaryWriter writer,PartInfo part,Object graph)        在System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeBody(XmlDictionaryWriter writer,MessageVersion version,String action,MessageDescription messageDescription,Object returnValue,Object [] parameters,Boolean isRequest)        在System.ServiceModel.Dispatcher.OperationFormatter.SerializeBodyContents(XmlDictionaryWriter writer,MessageVersion version,Object [] parameters,Object returnValue,Boolean isRequest)        在System.ServiceModel.Dispatcher.OperationFormatter.OperationFormatterMessage.OperationFormatterBodyWriter.OnWriteBodyContents(XmlDictionaryWriter writer)        在System.ServiceModel.Channels.BodyWriterMessage.OnWriteBodyContents(XmlDictionaryWriter writer)        在System.ServiceModel.Channels.Message.OnWriteMessage(XmlDictionaryWriter writer)        在System.ServiceModel.Channels.BufferedMessageWriter.WriteMessage(消息消息,BufferManager bufferManager,Int32 initialOffset,Int32 maxSizeQuota)        at System.ServiceModel.Channels.TextMessageEncoderFactory.TextMessageEncoder.WriteMessage(消息消息,Int32 maxMessageSize,BufferManager bufferManager,Int32 messageOffset)        在System.ServiceModel.Channels.HttpOutput.SerializeBufferedMessage(消息消息)        在System.ServiceModel.Channels.HttpOutput.Send(TimeSpan超时)        在System.ServiceModel.Channels.HttpPipeline.EmptyHttpPipeline.SendReplyCore(消息消息,TimeSpan超时)        在System.ServiceModel.Channels.HttpPipeline.EmptyHttpPipeline.SendReply(消息消息,TimeSpan超时)        在System.ServiceModel.Channels.HttpRequestContext.OnReply(消息消息,TimeSpan超时)        在System.ServiceModel.Activation.HostedHttpContext.OnReply(消息消息,TimeSpan超时)        在System.ServiceModel.Channels.RequestContextBase.Reply(消息消息,TimeSpan超时)        在System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.Reply(MessageRpc&amp; amp; rpc)System.ObjectDisposedException:ObjectContext实例已被释放,不能再用于需要连接的操作。        在System.Data.Objects.ObjectContext.EnsureConnection()        在System.Data.Objects.ObjectQuery 1.GetResults(Nullable 1 forMergeOption)        在System.Data.Objects.ObjectQuery 1.Execute(MergeOption mergeOption) at System.Data.Objects.DataClasses.EntityCollection 1.Load(List 1 collection, MergeOption mergeOption) at System.Data.Objects.DataClasses.EntityCollection 1.Load(MergeOption mergeOption)        在System.Data.Objects.DataClasses.RelatedEnd.Load()        在System.Data.Objects.DataClasses.RelatedEnd.DeferredLoad()        在System.Data.Objects.DataClasses.EntityCollection 1.get_Count() at WriteArrayOfProjectToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , CollectionDataContract ) at System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle) at WriteUserToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , ClassDataContract ) at System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle) at WriteArrayOfUserToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , CollectionDataContract ) at System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle) at System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver) at System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver) at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver) at System.Runtime.Serialization.XmlObjectSerializer.WriteObject(XmlDictionaryWriter writer, Object graph) at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameterPart(XmlDictionaryWriter writer, PartInfo part, Object graph) at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameter(XmlDictionaryWriter writer, PartInfo part, Object graph) at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeBody(XmlDictionaryWriter writer, MessageVersion version, String action, MessageDescription messageDescription, Object returnValue, Object[] parameters, Boolean isRequest) at System.ServiceModel.Dispatcher.OperationFormatter.SerializeBodyContents(XmlDictionaryWriter writer, MessageVersion version, Object[] parameters, Object returnValue, Boolean isRequest) at System.ServiceModel.Dispatcher.OperationFormatter.OperationFormatterMessage.OperationFormatterBodyWriter.OnWriteBodyContents(XmlDictionaryWriter writer) at System.ServiceModel.Channels.BodyWriterMessage.OnWriteBodyContents(XmlDictionaryWriter writer) at System.ServiceModel.Channels.Message.OnWriteMessage(XmlDictionaryWriter writer) at System.ServiceModel.Channels.BufferedMessageWriter.WriteMessage(Message message, BufferManager bufferManager, Int32 initialOffset, Int32 maxSizeQuota) at System.ServiceModel.Channels.TextMessageEncoderFactory.TextMessageEncoder.WriteMessage(Message message, Int32 maxMessageSize, BufferManager bufferManager, Int32 messageOffset) at System.ServiceModel.Channels.HttpOutput.SerializeBufferedMessage(Message message) at System.ServiceModel.Channels.HttpOutput.Send(TimeSpan timeout) at System.ServiceModel.Channels.HttpPipeline.EmptyHttpPipeline.SendReplyCore(Message message, TimeSpan timeout) at System.ServiceModel.Channels.HttpPipeline.EmptyHttpPipeline.SendReply(Message message, TimeSpan timeout) at System.ServiceModel.Channels.HttpRequestContext.OnReply(Message message, TimeSpan timeout) at System.ServiceModel.Activation.HostedHttpContext.OnReply(Message message, TimeSpan timeout) at System.ServiceModel.Channels.RequestContextBase.Reply(Message message, TimeSpan timeout) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.Reply(MessageRpc&amp;amp; rpc)</ExceptionString></Exception></TraceRecord></DataItem></TraceData></ApplicationData></E2ETraceEvent>

将自动生成的实体类从我的Web服务返回到客户端时收到以下CommunicationException错误

导入数据库模型时会自动生成User类,但在调用以下方法时,它不会通过Web服务传递给客户端。

  using (UserService1Client db = new UserService1Client()) {

             User[] newarray = db.userSearch(this.Model.Forename, 
                   this.Model.Surname, 
                   this.Model.Certifications, this.Model.UserTypes);
         kTrace); 
            }

这是我的堆栈跟踪..

A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in mscorlib.dll
There was a communication problem. The underlying connection was closed: The connection was closed unexpectedly.
Server stack trace: 
   at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
   at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
   at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at SimpleMvvmWpf1.Local_DatabaseServiceCS4.IUserService1.userSearch(String Forename, String Surname, String Certifications, String UserTypes)
   at SimpleMvvmWpf1.Local_DatabaseServiceCS4.UserService1Client.userSearch(String Forename, String Surname, String Certifications, String UserTypes) in i:\TRUNK\SimpleMvvmWpf1\SimpleMvvmWpf1\Service References\Local_DatabaseServiceCS4\Reference.cs:line 1613
   at SimpleMvvmWpf1.SearchViewModel.SubmitSearch() in i:\TRUNK\SimpleMvvmWpf1\SimpleMvvmWpf1\ViewModels\SearchViewModel.cs:line 51

我的方法..

[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Xml, UriTemplate = "GetUsers", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
List<User> GetUsers();


       public List<UserProfile> GetUsers()
    {
        using (cs4_databaseEntities entities = new cs4_databaseEntities())
        {
            try
            {
                var queryResult = from i in entities.Users select i;
                List<User> uList= queryResult.ToList();
                return uList; 
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return new List<UserProfile>();
            }
        }
    }

User Class有[DataContractAttribute(IsReference = true)],变量有[DataMemberAttribute()]但是我还没有得到任何东西,我也玩过绑定配置设置closeTimeout="00:40:00" openTimeout="00:40:00" receiveTimeout="00:40:00" sendTimeout="00:40:00" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647"

任何帮助都会很棒,这让我的大脑充满了感情!

1 个答案:

答案 0 :(得分:1)

您的代码正在使用所谓的Lazy Loading,简而言之,这意味着数据实际上只能按需读取。这将在响应序列化期间发生(因为它显示在堆栈跟踪中),在DbContext(在这种情况下为cs4_databaseEntities)处理之后很久 - 因为它仅存在于using块内。最简单的解决方案是禁用延迟加载:

yourContext.ContextOptions.LazyLoadingEnabled = false;

请参阅有关此主题的示例this good article