开发者

Consuming RESTful WCF with multiple parameters (Json)

I am in a process of consuming the RESTful WCF that I've created. I was able to consume the service with all the method (GET/PUT/DELETE/POST) with multiple parameters. These are some of the methods that I was able to consume at the client side:

[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);  

And these are my code at the client side to consume the methods given above:

This is my collection.

public class Employee  
{ 
    public int Id { get; set; }  
    public string FirstName { get; set; }  
    public string LastName { get; set; }  
}  

And these are the method to consume the WCF REST method.

static void ConsumeWcfRestPostMethod()  
{  
    var listEmployee = new List<Employee>();  
    listEmployee.Add(new Employee { Id = 1, FirstName = "Eireen", LastName = "Kim" });  
    var paramConte开发者_高级运维nt = 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;  
}  

Now my question is. How can I consume the method below???

[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);  

This method has 2 List and a string parameters..

Please HELP...

Thanks in advance!!


I already solved this problem of mine using WebChannelFactory on the clientside..
visit the link here!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜