WCF服务,SQL Azure的DB和的ContentType /绑定错误
我有两个OperationContracts
一个简单的WCF服务:WCF服务,SQL Azure的DB和的ContentType /绑定错误
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分贝dbContext
。 DriverDto
类有DataContract
属性,Name
,Team
和Cost
有DataMember
属性
当本地调试服务,并使用WCF测试客户端,我从GetDrivers正确的反应。当我调用GetRealDrivers时,虽然我得到一个错误。
接收到对
http://localhost:58059/Service1.svc
的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 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.HttpChannelFactory
1.HttpRequestChannel.HttpChannelRequest.WaitForReply(时间跨度 超时)内部异常:无法从传输中读取数据 连接:现有连接被远程 主机强制关闭。在System.Net.PooledStream.Read(Byte []缓冲区, Int32偏移量,Int32大小)在System.Net.Sockets.NetworkStream.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大小)
调试我看到它正确地使用上下文并使用DriverDto类型的对象填充结果变量 - 所有这些对象都具有正确的名称,成本和团队值。所以它连接到SQL数据库就好了。
我已经看到很多提出这个错误的问题,但花了好几个小时跟着他们,我觉得我错过了更明显的东西。
添加日志我找到日志像这样的异常错误:
内容类型application /肥皂+ xml的; charset = utf-8被发送到期望text/xml的服务 ;字符集= UTF-8。客户端和服务绑定可能不匹配。
花费更多的时间试图到达底部的错误,导致大脑崩溃。 我的Web.config服务没有任何绑定属性,但仍适用于简单的GetDrivers调用。我在我的网页配置中丢失了什么?这是使用Azure Cloud Service的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();
}
}
Web.Config中:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
你将不得不将它们存储在存储器中首先用'ToList()'例如,因为历境被序列化之前设置做完了。出于调试目的,您可以将'includeExceptionDetailsInFault = true'添加到服务器web.config。 – Silvermind
Omg,我不敢相信这件事很好,很容易。我一直在追逐绑定错误,然后是内容类型错误,整天都变成了ToList()。这是一个无益的错误消息。顺便说一下,我有includeExceptionDetailsInFault = true,但我仍然不会猜到。你是明星。添加为答案,我会接受它。 – Mashton
很高兴能有所帮助。我已经将它添加为答案。 – Silvermind