WCF作为服务端,目前是寄宿在IIS,客户端是普通的winform,就想简单的做一个“发送请求”、“返回结果”的功能。 但是服务端显示参数一直是null。
用的json格式。
WCF接口代码:
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "GetConnection", ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
ResultInfo GetConnection(KeyValuePair data);
接口实现:
public ResultInfo GetConnection(KeyValuePair<string, object> data)
{
//这里监视到的data就是null
return new ResultInfo(){IsSuccessed = true,}; //返回值是自己定义的一个类
}
客户端调用:
public static string SendHttpRequest(string requestURI, string requestMethod, string json,int timeOut = 300000)
{
//json格式请求数据
string requestData = json;
//拼接URL
string serviceUrl = string.Format("{0}/{1}", requestURI, requestMethod);
HttpWebRequest myRequest = null;
myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl);
//utf-8编码
byte[] buf = Encoding.GetEncoding("UTF-8").GetBytes(requestData);
//post请求
myRequest.Method = "POST";
myRequest.ContentLength = buf.Length;
myRequest.ContentType = "application/json";
myRequest.MaximumAutomaticRedirections = 1;
myRequest.AllowAutoRedirect = true;
myRequest.Timeout = timeOut;
Stream newStream = myRequest.GetRequestStream();
newStream.Write(buf, 0, buf.Length);
newStream.Close();
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
string ReqResult = reader.ReadToEnd();
reader.Close();
myResponse.Close();
return ReqResult;
}
传入的json在客户端看是正常的: Key是Guid,Value是一个自己定义的类型
{"Key":"6aafd4a3-6d9d-4bab-adb6-f5fe718437c1","Value":{"ComputerName":"Desk-Top","IpAddress":"192.169.6.126","Mac":null}}
也查了不少资料,可能悟性有限,一直没能解决,头大。
试过加上数据契约 比如
[DataContract]
public class PcInfo
{
[DataMember]
public string ComputerName{set;get;}.......
}
服务端接受的一直是null。
求大牛不吝赐教。。小弟感激不尽