如何通过WCF SOAP服务访问EF导航属性?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何通过WCF SOAP服务访问EF导航属性?相关的知识,希望对你有一定的参考价值。

我正在为此WCF错误挣扎一段时间,没有任何运气。基本上,我想绑定[[通过WCF服务获取具有导航属性和已连接对象的实体Poco。我的EF v6代码成功地从数据库中获取了所有相关实体的Poco

Poco debug view

但是当我尝试

通过WCF服务访问此实体

时,我看到以下错误-[收到对http://localhost:8734/Design_Time_Addresses/BusinessLogicServicesLayer/userServices/的HTTP响应时发生错误。这可能是由于服务端点绑定未使用HTTP协议。这也可能是由于服务器终止了HTTP请求上下文(可能是由于服务关闭了)。有关更多详细信息,请参见服务器日志。服务器堆栈跟踪:在System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException,HttpWebRequest请求,HttpAbortReason abortReason)在System.ServiceModel.Channels.HttpChannelFactory1.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 IuserServicesQuery.getCompleteUserSnapshot(String emailAddress) at IuserServicesQueryClient.getCompleteUserSnapshot(String emailAddress) Inner Exception: The underlying connection was closed: An unexpected error occurred on a receive. at System.Net.HttpWebRequest.GetResponse() at System.ServiceModel.Channels.HttpChannelFactory1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan超时)处内部异常:无法从传输连接中读取数据:远程主机强行关闭了现有连接。在System.Net.Sockets.NetworkStream.Read(Byte []缓冲区,Int32偏移量,Int32大小)在System.Net.PooledStream.Read(Byte []缓冲区,Int32偏移量,Int32大小)在System.Net.Connection.SyncRead(HttpWebRequest请求,布尔userRetrievedStream,布尔probeRead)内部异常:远程主机强行关闭了现有连接在System.Net.Sockets.Socket.Receive(Byte []缓冲区,Int32偏移量,Int32大小,SocketFlags socketFlags)在System.Net.Sockets.NetworkStream.Read(字节[]缓冲区,Int32偏移量,Int32大小)

我的AppConfig文件看起来像这样-

<endpoint address="" behaviorConfiguration="MyBehavior" binding="basicHttpBinding" bindingConfiguration="IncreasedTimeout" name="BasicHttpEndpoint" contract="BusinessLogicServicesLayer.IuserServicesQuery" listenUriMode="Explicit"> <identity> <dns value="localhost" /> </identity> </endpoint>

&&

<bindings> <basicHttpBinding> <binding name="IncreasedTimeout" openTimeout="12:00:00" receiveTimeout="12:00:00" closeTimeout="12:00:00" sendTimeout="12:00:00"> </binding> </basicHttpBinding> </bindings>
。。

<behaviors> <endpointBehaviors> <behavior name="MyBehavior"> <dataContractSerializer maxItemsInObjectGraph="2147483646" /> </behavior> </endpointBehaviors> <serviceBehaviors>

有人可以帮忙或为我指出正确的方向
答案
这是因为返回数据时,序列化失败,导致WCF服务自动停止。

解决方案:

我们可以在返回数据之前将代理类序列化为所需的实体。

这里是一个演示,学生类包含其他实体的导航属性:

public Student Getstu() CodeFirstDBContext codeFirstDBContext = new CodeFirstDBContext(); Student student =codeFirstDBContext.Student.Find(1); var serializer = new DataContractSerializer(typeof(Student), new DataContractSerializerSettings() DataContractResolver = new ProxyDataContractResolver() ); using (var stream = new MemoryStream()) serializer.WriteObject(stream, student); stream.Seek(0, SeekOrigin.Begin); var stu = (Student)serializer.ReadObject(stream); return stu;

这是客户端将调用的方法。

ServiceReference1.ServiceClient serviceClient = new ServiceReference1.ServiceClient(); var stu = serviceClient.Getstu();

客户端将成功呼叫。

UPDATE禁用加载加载也可以解决此问题:

public class CodeFirstDBContext : DbContext public CodeFirstDBContext() : base("name=DBConn") this.Configuration.ProxyCreationEnabled = false; this.Configuration.LazyLoadingEnabled = false; Database.SetInitializer(new CreateDatabaseIfNotExists<CodeFirstDBContext>());

以上是关于如何通过WCF SOAP服务访问EF导航属性?的主要内容,如果未能解决你的问题,请参考以下文章

如何获取 WCF Web 服务请求的 XML SOAP 请求?

如何创建 PHP SOAP 客户端以在 SSL 下调用 WCF Web 服务?

从 Silverlight 访问 Rally SOAP 服务

带有 EF CTP 5 CodeOnly DbContext 的 WCF 数据服务

在到 WCF Web 服务的 SOAP 消息中,如何将 KeyIdentifier 直接放在 SecurityTokenReference 中(内联,不使用引用令牌)

如果我使用 wcf,如何将 <s:Envelope 更改为 <SOAP-ENV:Envelope?