在WCF服务中通过JSON发送数据 [英] Send Data over JSON in WCF Service

查看:450
本文介绍了在WCF服务中通过JSON发送数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我需要通过JSON将大量数据从WFC发送到消费者(在我的应用程序中,我使用的是Windows手机智能设备)。



代码我是使用是,



 [ServiceContract] 
公共接口ISampleService
{
[OperationContract]
[WebInvoke(Method =POST,ResponseFormat = WebMessageFormat.Xml,BodyStyle = WebMessageBodyStyle.WrappedRequest,UriTemplate =GetEmployeeData / {userName})]
DataSet GetEmployeeData(string userName);

}





  public  DataSet GetEmployeeData(string userName)
{

DataSet ds = dbServices.GetEmployeeData(userName);
if (ds.Tables [ 0 ]。Rows.Count> 0
{
return ds;
}
else
return ;
}





我收到消费者(windows智能设备应用程序)中的数据



 HttpWebRequest webrequest =(HttpWebRequest)WebRequest.Create(url +GetEmployeeData+/+manikandan); 
webrequest.Method =POST;
webrequest.ContentType =application / xml;
webrequest.ContentLength = 0;
Stream stream = webrequest.GetRequestStream();
stream.Close();
字符串结果;
using(WebResponse response = webrequest.GetResponse())
{
using(StreamReader reader = new StreamReader(response.GetResponseStream()))
{
result = reader.ReadToEnd();
}
}
DataSet ds = new DataSet();
XmlElement exelement;
XmlDocument doc = new XmlDocument();
doc.LoadXml(result);
exelement = doc.DocumentElement;
if(exelement!= null)
{
XmlNodeReader nodereader = new XmlNodeReader(exelement);
ds.ReadXml(nodereader,XmlReadMode.Auto);
}
lblCount.Text = ds.Tables [0] .Rows.Count.ToString();
lblds.Text = ds.Tables [0] .Rows [0] [1] .ToString();
dataGrid1.DataSource = ds;



当我做了以上所有操作时,windows mobile中的datagrid没有加载数据。

我检查了数据集,

 string val = ds.Tables [0] .Rows [0] [1] .ToString(); 





它收到数据。现在任何人都可以告诉我如何将我的ds绑定到windows smartdevice移动应用程序中的datagrid。

提前谢谢。

解决方案

 使用 代码的行。 


datagrid1.DataSource = ds.Tables [ 0 ];


标记为 answer 如果就足以满足您的答案..


Hi everyone I need to send large data from WFC to consumer(in my application i am using windows phone smart device) through JSON.

the code i am using is,

[ServiceContract]
   public interface ISampleService
   {
       [OperationContract]
       [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.WrappedRequest, UriTemplate = "GetEmployeeData/{userName}")]
       DataSet GetEmployeeData(string userName);

    }



public DataSet GetEmployeeData(string userName)
       {

           DataSet ds = dbServices.GetEmployeeData(userName);
           if (ds.Tables[0].Rows.Count > 0)
           {
               return ds;
           }
           else
           return null;
       }



and i receive the data in consumer(windows smart device app) as

HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url + "GetEmployeeData" + "/" + "manikandan");
                webrequest.Method = "POST";
                webrequest.ContentType = "application/xml";
                webrequest.ContentLength = 0;
                Stream stream = webrequest.GetRequestStream();
                stream.Close();
                string result;
                using (WebResponse response = webrequest.GetResponse())
                {
                    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                    {
                        result = reader.ReadToEnd();
                    }
                }
                DataSet ds = new DataSet();
                XmlElement exelement;
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(result);
                exelement = doc.DocumentElement;
                 if (exelement != null)
                 {  
                     XmlNodeReader nodereader = new XmlNodeReader(exelement);
                     ds.ReadXml(nodereader, XmlReadMode.Auto);
                 }
                 lblCount.Text = ds.Tables[0].Rows.Count.ToString();
                 lblds.Text = ds.Tables[0].Rows[0][1].ToString();
                 dataGrid1.DataSource = ds;


when i did all the above the datagrid in the windows mobile is not loaded with the data.
I checked the dataset, by

string val = ds.Tables[0].Rows[0][1].ToString();



it receives the data. now can anybody tell me how to bind my ds to the datagrid in windows smartdevice mobile application.
Thanks in advance.

解决方案

Use Followoing line of code.


datagrid1.DataSource = ds.Tables[0];


Mark as answer if it suffice your answer...


这篇关于在WCF服务中通过JSON发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆