使用多个参数WITHOUT DataContract来使用WCF REST服务

我需要使用POST方法使用多个参数调用我的WCF REST服务,但是我无法创建包含我的参数的DataContract,因为我需要简单的类型:我的webservice将由目标C应用程序使用。

我在MSDN网站上找到了这种语法:

[OperationContract] [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "savejson?id={id}&fichier={fichier}")] bool SaveJSONData(string id, string fichier); 

为了快速解释上下文,我必须调用此方法来保存带有在数据库上传递的Id的JSON文件。

我的问题是:是否真的可以将几个参数传递给POST方法,如前所示?

其次:如何使用多个参数来使用我的服务(暂时使用C#,只是为了测试它)?

我已经使用DataContract进行了测试,我就是这样做的:

 string url = "http://localhost:62240/iECVService.svc/savejson"; WebClient webClient = new WebClient(); webClient.Headers["Content-type"] = "application/json; charset=utf-8"; RequestData reqData = new RequestData { IdFichier = "15", Fichier = System.IO.File.ReadAllText(@"C:\Dev\iECV\iECVMvcApplication\Content\fichier.json") }; MemoryStream requestMs = new MemoryStream(); DataContractJsonSerializer requestSerializer = new DataContractJsonSerializer(typeof(RequestData)); requestSerializer.WriteObject(requestMs, reqData); byte[] responseData = webClient.UploadData(url, "POST", requestMs.ToArray()); MemoryStream responseMs = new MemoryStream(responseData); DataContractJsonSerializer responseSerializer = new DataContractJsonSerializer(typeof(ResponseData)); ResponseData resData = responseSerializer.ReadObject(responseMs) as ResponseData; 

RequestData和ResponseData以这种方式声明:

 [DataContract(Namespace = "")] public class RequestData { [DataMember] public string IdFichier { get; set; } [DataMember] public string Fichier { get; set; } } [DataContract] public class ResponseData { [DataMember] public bool Succes { get; set; } } 

但正如我所说,我不能再这样做了……

我希望我足够清楚,如果没有,请不要犹豫,问我细节!

非常感谢你的帮助。

您可以采取一些措施来避免使用数据合同。 其中最简单的方法是使用JSON DOM库,它允许您以树forms创建(和解析)JSON数据,而无需将它们转换为现有类。 其中两个是JSON.NET项目(在下面的示例代码中使用)或System.Json库(可以通过NuGet下载)。 非.NET语言也有许多JSON库。

您可以做的另一件事是让您的生活更简单,就是将操作的体型从Wrapped(包装响应)更改为Wrapped to WrappedRequest。 需要包装请求,因为您有两个输入,但响应没有,因此您可以消除一个步骤。

 public class Post_182e5e41_4625_4190_8a4d_4d4b13d131cb { [ServiceContract] public class Service { [OperationContract] [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest, UriTemplate = "savejson")] public bool SaveJSONData(string id, string fichier) { return true; } } public static void Test() { string baseAddress = "http://" + Environment.MachineName + ":8000/Service"; WebServiceHost host = new WebServiceHost(typeof(Service), new Uri(baseAddress)); host.Open(); Console.WriteLine("Host opened"); JObject json = new JObject(); json.Add("id", JToken.FromObject("15")); json.Add("Fichier", "the file contents"); //System.IO.File.ReadAllText(@"C:\Dev\iECV\iECVMvcApplication\Content\fichier.json")); WebClient c = new WebClient(); c.Headers[HttpRequestHeader.ContentType] = "application/json"; string result = c.UploadString(baseAddress + "/savejson", json.ToString(Newtonsoft.Json.Formatting.None, null)); JToken response = JToken.Parse(result); bool success = response.ToObject(); Console.WriteLine(success); Console.Write("Press ENTER to close the host"); Console.ReadLine(); host.Close(); } }