底层连接已关闭:连接意外关闭
当多次使用WCF时,异常按摩并不能帮助我们解决问题 问题。上述消息通常是以下问题之一的症状:
- 返回值大于配置文件中定义的值。
- 端点设置有问题
- 数据序列化有问题
我遇到的第三个问题与枚举有关 问题是枚举是用值显式定义的
Public Enum FrequencyEnums
EveryTime = 1
OncePerHour = 2
OncePerDay = 3
OncePerWeek = 4
Never = 5
End Enum
并且使用该枚举的私有属性定义如下
Private m_sendFrequencyID As FrequencyEnums
现在因为枚举没有默认值的定义并且 因为该属性没有显式初始化并且 因为枚举中缺少 0 的枚举值,并且 因为无论指定的选项如何,enum 的默认值始终为 0 当我尝试将此类的实例返回给客户端时,出现以下错误: 基础连接已关闭:连接意外关闭
解决方案是以下方法之一:
- 为枚举定义 0 值或
- 根据枚举值为属性定义默认值。
- 为属性分配初始值
我的问题是如何使用 Microsoft 工具而不是通过反复试验发现此错误?
When working with WCF many times the exception massage doesn’t help us to solve the
problem. The above massage is usually a symptom for one of the following problems:
- The return values are bigger than the value which was defined in the config file.
- There is a problem with the endpoint setting
- There is a problem with serialization of the data
I experienced the 3rd problem which was with enums
The problem was that the an enum was defined explicitly with values
Public Enum FrequencyEnums
EveryTime = 1
OncePerHour = 2
OncePerDay = 3
OncePerWeek = 4
Never = 5
End Enum
And the private property that was using this enum was defined as follows
Private m_sendFrequencyID As FrequencyEnums
Now because the enum does not have a definition of a default value and
because the property is not initialized explicitly and
because the enum value for 0 is missing from the enum and
because the default value of enum regardless of the specified options is always 0
When I tried to return instance of this class to the client I got this error:
The underlying connection was closed: The connection was closed unexpectedly
The solution is one of the following:
- Define 0 value for enums or
- Define a default value for a property from the enum values.
- Assign initial value to the property
My question is how could I have found this error with Microsoft tools and not by trial and error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不久前发现了这个问题,试图解决相同的错误消息(我有一个不同的问题)。启用跟踪并使用 svctraceviewer.exe 查看跟踪日志对我帮助很大。请参阅以下网址了解更多信息:
启用 WCF 跟踪日志
Found this a while ago trying to resolve the same error message (I had a different issue). Enabling trace and use
svctraceviewer.exe
to look at the trace log helped me a lot. Please see the following URL for more information:Enable WCF Trace Log
您需要为枚举创建数据契约。请参阅: http://consultingblogs.emc.com/merrickchaffer/archive/2007/04/03/Passing-Enum-values-into-WCF-Service-operations.aspx
You need to create a data contract for the enum. See: http://consultingblogs.emc.com/merrickchaffer/archive/2007/04/03/Passing-Enum-values-into-WCF-Service-operations.aspx