我正在使用我创建的RESTful WCF。我能够使用具有多个参数的所有方法(GET / PUT / DELETE / POST)来使用该服务。这些是我能够在客户端使用的一些方法:
[WebInvoke(BodyStyle = WebMessageBodyStyle.Bare, Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, UriTemplate = "GetEmployeePost")]
[OperationContract]
string GetEmployeePost(List<Employee> listEmployee);
[WebInvoke(BodyStyle = WebMessageBodyStyle.Bare, Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, UriTemplate = "GetEmployeePost/{userId}")]
[OperationContract]
string GetEmployeePost2(List<Employee> listEmployee, string userId);
这些是客户端的代码,用于使用上面给出的方法:
这是我的收藏。
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
这些是使用WCF REST方法的方法。
static void ConsumeWcfRestPostMethod()
{
var listEmployee = new List<Employee>();
listEmployee.Add(new Employee { Id = 1, FirstName = "Eireen", LastName = "Kim" });
var paramContent = Serialize(listEmployee);
var result = PostMethod(_baseAddress + "GetEmployeePost", paramContent, "POST");
Console.WriteLine(result);
Console.ReadLine();
}
public static string Serialize<T>(T obj)
{
var serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType());
var ms = new MemoryStream();
serializer.WriteObject(ms, obj);
string retVal = Encoding.Default.GetString(ms.ToArray());
ms.Dispose();
return retVal;
}
public static T Deserialize<T>(string json)
{
var obj = Activator.CreateInstance<T>();
var ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
var serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType());
obj = (T)serializer.ReadObject(ms);
ms.Close();
ms.Dispose();
return obj;
}
static string PostMethod(string url, string msg, string method)
{
string result = string.Empty;
byte[] buffer = Encoding.UTF8.GetBytes(msg);
var myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
myHttpWebRequest.Method = method;
myHttpWebRequest.ContentType = "application/json";
myHttpWebRequest.ContentLength = buffer.Length;
using (var request = myHttpWebRequest.GetRequestStream())
{
request.Write(buffer, 0, buffer.Length);
request.Close();
}
var myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
using (var reader = new StreamReader(myHttpWebResponse.GetResponseStream(), Encoding.UTF8))
{
result = reader.ReadToEnd();
myHttpWebResponse.Close();
}
return result;
}
现在我的问题是。我该如何使用下面的方法???
[WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest, Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, UriTemplate = "GetEmployeePost3/{userId}")]
[OperationContract]
string GetEmployeePost3(List<Employee> listEmployee, List<EmployeeDetail> listEmployeeDetail, string userId);
此方法有2个List和一个字符串参数..
请帮助......
提前致谢!!