Theun Arbeider
Theun Arbeider

Reputation: 5419

The underlying connection was closed

I've spend some hours figuring out why I get the following message:

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

I've found out that I get this message whenever the webservice attempts to send a property with does have a basic get / set.

Example:

public string Name {get;set;} //Works without problems

public string Name { get { return "test"; } } //Fails

At first attempt it gives me the following error:

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

If I try it again it gives me the following error:

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.

In my tracelog I find the following error: No set method for property 'test' in type 'PS.Converter.ApiModel.DefaultBase'

Does this mean a property MUST have a set at all times when using WCF? Or is this is some way configurable ?

Upvotes: 3

Views: 1994

Answers (2)

Matt Whetton
Matt Whetton

Reputation: 6786

The property must have a setter in WCF - otherwise the data contract serializer cannot deserialize it. If you dont want the field to be serialized you can add the IgnoreDataMember attribute to it.

If you need the property to be read only just add a private setter.

Upvotes: 1

Kjartan
Kjartan

Reputation: 19111

You can provide a private setter. Unless I'm mistaken, that should make it (de-)serializable, but keep the value from being overwritten:

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

Upvotes: 0

Related Questions