c# - WCF 服务、Azure SQL 数据库和 ContentType/Binding 错误

标签 c# .net wcf azure wcf-binding

我有一个简单的 WCF 服务,其中包含两个 OperationContracts:

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 时,我收到错误。

An error occurred while receiving the HTTP response to http://localhost:58059/Service1.svc. 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 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 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)

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

我看到了很多因这个错误而提出的问题,但在花了很多时间关注这些问题后,我觉得我错过了一些更明显的东西。

添加日志记录我在日志中发现异常错误,如下所示:

Content Type application/soap+xml; charset=utf-8 was sent to a service expecting text/xml; charset=utf-8. The client and service bindings may be mismatched.

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

最佳答案

例如,您必须首先使用 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();
    }
}

网络配置:

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

关于c# - WCF 服务、Azure SQL 数据库和 ContentType/Binding 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22116330/

相关文章:

c# - 无法在没有DataSource的DataGridView中将当前单元格设置为不可见的单元格

c# - Xamarin Forms 平移手势速度

c# - 生成随机字符串

WCF 客户端连接缓存/池

WCF 缓慢的 ServiceHost.Open() 调用

c# - 如何使用 C# 运行远程计算机程序 'run as administrator'

c# - 需要帮助开发和绑定(bind) Double <--> 转换器

.net - 错误 175 : The specified store provider cannot be found in the configuration, 或无效

.net - 确定图像的文件类型

c# - 更新 WCF .map 文件服务引用