[英]WCF service, Azure SQL db and ContentType/Binding errors
我有一個帶有兩個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數據庫的dbContext
。 DriverDto
類具有DataContract
屬性,而Name
, Team
和Cost
具有DataMember
屬性
在本地調試服務並使用WCF測試客戶端時,我從GetDrivers得到正確的響應。 當我致電GetRealDrivers時,出現錯誤。
收到對
http://localhost:58059/Service1.svc
的HTTP響應時發生錯誤。 這可能是由於服務端點綁定未使用HTTP協議。 這也可能是由於服務器終止了HTTP請求上下文(可能是由於服務關閉了)。 有關更多詳細信息,請參見服務器日志。 服務器堆棧跟蹤:位於System.ServiceModel.Channels.HttpChannelFactory的System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException,HttpWebRequest請求,HttpAbortReason abortReason)1.System.ServiceModel.Channels處的1.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(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(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(TimeSpan超時)內部異常:無法從傳輸連接中讀取數據:現有連接已由遠程強制關閉主辦。 在System.Net.Socket.NetworkStream.Read(Byte []緩沖區,Int32偏移量,Int32大小)在System.Net.Socket.NetworkStream.Read(Byte []緩沖區,Int32偏移量,Int32大小)在System.Net.Connection.SyncRead (HttpWebRequest請求,布爾userRetrievedStream,布爾probeRead)內部異常:系統上的遠程主機在System.Net.Sockets.Socket.Receive(Byte []緩沖區,Int32偏移量,Int32大小,SocketFlags,socketFlags)處強制關閉了現有連接.Net.Sockets.NetworkStream.Read(Byte []緩沖區,Int32偏移量,Int32大小)
調試服務我發現它正確地使用了上下文,並使用DriverDto類型的對象填充了結果變量-所有這些都具有正確的名稱,成本和組值。 因此它可以很好地連接到SQL數據庫。
我已經看到很多與此錯誤有關的問題,但是花了很多時間跟蹤它們,我覺得我錯過了一些更明顯的事情。
添加日志記錄,我在日志中發現異常錯誤,如下所示:
內容類型application / soap + xml; charset = utf-8已發送到需要text / xml的服務; 字符集= UTF-8。 客戶端和服務綁定可能不匹配。
花更多的時間試圖找出最根本的錯誤已導致大腦崩潰。 我用於該服務的Web.config沒有任何綁定屬性,但仍可用於簡單的GetDrivers調用。 我在網絡配置中缺少什么嗎? 這是使用VS2013 Azure雲服務模板創建的。
例如,您必須先使用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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.