Silverlight4学习笔记2--WCF的IIS发布

本文主要讲述WCF的IIS发布及Silverlight调用

环境:VS2010,SL4.0 , WCF , IIS6.0,win2003 server

1、首先新建Silverlight应用程序SLAppIIS,对应宿主WEB项目名称SLAppIIS.Web:

Silverlight4学习笔记2--WCF的IIS发布_第1张图片

2、在生成的解决方案中点击右键,添加-新建项目,选择WCF-WCF服务应用程序,命名为WcfServiceTest,确定。

生成的项目为WCF服务的实现层。

3、在生成的解决方案中点击右键,添加-新建网站,选择Visual C#-WCF服务,命名为WCFServiceHost,确定。

生成的项目将作为服务的宿主,在IIS发布。

4、目前的解决方案中,由四部分组成

Silverlight4学习笔记2--WCF的IIS发布_第2张图片

5、准备工作就绪,开始一步步实现发布。我们将使用WCF默认添加的GetData函数在Silverlight中进行测试。

6、删除WCFServiceHost网站App_Code目录下的两个默认生成的对我们本次实例来说无用的文件。然后添加对WcfServiceTest的引用。

7、现在,打开WCFServiceHost中的Service.svc文件,可以看到如下一句代码:

 

  
  
< %@ ServiceHost Language ="C#" Debug ="true" Service ="Service" CodeBehind ="~/App_Code/Service.cs" % >

 

其中的Service属性就是我们刚才残忍地删掉的类,因为我们将替换为WcfServiceTest项目中的服务。

将Service属性修改为:WcfServiceTest.Service1,同时删除CodeBehind属性部分,处理后的代码如下:

 

  
  
< %@ ServiceHost Language ="C#" Debug ="true" Service ="WcfServiceTest.Service1" % >

 

 

8、生成WCFServiceHost项目,右键点击Service.svc文件,在浏览器中查看,可以看到服务已经成功寄宿与宿主网站中:

Silverlight4学习笔记2--WCF的IIS发布_第3张图片

9、右键点击WCFServiceHost项目,选择发布网站,默认路径为***\SLAppIIS\PrecompiledWeb\WCFServiceHost,点击确定,发布成功,找到WCFServiceHost目录,在文件夹上点右键,选择共享和安全-WEB共享,选择“共享文件夹”,弹出的对话框中点击确定,将网站发布到IIS中。

10、打开IIS,新建应用程序池,命名为AppPoolSLTest。

11、在默认网站中找到WCFServiceHost,点击右键,选择属性,将应用程序池设置为AppPoolSLTest,将ASP.NET版本设置为4.0,点击应用,弹出关于重新启动W3SVC服务的对话框,直接确定。然后在目录安全性、身份认证中设置为启用匿名访问,设置完成。

12、测试一下,在IIS中,点击Service.svc文件,选择浏览,弹出网页:http://localhost/WCFServiceHost/Service.svc,显示效果和在vs中浏览相同。

13、下面在Silverlight中使用发布的服务。右键点击SLAppIIS,选择“添加服务引用”,命名空间设置为ServiceReferenceTest,地址中输入上面的网址,点击“前往”,“确定”,在Service References目录下生成服务代理,并自动添加相关引用。

Silverlight4学习笔记2--WCF的IIS发布_第4张图片

14、在MainPage.xaml页面中添加一个TextBlock,命名为txbTest:

 

代码
   
   
< UserControl x:Class ="SLAppIIS.MainPage"
xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d
="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc
="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable
="d"
d:DesignHeight
="300" d:DesignWidth ="400" >

< Grid x:Name ="LayoutRoot" Background ="White" >
< TextBlock Height ="23" HorizontalAlignment ="Left" Margin ="10,10,0,0" Name ="txbTest" Text ="TextBlock" VerticalAlignment ="Top" />
</ Grid >
</ UserControl >

15、打开MainPage对应的代码文件,增加对服务的测试代码:

 

代码
   
   
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SLAppIIS
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();

ServiceReferenceTest.Service1Client sc
= new ServiceReferenceTest.Service1Client();
sc.GetDataCompleted
+= new EventHandler < ServiceReferenceTest.GetDataCompletedEventArgs > (sc_GetDataCompleted);
sc.GetDataAsync(
123 );

}

void sc_GetDataCompleted( object sender, ServiceReferenceTest.GetDataCompletedEventArgs e)
{
// throw new NotImplementedException();
txbTest.Text = e.Result;
}
}
}

16、生成SLAppIIS项目,在SLAppIIS.Web中,右键点击SLAppIISTestPage.aspx页面,浏览,很遗憾,有错误:

Silverlight4学习笔记2--WCF的IIS发布_第5张图片

17、别慌,这个是因为跨域策略造成,需要在IIS根目录中添加跨域策略的文件clientaccesspolicy.xml,内容如下:

 

代码
   
   
<? xml version="1.0" encoding="utf-8" ?>
-
< access-policy >
-
< cross-domain-access >
-
< policy >
-
< allow-from http-request-headers ="*" >
< domain uri ="*" />
</ allow-from >
-
< grant-to >
< resource path ="/" include-subpaths ="true" />
</ grant-to >
</ policy >
</ cross-domain-access >
</ access-policy >

 默认网站的根目录可以在属性-主目录中查询:

Silverlight4学习笔记2--WCF的IIS发布_第6张图片

18、刷新页面,数据服务已经可以正常使用:

Silverlight4学习笔记2--WCF的IIS发布_第7张图片

19、注意,如果在调试环境下访问WCF服务,即服务引用地址设置为ASP.NET Development Server下的带有端口的服务,需要在WCFServiceHost根目录下添加跨域策略文件方可正常访问。

 

你可能感兴趣的:(silverlight)