Question

Can I use recursive MessageContract in WCF ? for example :

I need to post some parameters, one of the parameters is an array of file stream. this is My OperationContract :

DomainResult AddSomethingNew(int externalCustomerId, string domainName, bool isDefault, FileDataContract[] files);

here is my MessageContract :

[MessageContract]
public class FileDataContract
{        
    [MessageHeader(MustUnderstand=true)]
    public int ExternalCustomerId { get; set; }

    [MessageHeader(MustUnderstand=true)]
    public string DomainName{get;set;}

    [MessageHeader(MustUnderstand=true)]
    public bool IsDefault{get;set;}

    [MessageBodyMember(Order=1)]
    public FileUploadInputParameter[] Files { get; set; }
}

[MessageContract]
public class FileUploadInputParameter
{
    [MessageHeader(MustUnderstand = true)]
    public string FileName { get; set; }

    [MessageHeader(MustUnderstand = true)]
    public decimal FileSize { get; set; }

    [MessageBodyMember(Order = 1)]
    public Stream FileStream { get; set; }
}

need your helps..

Was it helpful?

Solution

You can use inheritance to define relation between two message contracts:

[MessageContract]
public class FileUploadInputParameter
{
    [MessageHeader(MustUnderstand = true)]
    public string FileName { get; set; }

    [MessageHeader(MustUnderstand = true)]
    public decimal FileSize { get; set; }

    [MessageBodyMember(Order = 1)]
    public Stream FileStream { get; set; }
}

[MessageContract]
public class FileDataContract : FileUploadInputParameter
{        
    [MessageHeader(MustUnderstand=true)]
    public int ExternalCustomerId { get; set; }

    [MessageHeader(MustUnderstand=true)]
    public string DomainName{get;set;}

    [MessageHeader(MustUnderstand=true)]
    public bool IsDefault{get;set;}
}

You cannot define message contract as you described in FileDataContract because you cannot have array of message headers and more over Stream can be in most cases the only available body element and it must be only one. So if you need to pass multiple files you should also implement some compression (zip) and send single stream.

OTHER TIPS

Quick answer: no, you can't. Message contract is a top-level definition of a SOAP message, not something which can be composed. In your example, you define the Files member to be in the body, but some of its properties (FileName, FileSize) to be in the header which is not consistent. If you try that it may even "work", in a way that you won't see any errors, but just because WCF will be treating the FileUploadInputParameter type as a POCO serializable type.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top