底层连接已关闭

本文关键字:连接 | 更新日期: 2023-09-27 18:02:21

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

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

我发现每当webservice试图发送一个带有基本get/set的属性时,我都会得到这个消息。

的例子:

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时,属性必须始终有一个集合?或者这是可配置的?

底层连接已关闭

属性必须在WCF中有一个setter -否则数据契约序列化器不能反序列化它。如果你不希望这个字段被序列化,你可以给它添加IgnoreDataMember属性。

如果你需要的属性是只读的,只需添加一个私有setter

您可以提供一个私有setter。除非我弄错了,这应该使它(反)序列化,但保持值不被覆盖:

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