我花了几个小时搞清楚为什么会收到以下消息:
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.
在我的tracelog中,我发现以下错误:No set method for property 'test' in type 'PS.Converter.ApiModel.DefaultBase'
这是否意味着在使用WCF时,属性必须始终有一个集合?或者这是可配置的?
答案 0 :(得分:1)
该属性必须在WCF中有一个setter - 否则数据协定序列化程序无法对其进行反序列化。如果您不希望序列化字段,可以向其添加IgnoreDataMember属性。
如果您需要阅读该属性,只需添加一个私有的setter。
答案 1 :(得分:0)
您可以提供私人二传手。除非我弄错了,否则应该使它(de-)可序列化,但保持值不会被覆盖:
public string Name {
get { return "test"; }
private set{}
}