2013-03-05 177 views
3

我已经花了一些时间弄清楚为什么我得到以下信息:基础连接被关闭

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

我发现了,我得到每当web服务试图发送一个属性此消息与确实有一个基本的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时必须始终有一个集合?或者这是一些可配置的方式?

+2

看到这一点:http://stackoverflow.com/q/2323277/201088 – 2013-03-05 08:40:17

回答

0

您可以提供私人二传手。除非我记错了,应该让它(去)序列化,但保留被覆盖的值:

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

该属性必须在WCF中有一个setter,否则数据协定序列化程序不能反序列化它。如果您不希望字段被序列化,您可以将IgnoreDataMember属性添加到它。

如果您需要只读属性,只需添加一个私人setter。

+0

我需要它做序列化,但它是一个只读字段。是否有可能忽略二传手? – 2013-03-05 08:48:57

+0

你可以添加一个私人setter来获取只读(ish)行为 – 2013-03-05 10:11:55

相关问题