简体   繁体   中英

WCF custom datatype as OperationContract parameter

I'm very new to WCF and have a question that I hope you can help me with.

Project : I've been asked to create a WCF service that allows a client to be able to upload a word file along with some metadata.

The client doesn't have a sample of the POST call they'll be making so I can't create a class off of that WSDL, but the post would contain data like this:

{
    author: 'John Doe',
    pages: '32',
    size: '14432',
    authToken: '322222222233',
    encoding: 'binary'
    name: 'Document1.doc'
}

I'm thinking of creating an [OperationContract] such as bool UploadFile( CustomDocument inputDocument) instead of bool UploadFile (string author, string encoding ....).

My question : If I use a custom object as an input parameter ( CustomDocument ) for an [OperationContract] would the client be able to pass all the information as string, int etc in its service call, or would they have to first create an instance of CustomDocument on their end, and then include that object in the post?

Sorry, I'm very new to WCF, my apologies in advance if this question doesn't make any sense; I'll update it based on your feedback.

You have to make sure that CustomDocument is a Serializable object and have a public parameterless constructor. The easiest way is share the dll that contains the class CustomDocument between the WebService and the Application that will use it. But personally when I try to send a complex object to a WebServce I prefer to serialize as a byte array and then Deserialize inside the WebService.

Good luck!

You don't need the custom object CustomDocument . Suppose you have this service

[ServiceContract]
public interface IMyTestServce
{
    [OperationContract]
    [WebInvoke(Method = "POST", 
       BodyStyle = WebMessageBodyStyle.Wrapped,
       UriTemplate = "/Upload?author={author}&pages={pages}&size={size}&name={name}&authToken={authToken}")]
    void Upload(string author, int pages, long size, string name, 
                string authToken, 
                Stream file);
}

public class MyTestService : IMyTestServce
{
    public void Upload(string author, int pages, long size, string name, 
                       string authToken, 
                       Stream file)
    {
        Console.WriteLine(String.Format("author={0}&pages={1}&size={2}&name={3}&authToken={4}", author, pages, size, name, authToken));
        Console.WriteLine(new StreamReader(file).ReadToEnd());
    }
}

You can easily call it like

HttpClient client = new HttpClient();
var content = new StreamContent(File.OpenRead(filename);
await client.PostAsync("http://localhost:8088/Upload?author=aa&pages=3&name=bb&authToken=112233", content);

PS: You need to use webHttpBinding (or WebServiceHost if it is not hosted in IIS).

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