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条回答
可以哭但决不认输i
2楼-- · 2019-01-21 06:22

I don't know why this can happen. but i also had similar problems.

I have changed my enums.remove the indexes(e.g. ASNOrder = 1, -> ASNOrder,),and no error occoured.

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

I've had this error when using 'yield return' to build up an enumeration of objects mapped to my DataContract type.

Calling ToList / ToArray on the yield results fixed the issue and the service call worked correctly.

查看更多
祖国的老花朵
4楼-- · 2019-01-21 06:28

did you specify in your service behavior config? it seems like some information is missing in this stacktrace.

can you grab the exception at server side (e.g. in visual studio debug mode or with a logging library like log4net).

have you tried calling some other methods (simple helloworld() e.g.) on the same service to be sure that the service configuration itself works? this kind of exceptino could also indicate some serialization problems. what types do you want to send over the wire? do you use KnownType's somewhere?

查看更多
我命由我不由天
5楼-- · 2019-01-21 06:28

Try setting [OperationBehavior()] above your implementation of the interface method.

查看更多
孤傲高冷的网名
6楼-- · 2019-01-21 06:29

Yep, I had the same problem here and it was todo with returning objects that had enum values in it. Changed the DataMember to an int and everything statrted working.

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

this is actually the same information as your first exception description. it would be interesing what the original cause for the socketexception was. it has to be some type of error in the service itself. can you locate where exactly whar exception happens?

i had similar errors when trying to return normal IEnumerables that were overwritten (they were marked as virtual) by NHibernate, and substitued with GenericPersistentBag, which is not serializable. have you marked your IEnumerable datamembers as virtual due to nhibernate or something similar? this could explain your error.

btw. wcf exceptions are often quite meaningless (which can be very frustrating when tracking down a bug ;)

查看更多
登录 后发表回答