问题
I'm building a web service which uses MessageContracts, because I want to add custom fields to my SOAP header. In a previous topic, I learned that a composite response has to be wrapped. For this purpose, I devised a generic ResponseWrapper class.
[MessageContract(WrapperNamespace = "http://mynamespace.com",
WrapperName="WrapperOf{0}")]
public class ResponseWrapper<T>
{
[MessageBodyMember(Namespace = "http://mynamespace.com")]
public T Response
{
get;
set;
}
}
I made a ServiceResult base class, defined as follows:
[MessageContract(WrapperNamespace = "http://mynamespace.com")]
public class ServiceResult
{
[MessageBodyMember]
public bool Status
{
get;
set;
}
[MessageBodyMember]
public string Message
{
get;
set;
}
[MessageBodyMember]
public string Description
{
get;
set;
}
}
To be able to include the request context in the response, I use a derived class of ServiceResult, which uses generics:
[MessageContract(WrapperNamespace = "http://mynamespace.com",
WrapperName = "ServiceResultOf{0}")]
public class ServiceResult<TRequest> : ServiceResult
{
[MessageBodyMember]
public TRequest Request
{
get;
set;
}
}
This is used in the following way
[OperationContract()]
ResponseWrapper<ServiceResult<HCCertificateRequest>> OrderHealthCertificate(RequestContext<HCCertificateRequest> context);
I expected my client code to be generated as
ServiceResultOfHCCertificateRequest OrderHealthCertificate(RequestContextOfHCCertificateRequest context);
Instead, I get the following:
ServiceResultOfHCCertificateRequestzSOTD_SSj OrderHealthCertificate(CompType1 c1, CompType2 c2, HCCertificateRequest context);
CompType1
and CompType2
are properties of the RequestContext
class. The problem is that a hash is added to the end of ServiceResultOfHCCertificateRequestzSOTD_SSj
. How do I need define my generic return types in order for the client type to be generated as expected (without the hash)?
回答1:
We have also had problems returning a generic list over WCF.
The solution that works for us is to create a class that has a single property, and that property is a generic list.
Then we send that class over WCF. If you have many such lists you can create a class that has a list of type T. So that when you create the class you can specify which type of list you want.
来源:https://stackoverflow.com/questions/2387176/problem-with-messagecontract-generic-return-types-and-clientside-naming