使用带多个参数(Json)的RESTful WCF

本文关键字:Json RESTful WCF 参数 | 更新日期: 2023-09-27 18:06:38

我正在使用我创建的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和一个字符串参数。

请帮…

提前感谢!!

使用带多个参数(Json)的RESTful WCF

我已经在客户端使用WebChannelFactory解决了这个问题。
访问这里的链接!