Reputation: 9691
I have a class with this property
public bool IsDeleted { get; internal set; }
I'm using this class over some WCF services, playing with some instances of the class. All the other public properties are visible after getting an instance of the class, except IsDeleted. If I make the setter public, IsDeleted works too.
Any ideea of this weird behaviour?
Upvotes: 4
Views: 426
Reputation: 127593
Most all serialization methods in .NET (including WCF) require a accessible getter and setter to work because it must be able to set the property to a value when it desearalizes the incoming field.
There is a work around, you can mark the backing store private member as the serialization field instead.
[DataContract]
class Foo
{
[DataMember(Name="IsDeleted")]
private bool _isDeleted;
public bool IsDeleted
{
get { return _isDeleted; }
internal set { _isDeleted = value; }
}
}
However, if your client does not have a refrence to Foo
as a refrence DLL it will create a auto-generated proxy that will will look like.
class Foo
{
public bool IsDeleted { get; set; }
}
To prevent that from happening make sure metadata publishing is turned off and you distribute a DLL with Foo
to all clients who will consume your WCF service.
If you don't want to pass around a DLL you can use the workaround Silvermind mentioned in the comments by having the setter do nothing and having a internal method to set the backing member.
[DataContract]
class Foo
{
private bool _isDeleted;
public bool IsDeleted
{
get { return _isDeleted; }
set { }
}
internal void SetIsDeletedInternal(bool value)
{
_isDeleted = value;
}
}
However note that if you do this and the client passes a Foo
object to you this will always force set _isDeleted
to default(bool)
on the object you receive from the client.
Upvotes: 6