如何通过 WCF SOAP 服务访问 EF 导航属性? [英] how to access EF navigation properties via WCF SOAP service?

查看:20
本文介绍了如何通过 WCF SOAP 服务访问 EF 导航属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个 WCF 错误 中挣扎了一段时间,但没有任何运气.基本上,我要通过 WCF 服务获取具有导航属性和连接对象的实体 Poco.我的 EF v6 代码成功地从数据库中获取了带有所有相关实体的 poco

I am struggling with this WCF error for some time now without any luck. Basically I am tying to fetch an Entity Poco with Navigation Properties and connected objects via WCF Services. My EF v6 code successfully get the poco with all the related entities from the DB

Poco 调试视图

但是当我尝试通过 WCF 服务访问该实体时,我看到以下错误 -

but as i try to access this Entity via WCF service, i see the following error -

接收对 http://localhost:8734/Design_Time_Addresses/BusinessLogicServicesLayer 的 HTTP 响应时出错/userServices/.这可能是由于服务端点绑定未使用 HTTP 协议.这也可能是由于服务器中止了 HTTP 请求上下文(可能是由于服务关闭).有关更多详细信息,请参阅服务器日志.服务器堆栈跟踪:在 System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)在 System.ServiceModel.Channels.HttpChannelFactory1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)在 System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)在 System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)在 System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)在 System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)在 System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage 消息)在 [0] 处重新抛出异常:在 System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)在 System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 类型)在 IuserServicesQuery.getCompleteUserSnapshot(String emailAddress)在 IuserServicesQueryClient.getCompleteUserSnapshot(String emailAddress)内部异常:基础连接已关闭:接收时发生意外错误.在 System.Net.HttpWebRequest.GetResponse()在 System.ServiceModel.Channels.HttpChannelFactory1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)内部异常:无法从传输连接读取数据:远程主机强行关闭了现有连接.在 System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)在 System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size)在 System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)内部异常:现有连接被远程主机强行关闭在 System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)在 System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)

An error occurred while receiving the HTTP response to http://localhost:8734/Design_Time_Addresses/BusinessLogicServicesLayer/userServices/. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details. Server stack trace: at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason) at 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 timeout) Inner Exception: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) at System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size) at System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead) Inner Exception: An existing connection was forcibly closed by the remote host at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)

我的 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>

有人可以帮忙或指出正确的方向

can someone please help out or point me to a correct direction

推荐答案

这是因为返回数据时,序列化失败,导致WCF服务自动停止.

This is because when data is returned, serialization fails, causing the WCF service to stop automatically.

解决方案:

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

We can serialize the proxy class into the entities we need before returning the data.

这里是一个demo,student类包含了其他实体的导航属性:

Here is a demo,The student class contains the navigation properties of other entities:

            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;
        }


    }

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

This is the method that the client will call.

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

客户端调用成功.

UPDATE 禁用 Loading 加载也解决了这个问题:

UPDATE Disabling Loading loading also solves this problem:

      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 导航属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆