2013-03-05 231 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

相關問題