终于开始正式学习silverlight,虽然有点晚,但总算开始了,今天看了一下sdk,主要是想看下silverlight中如何动态调用数据,对于数据库的访问,sdk中的官方示例多用的是Ado.Net数据服务,方便到是方便,就是感觉Ado.Net数据服务返回的xml太啰嗦了,一点也不精简,数据通讯量太大(N多无用的标签导致客户端与服务端之间传输数据增加了不少),于是想到能否直接用wcf返回json格式,
何况sdk中提到silverlight中System.Json已经完成了对json的解析
经实验,用WebClient貌似就能解析一切,包括加载json文本,纯文本,以及wcf,不过要注意的是:xap与被调用的txt或wcf必须在同一个域下,否则将调用失败,具体看下面的代码
Page.Xaml完整内容如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Page.Xaml.cs完整内容如下:
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/0be121fa5b8988fbabbbc526af3b0fc0.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
using System;
using System.Json;
using System.Net;
using System.Windows.Controls;
using System.IO;
namespace WcfTest
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
LoadJsonTxt();
LoadBlankTxt();
LoadWcfTxt();
}
/// <summary>
/// 加载Json格式的文本文件
/// </summary>
void LoadJsonTxt()
{
Uri serviceUri = new Uri("http://localhost:7055/data/json.txt");//很奇怪,只能用绝对路径
WebClient downloader = new WebClient();
downloader.OpenReadCompleted += new OpenReadCompletedEventHandler(LoadJsonTxtCompleted);
downloader.OpenReadAsync(serviceUri);
}
/// <summary>
/// 异常回调函数
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void LoadJsonTxtCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null)
{
JsonArray _data = (JsonArray)JsonArray.Load(e.Result);
string _Result = "";
for (int i = 0; i < _data.Count; i++)
{
_Result += (",name=" + _data[i]["name"].ToString());
}
if (_Result.StartsWith(","))
{
_Result = _Result.Substring(1);
}
this.txtJson.Text = _Result;
}
else
{
txtJson.Text = "出错:" + e.Error.Message.ToString();
}
}
/// <summary>
/// 加载普通格式的文本文件
/// </summary>
void LoadBlankTxt()
{
Uri serviceUri = new Uri("http://localhost:7055/data/txt.txt");
WebClient downloader = new WebClient();
downloader.OpenReadCompleted += new OpenReadCompletedEventHandler(LoadBlankTxtCompleted);
downloader.OpenReadAsync(serviceUri);
}
void LoadBlankTxtCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null)
{
StreamReader _Reader = new StreamReader(e.Result);
this.txtBlank.Text = _Reader.ReadToEnd();
}
else
{
txtBlank.Text = "出错:" + e.Error.Message.ToString();
}
}
/// <summary>
/// 加载wcf返回的文本
/// </summary>
void LoadWcfTxt()
{
Uri serviceUri = new Uri("http://localhost:7055/Demo.svc/GetData?callBack=123");
WebClient downloader = new WebClient();
downloader.OpenReadCompleted += new OpenReadCompletedEventHandler(LoadWcfCompleted);
downloader.OpenReadAsync(serviceUri);
}
void LoadWcfCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null)
{
StreamReader _Reader = new StreamReader(e.Result);
this.txtWcf.Text = _Reader.ReadToEnd();
}
else
{
txtWcf.Text = "出错:" + e.Error.Message.ToString();
}
}
}
}
wcf的代码也在这里贴出来,就是一"启用了ajax的wcf服务",没啥特别的,下面的代码仅参考(从NorthWind数据库中,读取了表Categories的部分信息)
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/0be121fa5b8988fbabbbc526af3b0fc0.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using JIMMY.TOOLS;
namespace WcfTest.Web
{
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Demo
{
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "GetData?callback={callback}")]
public Stream GetData(string callback)
{
DataTable _Table = new DataTable();
string _Result = "";
using (SqlConnection conn = Database.GetConn(ConfigurationManager.ConnectionStrings["ConnStr"].ToString()))
{
try
{
string sql = "Select CategoryId,CategoryName,Description from Categories";
SqlDataReader sdr = Database.ExecuteReader(conn, CommandType.Text, sql);
_Table = Database.ConvertDataReaderToDataTable(sdr);
}
catch { }
finally { conn.Close(); }
}
if (_Table.Rows.Count <= 0)
{
_Result = "var _" + callback + "={Head:[]}";
}
else
{
_Result = "var _" + callback + "=" + Utils.CreateJsonParameters(_Table);
}
return GetStream(_Result);
}
private Stream GetStream(string str)
{
MemoryStream ms = new MemoryStream();
StreamWriter sw = new StreamWriter(ms);
sw.AutoFlush = true;
sw.Write(str);
ms.Position = 0;
WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
return ms;
}
}
}
最后附上:txt.txt以及json.txt二个测试文本的内容
json.txt内容:
[{'name':"jimmy.yang"},{'name':'yezhouyun'},{'name':123456}]
txt.txt内容:
要做到这两点只需要更改一下所示的高两部分即可。第一个高两部分表示我们将NorthwindEntities作为我们的数据源。NorthwindEntities是我们刚才创建的EDMX的类名,Employees等表都已经作为其属性被Mapping成对象。第二个高亮部分是控制EntitySet(其实就是对应的表一级)的访问权限。例如你仍然可以通过下边的代码仅仅暴露Employees对象的只读权限:config.SetEntityAccessRule(“Employees”,EntitySetRights.AllRead.这样,服务只会暴露Employees集合并且只接受读取,而不能有更新操作。
运行效果图如下: