I am developing a web servic e using .NET WCF. I am using a mesaging service style like this:
[ServiceContract]
public interface IMyService {
[OperationContract]
MyMessageResponse MyOperation(MyMessageRequest msg);
}
Well, I want to apply the MessageContractAttribute to MyMessageResponse and MyMessageRequest because I need to tune SOAP in my own way.
Well, the problem is that MyMessageResponse and MyMessageRequest are part of a hierarchy (I have many service operations and, so, many message types).
Suppose my hierarchy to be this one (class inheritance pseudocode):
MyMessage <- MyMessageFamily1 <- MyMessageRequest
MyMessage <- MyMessageFamily1 <- MyMessageResponse
MyMessage <- MyMessageFamily2 <- MyOtherMessageRequest
MyMessage <- MyMessageFamily2 <- MyOtherMessageResponse
I would like to apply MessageContractAttribute to MyMessage, then to MyMessageFamily1 and then to MyMessageRequest and to MyMessageResponse (so for the others) like this:
[MessageContract(...)]
public class MyMessage {
[MessageBodyMember(...)]
public MyType1 Member1; /* MyType1 has a DataContract applied */
[MessageBodyMember(...)]
public MyType2 Member2; /* MyType2 has a DataContract applied */
}
[MessageContract(...)]
public class MyMessageFamily1 : MyMessage {
[MessageBodyMember(...)]
public MyType3 MemberFamily11; /* MyType3 has a DataContract applied */
[MessageBodyMember(...)]
public MyType3 MemberFamily12;
}
[MessageContract(...)]
public class MyMessageFamily2 : MyMessage {
[MessageBodyMember(...)]
public MyType3 MemberFamily21; /* MyType3 has a DataContract applied */
[MessageBodyMember(...)]
public MyType3 MemberFamily22;
}
[MessageContract(...)]
public class MyMessageRequest : MyMessageFamily1 {
[MessageBodyMember(...)]
public MyType4 MemberRequest1; /* MyType4 has a DataContract applied */
[MessageBodyMember(...)]
public MyType4 MemberRequest2;
}
[MessageContract(...)]
public class MyMessageResponse : MyMessageFamily1 {
[MessageBodyMember(...)]
public MyType4 MemberResponse1;
[MessageBodyMember(...)]
public MyType4 MemberResponse2;
}
[MessageContract(...)]
public class MyOtherMessageRequest : MyMessageFamily2 {
[MessageBodyMember(...)]
public MyType5 MemberRequest1; /* MyType5 has a DataContract applied */
[MessageBodyMember(...)]
public MyType5 MemberRequest2;
}
[MessageContract(...)]
public class MyOtherMessageResponse : MyMessageFamily2 {
[MessageBodyMember(...)]
public MyType5 MemberResponse1;
[MessageBodyMember(...)]
public MyType5 MemberResponse2;
}
Problem: MessageContractAttribute is an attribute tat can be inherited, but MessageBodyMemberAttribute and MessageHeaderAttribute do not inherit because of their definition.
The problem is that I want to create a service implementing IMyService.
IMyService has MyOperation that uses MyMessageResponse and MyMessageRequest.
But MyMessageResponse should inherit MemberFamily11, MemberFamily12, Member1 and Member2 and treat them in the soap message in order to place these in the body of the soap message. But the MessageBodyMemberAttribute is not inherited…
How can I solve this problem?
You could do that but it is perhaps not the best way to do XML SOAP services because your service definition is being driven by the implementation rather than the other way around. .net and c# are a consequence, not the contract.
My advice is to create concrete-types and not use derivation or hierarchies. This sort-of problem is easily discovered and rectified in XSD-schema-first SOAP development – 1.
hope this helps
1 more information on schema-first can be found here