2008-10-15 85 views
21

我遇到了一個問題,試圖返回一個對象,該對象包含一個可以容納一個孫集對象的集合的子對象。我收到一個錯誤,'主持人強行關閉連接'。WCF中的複雜數據類型?

有什麼辦法可以使這項工作?我現在有類似這樣的結構:

僞代碼:

Person: 
IEnumerable<Order> 

Order: 
IEnumerable<OrderLine> 

所有這三個對象具有DataContract屬性和所有的公共屬性我要暴露(包括了IEnumerable的)有數據成員的屬性。

我在我的服務上有多個OperationContract,並且所有方法都返回一個對象,或者一個對象的IEnumerable完美地工作。只有當我嘗試嵌套IEnumerable它變成壞。同樣在我的客戶服務參考中,我選擇了通用列表作爲我的集合類型。我只想強調,只有我的一個操作/方法失敗並出現此錯誤 - 其餘部分完美工作

EDIT(更詳細的錯誤描述):

[SocketException (0x2746): An existing connection was forcibly closed by 
the remote host] 
[IOException: Unable to read data from the transport connection: 
An existing connection was forcibly closed by the remote host.] 
[WebException: The underlying connection was closed: An unexpected 
error occurred on a receive.] 
[CommunicationException: An error occurred while receiving the HTTP 
response to http://myservice.mydomain.dk/MyService.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.] 

我試圖尋找日誌,但我無法找到任何...還我使用的WSHttpBinding和HTTP端點。

+0

您的對象中是否有不正確的序列化? – 2008-10-15 09:12:07

+0

我不知道。我想也許嵌套的IEnumberable 可能無法序列化?但是我怎麼知道?我可以一路調試到實際OperationContract的返回,一切都很好,但交通運輸似乎出了問題。我沒有序列化屬性,而是使用[DataMember] – 2008-10-15 09:21:21

+0

我面臨着同樣的錯誤,因爲我..我的類也定義了枚舉屬性,但我沒有看到這是任何地方的問題..枚舉應該沒問題..你通過設置默認值是什麼意思?他們是價值類型,所以他們總是有一個默認值。我會嘗試刪除枚舉屬性,看看是否修復它..這裏是支持的數據約定類的一些信息http://msdn.microsoft.com/en-us/library/ms731923.aspx – 2010-10-29 00:17:36

回答

37

作爲一個說明,你需要學習如何使用WCF日誌實用程序:

Logging info.

Config Editor(使它成爲設置快照)。

Trace viewer.完全真棒。允許多個服務(客戶端和服務器)跟蹤並加入它們並幫助您分析所有細節。讓你快速找到問題的根源。 (原因當有服務器WCF錯誤,客戶端不太可能獲得有用的數據。)

+0

我在哪裏可以找到跟蹤查看器應用程序,MS文檔談論它,但沒有提及如何開始使用它? – 2010-03-06 11:01:08

0

您在服務行爲配置中指定了嗎?這個堆棧跟蹤中似乎缺少一些信息。

你可以在服務器端抓取異常(例如在visual studio調試模式下或者使用類似log4net的日誌庫)。

您是否嘗試過在同一服務上調用其他一些方法(例如簡單的helloworld())以確保服務配置本身有效? 這種exceptino還可能表明一些序列化問題。你想通過電匯發送什麼類型?你在什麼地方使用KnownType?

+0

我真的很高興爲所有我可以得到的幫助,所以謝謝回覆!,但請再次閱讀我的問題 - 只有一個我的方法失敗,出現此錯誤。關於例外,我可以發佈完整的stacktrace,但它只是膨脹的問題?我會在回覆中發佈它:) – 2008-10-15 09:11:52

0
Server Error in '/' Application. 
-------------------------------------------------------------------------------- 

An existing connection was forcibly closed by the remote host 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host 

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

Stack Trace: 


[SocketException (0x2746): An existing connection was forcibly closed by the remote host] 
    System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) +93 
    System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) +119 

[IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.] 
    System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) +267 
    System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size) +25 
    System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead) +306 

[WebException: The underlying connection was closed: An unexpected error occurred on a receive.] 
    System.Net.HttpWebRequest.GetResponse() +1532114 
    System.ServiceModel.Channels.HttpChannelRequest.WaitForReply(TimeSpan timeout) +40 

[CommunicationException: An error occurred while receiving the HTTP response to http://Zzzstrukturservice.xxx.dk/ZzzstrukturService.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.] 
    System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) +2668969 
    System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) +717 
    xxx.Services.ZzzstrukturServiceClient.ZzzstrukturServiceProxy.IZzzstrukturService.GetMatrixSet(Int32 matrixSetId) +0 
    xxx.Services.ZzzstrukturServiceClient.ZzzstrukturRepository.GetMatrixSetById(Int32 matrixSetId) in f:\ccnet\work\xxx.Zzzstruktur\1. Presentation Layer\ZzzstrukturServiceClient\ZzzstrukturRepository.cs:90 
    xxx.yyy.yyyWeb.AnnoncePage.OnLoad(EventArgs e) in f:\ccnet\work\yyyV2\1. Presentation Layer\yyyWeb\Annonce.aspx.cs:40 
    System.Web.UI.Control.LoadRecursive() +47 
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1436 




-------------------------------------------------------------------------------- 
Version Information: Microsoft .NET Framework Version:2.0.50727.1433; ASP.NET Version:2.0.50727.1433 
1

這實際上與您的第一個異常描述相同的信息。它會影響到Socketexception的最初原因是什麼。它必須是服務本身的某種類型的錯誤。你能找到正確的碼發生異常嗎?

當我嘗試返回被NHibernate覆蓋(它們被標記爲虛擬)的正常IEnumerables並且用不可序列化的GenericPersistentBag代替時,我有類似的錯誤。 您是否因爲nhibernate或類似的東西而將您的IEnumerable數據成員標記爲虛擬?這可以解釋你的錯誤。

btw。 wcf例外往往是毫無意義的(這可能是非常令人沮喪的,當追蹤一個bug時)

+0

這就是我可以得到的所有異常信息。當我調試通過時,我得到相同的異常(如果我包括堆棧跟蹤)。我們使用Linq2Sql,但實際上將這些對象轉換爲較輕的對象,只保存簡單的數據類型,但嵌套的集合除外。 – 2008-10-15 09:24:54

+0

我的意思是 - DataContract對象不屬於項目中的任何其他部分 - 它們幾乎都是DTO的,除了持有其他「DTO」的集合 – 2008-10-15 09:26:14

11

好吧,我終於找到了真正的問題在我的情況。看起來暴露枚舉並不是世界上最偉大的事情。我要麼必須在它們上設置一個默認值,或者將該屬性作爲int或者我的枚舉所基於的任何整數類型公開。

感謝您的幫助,您無法知道這一點 - 我發現在我的結構中的第三級枚舉,並系統地逐個刪除數據成員是我發現的方式。看來我不是一個誰遇到了這個問題只有一個 - 這傢伙顯然也有類似的問題:)

http://zianet.dk/blog/2007/11/24/serializing-enums-in-wcf/

0

我不知道爲什麼會發生這種情況。但我也有類似的問題。

我改變了我的enums.remove索引(例如ASNOrder = 1, - > ASNOrder),並沒有錯誤發生。

1

我也有同樣的問題(.Net 3.5)。原來我的基類DataContract缺少一個已知類型。不幸的是,WCF的錯誤不是更具描述性。

3

添加此行到<system.web/>

<httpRuntime maxRequestLength="102400" executionTimeout="3600" /> 
0

是的,我在這裏有同樣的問題,它與待辦事項返回枚舉值的對象。將DataMember更改爲int並且一切正常工作。

0

嘗試設置[OperationBehavior()]以上您的接口方法的實現。

0

我在使用'yield return'建立映射到我的DataContract類型的對象的枚舉時遇到了此錯誤。

在收益結果上調用ToList/ToArray修復了問題並且服務調用正常工作。

10

如果您正在使用WCF +(EF + POCO)工作,然後嘗試設置,

ObjectContext.ContextOptions.LazyLoadingEnabled = false; 
ObjectContext.ContextOptions.ProxyCreationEnabled = false; 
3

枚舉得到DataContract屬性,像任何一類會,但枚舉值不應該在有DataMember屬性他們。

將它們更改爲EnumMember,您將不會再收到這個難以解釋的錯誤。