【问题标题】:Consuming RESTful WCF with multiple parameters (Json)使用具有多个参数的 RESTful WCF (Json)
【发布时间】:2011-11-04 07:35:50
【问题描述】:

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

请帮忙...

提前致谢!!

【问题讨论】:

  • 我已经使用 WebChannelFactory 解决了我的这个问题。
  • 不错的代码..这看起来确实很好用,而且您可以完全控制正在发生的事情...好东西,感谢您分享您这样做的方式,尤其是上面的通用方法。

标签: c# wcf wcf-client


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2017-05-06
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多