使用多个参数消耗WCF REST服务WITHOUT DataContract
问题描述:
我需要使用POST方法调用带有多个参数的WCF REST服务,但由于我需要简单类型,因此无法创建包含我的参数的DataContract:我的webservice将被客观的C应用程序。使用多个参数消耗WCF REST服务WITHOUT DataContract
我发现在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库,该库可让您将JSON数据创建(并解析)为树状结构,而无需将它们转换为现有的类。其中两个是JSON.NET项目(在下面的示例代码中使用)或System.Json库(可以通过NuGet下载)。还有许多用于非.NET语言的JSON库。
你可以做的另一件事使你的生活更简单,就是将操作的主体风格从Wrapped(包装响应)变成Wrapped到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<bool>();
Console.WriteLine(success);
Console.Write("Press ENTER to close the host");
Console.ReadLine();
host.Close();
}
}
使用数据契约不需要任何特殊的客户端。你的ObjC客户端有什么特殊之处可以让你的情况有所不同? – 2012-03-09 13:55:34