简体   繁体   English

WCF自定义数据类型为OperationContract参数

[英]WCF custom datatype as OperationContract parameter

I'm very new to WCF and have a question that I hope you can help me with. 我对WCF很新,并且有一个问题,我希望你可以帮助我。

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. 项目 :我被要求创建一个WCF服务,允许客户端上传word文件和一些元数据。

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: 客户端没有他们将要进行的POST调用的示例,因此我无法从该WSDL创建类,但该帖子将包含如下数据:

{
    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 ....). 我正在考虑创建一个[OperationContract] ,如bool UploadFile( CustomDocument inputDocument)而不是bool UploadFile(字符串作者,字符串编码....)。

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? 我的问题 :如果我使用自定义对象作为[OperationContract]的输入参数( CustomDocument ),客户端是否能够在其服务调用中将所有信息作为字符串,int等传递,或者他们是否必须首先创建实例CustomDocument的结尾,然后在帖子中包含该对象?

Sorry, I'm very new to WCF, my apologies in advance if this question doesn't make any sense; 对不起,我对WCF很新,如果这个问题没有任何意义,我提前道歉; 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. 您必须确保CustomDocument是Serializable对象并具有公共无参数构造函数。 The easiest way is share the dll that contains the class CustomDocument between the WebService and the Application that will use it. 最简单的方法是共享包含WebService和将使用它的Application之间的类CustomDocument的dll。 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. 但是当我尝试将复杂对象发送到WebServce时,我更喜欢将其序列化为字节数组,然后在WebService中反序列化。

Good luck! 祝好运!

You don't need the custom object CustomDocument . 您不需要自定义对象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). PS:您需要使用webHttpBinding (如果它不在IIS中托管,则使用WebServiceHost )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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