代码改变世界

Silverlight3系列(一)Silverlight配合WCF进行数据库操作

2010-01-20 09:51  Virus-BeautyCode  阅读(6067)  评论(7编辑  收藏  举报

 

 

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" />
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="*" />
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="*"/>
      allow-from>
      <grant-to>
        <resource path="/" include-subpaths="true"/>
      grant-to>
    policy>
  cross-domain-access>
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"/>
      allow-from>
      <grant-to>
        <resource path="/public-services/" include-subpaths="true"/>
      grant-to>
    policy>
  cross-domain-access>
access-policy>

You can find additional information about cross-domain files on msdn.

这两个文件需要放在wcf的根目录,好像网上说如果用IISwcf的宿主,这两个文件可能需要放在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 servicewcfado.net data serviceria service,但是后面的ado.net data service还是CTP版,RIA service还是beta版,都不适合用于实际应用开发,而且后两者也有一个问题,可能在某些方面要好于wcf,但是整体比较他们不如wcf,而且他们在数据库方面只支持MS SQL。在

Silverlight与数据库的三种互操作,中有一个简单的比较。

wcf肯定要比web service要好,wcf是微软新一代的分布式应用通信架构,整合了以前的web service.NET REmotingwseCOM+等等,可以完全使用托管代码编写,可以跨应用、跨机器、跨系统、甚至跨不同的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/下载。