2008-10-15 103 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,您将不会再收到这个难以解释的错误。