If you do not want to use inheritance, for example:
[DataContract] public class User { } [DataContract] public class Service1User : User { [DataMember] public string PropertyVisibleOnlyForService1{...} } [DataContract] public class Service2User : User { [DataMember] public string PropertyVisibleOnlyForService2{...} } [ServiceContract] public interface IService1 { List<Service1User> GetUsers();
Then I'm not sure what you will do. Your sortof breaks down the principles of type declaration at this point. Think about it in normal .NET mode; if you define "User" in your application, then it is the same everywhere. Some properties cannot be hidden from some other classes or methods.
WCF is also going to pack this type of information into the generated WSDL, and it will only determine the user type once, so it needs to know what properties exist.
Now, if all you care about is the actual SOAP message that was designed, and you don't care about the WSDL or what any clients created from WSDL will see, then technically you can make it not highlight it property in the SOAP message when it is null by doing:
[DataMember(EmitDefaultValue=false)]
Then, when this property is null, it will not be included in serialization. This would not make any difference if the client were created from WSDL, although its user type should still contain both properties. This will simply change the serialization so that instead of sending to the client something like:
<User> <PropertyVisibleOnlyForService1 nil="true" /> <PropertyVisibleOnlyForService2>something</PropertyVisibleOnlyForService2> </User>
instead, it will send:
<User> <PropertyVisibleOnlyForService2>something</PropertyVisibleOnlyForService2> </User>
Codingwithspike
source share