【问题标题】:The underlying connection was closed底层连接已关闭
【发布时间】:2013-03-05 08:18:26
【问题描述】:

我花了几个小时弄清楚为什么会收到以下消息:

The underlying connection was closed: A connection that was expected to be kept alive was closed by the server.

我发现每当网络服务尝试发送具有基本获取/设置的属性时,我都会收到此消息。

例子:

public string Name {get;set;} //Works without problems

public string Name { get { return "test"; } } //Fails

第一次尝试它给了我以下错误:

The underlying connection was closed: A connection that was expected to be kept alive was closed by the server.

如果我再试一次,它会给我以下错误:

An error occurred while receiving the HTTP response to http://localhost/webapi3/ProductData.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.

在我的跟踪日志中,我发现以下错误:No set method for property 'test' in type 'PS.Converter.ApiModel.DefaultBase'

这是否意味着在使用 WCF 时属性必须始终有一个集合?或者这是某种可配置的方式?

【问题讨论】:

标签: c# web-services properties


【解决方案1】:

该属性必须在 WCF 中有一个设置器 - 否则数据协定序列化程序无法反序列化它。如果您不希望该字段被序列化,您可以向其添加 IgnoreDataMember 属性。

如果您需要只读属性,只需添加一个私有设置器。

【讨论】:

  • 我需要它被序列化,但它是一个只读字段。是否有可能忽略setter?
  • 您可以添加一个私有设置器来获取只读(ish)行为
【解决方案2】:

您可以提供一个私人设置器。除非我弄错了,否则它应该可以(反)序列化,但要防止值被覆盖:

public string Name { 
    get { return "test"; }
    private set{}
} 

【讨论】:

    猜你喜欢
    • 2013-10-31
    • 2018-01-31
    • 2017-09-21
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多