底层连接已关闭。

3

我花了几个小时才明白我为什么会收到以下消息:

底层连接已关闭:服务器关闭了预期保持活动状态的连接。

我发现只要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.

在我的跟踪日志中,我发现以下错误:PS.Converter.ApiModel.DefaultBase类型的属性'test'没有设置方法。这是表示在使用WCF时属性必须始终有一个set方法吗?或者这是可配置的吗?

2
请查看此链接:https://dev59.com/m3E95IYBdhLWcg3wadKq - Eren Ersönmez
2个回答

1
该属性必须在WCF中有一个setter - 否则数据契约序列化程序无法反序列化它。如果您不想将字段序列化,可以向其添加IgnoreDataMember属性。
如果您需要只读属性,只需添加一个私有setter即可。

我需要它被序列化,但它是只读字段。是否有可能忽略 setter? - Theun Arbeider
你可以添加一个私有设置器来获得类似只读的行为。 - Matt Whetton

0

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接