简体   繁体   中英

How do I return a struct containing binary data from a WCF REST service?

I have to implement the following scenario:

  • the client sends a request to a WCF REST service providing a set of parameters and a binary file to process
  • the service gets the file, processes it and produces a result binary file
  • the service has to return some structure that contains a "success/fail" processing status, an error message if any, and if there was no error - the result file

In a SOAP service I'd just return something like the following:

class ProcessingResult {
public:
    bool IsFailed;
    string ErrorMessage;
    byte[] ResultData;
};

and the middleware would properly serialize this over the wire, then the client proxy (produced by parsing WSDL) would deserialize it and the client would be happy.

How do I desing something similar in a WCF REST service?

You can do the same in REST service. Here is the service contract:

[ServiceContract]
public interface IService
{
    [OperationContract]
    [WebInvoke(Method = "POST")]
    ProcessingResult ProcessData(byte[] data);
}

public class ProcessingResult
{
    public bool IsFailed { get; set; }
    public string ErrorMessage { get; set; }
    public byte[] ResultData { get; set; }
}

All byte arrays will be send as base64 encoded string. Example of request message:

<base64Binary xmlns="http://schemas.microsoft.com/2003/10/Serialization/">QmFzZSA2NCBTdHJlYW0=</base64Binary>

Example of response message:

<ProcessingResult xmlns="http://schemas.datacontract.org/2004/07/RestService">
  <ErrorMessage>String content</ErrorMessage>
  <IsFailed>true</IsFailed>
  <ResultData>QmFzZSA2NCBTdHJlYW0=</ResultData>
</ProcessingResult>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM