【问题标题】:WCF service, Azure SQL db and ContentType/Binding errorsWCF 服务、Azure SQL db 和 ContentType/Binding 错误
【发布时间】:2014-04-02 16:54:51
【问题描述】:

我有一个带有两个 OperationContracts 的简单 WCF 服务:

public IEnumerable<string> GetDrivers()
    {
        return new List<string>() { "Senna", "Mansell", "Prost" };
    }

    public IEnumerable<DriverDto> GetRealDrivers()
    {
        using (var ctx = new F1Entities())
        {
            var result = from d in ctx.Drivers select new DriverDto { Name = d.Name, Cost = d.Cost, Team = d.Team };
            return result;

        }
    }

F1Entities 对象是 Azure SQL 数据库的 dbContextDriverDto 类具有DataContract 属性,而NameTeamCost 具有DataMember 属性

在本地调试服务并使用 WCF 测试客户端时,我从 GetDrivers 得到正确响应。当我调用 GetRealDrivers 时出现错误。

接收到的 HTTP 响应时出错 http://localhost:58059/Service1.svc。这可能是由于 服务端点绑定不使用 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 IF1Service.GetRealDrivers() at F1ServiceClient.GetRealDrivers() 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, Boolean probeRead) 内部异常:现有 连接被远程主机强行关闭 System.Net.Sockets.Socket.Receive(Byte[] 缓冲区,Int32 偏移量,Int32 大小,SocketFlags socketFlags)在 System.Net.Sockets.NetworkStream.Read(Byte[] 缓冲区,Int32 偏移量, int32 大小)

调试服务我发现它正确地使用了上下文,并使用 DriverDto 类型的对象填充了结果变量——所有这些都具有正确的名称、成本和团队值。所以它连接到 SQL 数据库就好了。

我已经看到很多关于这个错误的问题,但是花了很多时间关注它们,我觉得我错过了一些更明显的东西。

添加日志记录我发现日志中的异常错误如下:

内容类型 application/soap+xml; charset=utf-8 被发送到服务 期待文本/xml;字符集=utf-8。客户端和服务绑定可以 不匹配。

花费更多时间试图找出错误的根源导致大脑崩溃。 我的服务的 Web.config 没有任何绑定属性,但仍然适用于简单的 GetDrivers 调用。我的网络配置中是否缺少某些内容?这是使用 Azure 云服务的 VS2013 模板创建的。

【问题讨论】:

  • 您必须首先使用ToList() 将它们存储在内存中,因为上下文在序列化完成之前已被释放。出于调试目的,您可以将 includeExceptionDetailsInFault=true 添加到服务器 web.config。
  • 天啊,我不敢相信它是那么好和容易。我整天都在追逐绑定错误,然后是内容类型错误,结果是一个 ToList()。这是一条无用的错误消息。顺便说一句,我有 includeExceptionDetailsInFault=true,但我仍然猜不到。你是明星。添加为答案,我会接受。
  • 很高兴能提供帮助。我已将其添加为答案。

标签: c# .net wcf azure wcf-binding


【解决方案1】:

例如,您必须首先使用ToList() 将它们存储在内存中,因为在序列化完成之前释放上下文。出于调试目的,您可以将 includeExceptionDetailsInFault=true 添加到服务器 web.config。

例子:

public IEnumerable<DriverDto> GetRealDrivers()
{
    using (var ctx = new F1Entities())
    {
        var result = from d in ctx.Drivers
                     select new DriverDto 
                     {
                         Name = d.Name,
                         Cost = d.Cost,
                         Team = d.Team
                     };
        return result.ToList();
    }
}

Web.Config:

<system.serviceModel>
    <behaviors>
        <serviceBehaviors>
            <behavior>
                <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>

【讨论】:

    猜你喜欢
    • 2013-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-02
    • 2012-11-12
    • 2011-03-28
    • 1970-01-01
    相关资源
    最近更新 更多