Webservice Response is turned into ref parameter when using MessageContract

大城市里の小女人 提交于 2019-12-11 02:36:02

问题


I have defined the following Interface

[ServiceContract]
public interface IHealthProducts
{
    [OperationContract()]
    ResponseClass OrderSelfSignedHealthCertificate();
}

Which returns the following type

[MessageContract]
public class ResponseClass
{
    [MessageBodyMember]
    public string AnimalSpeciesCode
    {
        get;
        set;
    }

    [MessageBodyMember]
    public int VBN
    {
        get;
        set;
    }
}

I expected that the generated client code would expose the OrderSelfSignedHealthCertificate method in the following way:

HealthProductsClient client = new HealthProductsClient();
ResponseClass response = client.OrderSelfSignedHealthCertificate();

Instead, the properties of ResponseClass aren't wrapped in the ResponseClass, but exposed like so:

string OrderSelfSignedHealthCertificate(out int VBN)

When I exchange the MessageContract for a DataContract attribute and the MessageBodyMember for a DataMember attribute, I get the expected behaviour (ResponseClass response type). I need the MessageContract however, because I need to put some of the properties in the SOAP header.

Am I doing something wrong? Is this normal behaviour? How do I get a ResponseClass return type, when using a MessageContract?

Any help greatly appreciated.


回答1:


When your [MessageContract] contains more than one [MessageBodyMember], then WCF will return the first one as return value from the service call (AnimalSpeciesCode in your case), and all others are returned as out or ref paraemters.

You can solve this by having only a single [MessageBodyMember] in your message contract that encapsulates all items you need to have return, something like this:

[DataContract]
class CompoundData
{
    public string AnimalSpeciesCode { get; set; }
    public int VBN { get; set; }
}

[MessageContract]
public class ResponseClass
{
    [MessageBodyMember]
    public CompoundData Payload { get; set; }
}

In this case, the CompoundData should be the return value from your service call - containing both elements you need.




回答2:


Thanks Marc, that did it. I made a generic ResponseWrapper class to encapsulate the return types:

[MessageContract(WrapperNamespace = "http://mynamespace.com")]
public class ResponseWrapper<T>
{
    [MessageBodyMember(Namespace = "http://mynamespace.com")]
    public T Response
    {
        get;
        set;
    }
}

I have defined the returntype of OrderSelfSignedHealthCertificate to be

ResponseWrapper<ResponseClass> OrderSelfSignedHealthCertificate();

In my client code this translate to

ResponseClass OrderSelfSignedHealthCertificate(); 

Which is what I wanted. Thanks!




回答3:


I have posted a follow-up to this question here. Generic return types aren't named properly when using a MessageContract.



来源:https://stackoverflow.com/questions/2373564/webservice-response-is-turned-into-ref-parameter-when-using-messagecontract

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!