如何将自定义对象传递给WCF服务 [英] How to pass a custom object to WCF service
本文介绍了如何将自定义对象传递给WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我试图通过客户端应用程序将复杂对象传递给WCF服务。
当我调试时代码,我看到对象即将为空。
我尝试了以下代码。
界面方法
Hi All,
I am trying to pass a complex object to WCF service through a Client Application.
When i debug the code, I see object is coming as null.
I tried the following code.
Interface method
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest,
UriTemplate = "Testmethod/JSON/")]
bool TestmethodJson(Complain comp);
服务实施
Service Implementation
public bool TestmethodJson(Complain comp)
{
if (comp != null)
{
//code goes here
return true;
}
else
{
//code goes here
return false;
}
客户端应用程序代码如下 < br $> b $ b
Client application code goes as follows
static void Main(string[] args)
{
Complain comp = new Complain()
{
CompainType = "type1",
CompainBody = "Body1"
};
//JavaScriptSerializer serializer = new JavaScriptSerializer();
string output = JsonConvert.SerializeObject(comp);
//string output = serializer.Serialize(complain);
string strUri = "http://localhost:35798/NavGAT.svc/Testmethod/JSON/";
Uri uri = new Uri(strUri);
WebRequest request = WebRequest.Create(uri);
request.Method = "POST";
request.ContentLength = output.Length;
request.ContentType = "application/json; charset=utf-8";
// JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
string serOut = JsonConvert.SerializeObject(comp);
// string serOut = jsonSerializer.Serialize(complain);
using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write(serOut);
}
WebResponse responce = request.GetResponse();
Stream reader = responce.GetResponseStream();
StreamReader sReader = new StreamReader(reader);
string outResult = sReader.ReadToEnd();
sReader.Close();
}
我可以从客户端调用服务但是对象是null。请让我知道问题所在通过复杂的对象服务。
在此先感谢.. :)
I am able to call a service from client but object comes as null.Please let me know the problem in passing complex object service.
Thanks in Advance.. :)
推荐答案
请确保您拥有带有DataContract属性的Complain实体类声明,并且CompainType和CompainBody必须具有DataMemeber属性。
Please ensure you have Complain entity class declaration with DataContract attribute over it and CompainType and CompainBody must have DataMemeber attribute over it.
using System.Runtime.Serialization;
[DataContract]
public class Complain
{
#region Private Members
private string _CompainBody ;
private string _CompainType ;
#endregion
#region Public Members
[DataMember]
public int CompainBody
{
get { return _CompainBody ; }
set { _CompainBody = value; }
}
[DataMember]
public string CompainType
{
get { return _CompainType ; }
set { _CompainType = value; }
}
}
}
你有BodyStyle作为WrappedRequest,所以你需要做 -
删除
You have BodyStyle as WrappedRequest,so you need to do-
Either remove
BodyStyle = WebMessageBodyStyle.WrappedRequest
或传递你的对象,如
{
"comp": {
"ComplainType": "1",
"ComplainBody": "message"
}
}
这就是你如何在对象中看到值。
That's how you can see values in in object.
这篇关于如何将自定义对象传递给WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文