Complex data types in WCF?

2019-01-21 05:55发布

I've run into a problem trying to return an object that holds a collection of childobjects that again can hold a collection of grandchild objects. I get an error, 'connection forcibly closed by host'.

Is there any way to make this work? I currently have a structure resembling this:

pseudo code:

Person:
IEnumerable<Order>

Order:
IEnumerable<OrderLine>

All three objects have the DataContract attribute and all public properties i want exposed (including the IEnumerable's) have the DataMember attribute.

I have multiple OperationContract's on my service and all the methods returning a single object OR an IEnumerable of an object works perfectly. It's only when i try to nest IEnumerable that it turns bad. Also in my client service reference i picked the generic list as my collection type. I just want to emphasize, only one of my operations/methods fail with this error - the rest of them work perfectly.

EDIT (more detailed error description):

[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.]

I tried looking for logs but i can't find any... also i'm using a WSHttpBinding and an http endpoint.

14条回答
Anthone
2楼-- · 2019-01-21 06:35

I have had the same problem too (.Net 3.5). Turns out my base class DataContract was missing a known type. It's unfortunate that the WCF error was not more descriptive.

查看更多
Ridiculous、
3楼-- · 2019-01-21 06:36

Ok i finally found the real problem in my case. It seems exposing enums is not the greatest thing in the world. I either have to set a default value on them, or instead expose the property as an int or whatever integer-type my enum is based on.

Thanks for helping, you had no way of knowing this - i found the enums on the 3rd level in my structure and systematicly removing datamembers one by one was the way i found out. It seems i'm not the only one who ran into this problem - this guy obviously had similar problems :)

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

查看更多
爷、活的狠高调
4楼-- · 2019-01-21 06:37

If you are working with WCF+(EF+POCO) then try setting,

ObjectContext.ContextOptions.LazyLoadingEnabled = false;
ObjectContext.ContextOptions.ProxyCreationEnabled = false;
查看更多
我只想做你的唯一
5楼-- · 2019-01-21 06:37
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 
查看更多
霸刀☆藐视天下
6楼-- · 2019-01-21 06:38

Don't return the literal IEnumerable in a contract, there is the famous WCF IEnumerable bug

查看更多
该账号已被封号
7楼-- · 2019-01-21 06:41

Add this line into <system.web/>:

<httpRuntime maxRequestLength="102400" executionTimeout="3600" />
查看更多
登录 后发表回答