Silverlight3系列(一)Silverlight配合WCF进行数据库操作
?
?
1、環境配置
本文的Silverlight版本為Silverlight3,具體的配置過程可以參考:http://www.cnblogs.com/psunny/archive/2009/08/31/1556976.html或者http://www.cnblogs.com/wheeloffate/archive/2009/08/25/1553973.html
如果希望破解expression studio 3的話,可以在google中搜索【expression studio 3 破解】,也就是將下載的dll替換安裝目錄中的原文件,替換之前別忘了備份原文件,以防意外。
2、注意事項
2.1 跨域訪問
跨域指的是當WCF的宿主和Silverlight得宿主不在同一個域中的時候,默認是不可以訪問的。這里的域指的是應用程序的域,詳細介紹可以參看:.net中應用程序域的概念。
主要是指的wcf默認不支持跨域訪問(當你在單獨一個項目中),需要兩個xml文件來聲明wcf是可以跨域訪問的。
下面的跨域支持來自:http://www.silverlightshow.net/items/WCF-Integration-in-Silverlight-2-Beta-1.aspx
Cross-Domain Support
If you want to enable your service to work cross-domain, there are two files you will want to concern yourself with:
crossdomain.xml
This file comes from back in the macromedia flash days. Since it has wide support on the internet, it makes sense for Silverlight to support it. You can read some documentation on this file format here.
One use supported by the specification is to specify domains which can access the service:
xml version="1.0"?> DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"> <cross-domain-policy><allow-access-from domain="www.yoursite.com" /><allow-access-from domain="yoursite.com" /><allow-access-from domain="*.moock.org" /> < span>cross-domain-policy>?
However, that detailed format is not supported by Silverlight. Instead, you must opt in to all domains if you want to use the crossdomain.xml approach. Such a policy file would look like this:
xml version="1.0"?> DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"> <cross-domain-policy><allow-access-from domain="*" /> < span>cross-domain-policy>However, since this format doesn't cover all the scenarios that Silverlight developers are likely interested in (and flash developers too, most likely), Microsoft has provided another cross-domain file that is more flexible and currently Silverlight-specific: clientaccesspolicy.xml.
clientaccesspolicy.xml
lf you want finer control over your cross-domain policy, you'll want to use clientaccesspolicy.xml. This file allows you to specify both what domains have access to your service, and also what subpaths are covered by the policy. You can include both clientaccesspolicy.xml and crossdomain.xml for your services; Silverlight will check this file before looking for the crossdomain.xml
The equivalent to the "allow all domains access to all services" is below:
xml version="1.0" encoding="utf-8"?> <access-policy><cross-domain-access><policy><allow-from><domain uri="*"/>< span>allow-from><grant-to><resource path="/" include-subpaths="true"/>< span>grant-to>< span>policy>< span>cross-domain-access> < span>access-policy>If you want to get more selective about what you enable, you can specify external domains and subfolders on your site:
xml version="1.0" encoding="utf-8"?> <access-policy><cross-domain-access><policy><allow-from><domain uri="http://contoso.com"/>< span>allow-from><grant-to><resource path="/public-services/" include-subpaths="true"/>< span>grant-to>< span>policy>< span>cross-domain-access> < span>access-policy>You can find additional information about cross-domain files on msdn.
這兩個文件需要放在wcf的根目錄,好像網上說如果用IIS做wcf的宿主,這兩個文件可能需要放在www的根目錄interpub里面。
2.2 Silverlight部署
http://www.cnblogs.com/ibillguo/archive/2008/08/31/1280418.html
http://www.cnblogs.com/kvspas/archive/2009/04/25/silverlight3_install_bug.html
http://www.pin5i.com/showtopic-16755.html
http://blog.csdn.net/net_lover/archive/2008/04/06/2255074.aspx
? 這里的部署指的是sl3部署,宿主是IIS網站,ASP.NET網站,將網站發布到IIS中,設置網站的屬性,設置ASP.NET屬性版本為2.0,在HTTP頭中添加兩種MIME類型,.xaml application/xaml+xml和.xap application/x-silverlight-app,就可以了。
2.3 wcf部署
http://msdn.microsoft.com/zh-cn/library/aa751792.aspx
http://social.microsoft.com/Forums/zh-CN/wcfzhchs/thread/10358039-fa88-494e-a382-ba1b70a2b866
http://www.cnblogs.com/pdfw/archive/2007/12/25/1014293.html
http://www.cnblogs.com/GSonOVB/archive/2009/08/13/1545648.html
http://msdn.microsoft.com/zh-cn/library/ms733766.aspx
? 這里的部署,以IIS網站為宿主,將wcf發布到一個IIS網站,在網站的根目錄添加上面提到的兩個文件CrossDomain.xml和ClientAccessPolicy.xml,甚至要添加到www的根目錄【C:\Inetpub\wwwroot】。
2.4 Silverlight的數據庫操作
目前有四種:web service、wcf、ado.net data service和ria service,但是后面的ado.net data service還是CTP版,RIA service還是beta版,都不適合用于實際應用開發,而且后兩者也有一個問題,可能在某些方面要好于wcf,但是整體比較他們不如wcf,而且他們在數據庫方面只支持MS SQL。在
Silverlight與數據庫的三種互操作,中有一個簡單的比較。
wcf肯定要比web service要好,wcf是微軟新一代的分布式應用通信架構,整合了以前的web service、.NET REmoting、wse、COM+等等,可以完全使用托管代碼編寫,可以跨應用、跨機器、跨系統、甚至跨不同的OS都可以通信,只要他們遵守相同的交互標準。
?
2.5 調試wcf程序
?
WCF多層結構的開發,與往常的單程序調試有所不同,總是由于通信導致調試的無法進行,搜了一下互聯網,找到了這個辦法,測試后發現可行。
在WCF的HOST(寄主)所在項目中右鍵->調試->啟動新實例,開始調試服務端。
在客戶端所在項目中右鍵->調試->啟動新實例,開始調試客戶端。
這樣的話,不管在同一個解決方案中還是不同解決方案中都可以進行WCF的調試。
?2.6 其他
wcf如果進行了修改,修改了代碼,就需要重新編譯,在引用wcf的客戶端,也需要更新wcf的服務引用,否則新的wcf不會被使用,你使用的還是舊的wcf。
silverlight修改了代碼,也要重新編譯一遍,否則,web項目還是舊的Silverlight類庫,所以還是出不來效果。
3、樣例代碼
http://silverlightwcf.codeplex.com/
數據庫SQL
數據庫SQL create?database?slwcf
go
USE?[slwcf]
GO
/******?對象:??Table?[dbo].[Customer]????腳本日期:?01/20/2010?13:14:09?******/
SET?ANSI_NULLS?ON
GO
SET?QUOTED_IDENTIFIER?ON
GO
CREATE?TABLE?[dbo].[Customer](
????[CustomerId]?[int]?NULL,
????[CustomerCode]?[nvarchar](50)?COLLATE?Chinese_PRC_CI_AS?NULL,
????[CustomerName]?[nvarchar](50)?COLLATE?Chinese_PRC_CI_AS?NULL
)?ON?[PRIMARY]
go
insert?into?Customer(CustomerId,CustomerCode,CustomerName)
values(1,'sssss','ssssss')
insert?into?Customer(CustomerId,CustomerCode,CustomerName)
values(2,'ddddd','ddddd')
insert?into?Customer(CustomerId,CustomerCode,CustomerName)
values(3,'eeeee','eeeeee')
insert?into?Customer(CustomerId,CustomerCode,CustomerName)
values(4,'ffffff','ffffff')
insert?into?Customer(CustomerId,CustomerCode,CustomerName)
values(5,'aaaaa','aaaaa')
go
?
功能就是根據條件查找單個數據,grid顯示全部數據,添加數據,wcf的數據訪問層使用了NHibernate2.1.2。
?
4、應用架構圖
這次的應用有一部分的客戶端為了增加交互性,提高體驗,使用了Silverlight3,數據庫交互部分使用的是WCF,有一部分后臺管理還是用ASP.NET頁面來做,數據庫交互就直接使用ADO.NET,下面是我的一張程序架構圖,希望大家可以指出問題。
Technorati 標簽: Silverlight,wcf,數據庫交互,開發環境配置,跨域訪問??
?
?
WCF目前知道的不需要配置證書的情況;
1.Security Mode為None,也就是不啟用安全,這里不需要證書。
2.NetTcpBinding 在使用Windows安全驗證的時候,不需要證書,安全由Windows Domain提供。
3.如果不想使用證書,可以在basicHttpBinding下使用TransportCredentialOnly安全模式,這里客戶端憑據信息都是明文傳遞的。
?
代碼可以在http://silverlightwcf.codeplex.com/下載。
總結
以上是生活随笔為你收集整理的Silverlight3系列(一)Silverlight配合WCF进行数据库操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux经典实用的使用技巧33则和常用
- 下一篇: 使用T-SQL配置数据库事务日志传送