关注WCF有很长一段时间了,随着正式版的发布,决定督促自己加快学习步伐
Robert Lee, All Rights Reserved......
关于WCF的优势与特点就不赘述了,无论是中文文档还是英文文档都说了很多,为了让更多对WCF感兴趣的朋友尽快投入学习的阵营,这里贴出相关配置与示例的一些说明。内容比较浅,英文文档里都有,不过写成blog,一是督促自己,再则为了给大家提供点便利,希望能起到抛砖引玉的功效。
由于是第一篇文章,时间比较仓促,详细的说明与图解暂时没有,日后会逐渐对其补完。不过基础的原理园子里有位兄台已经有专文描述了
安装开发环境很简单,需要.NET 3.0/SDK/WCF
===============================================================================================
WCF运行环境设置
===============================================================================================
1.确认ASP.NET已正确安装,可通过运行后缀为aspx的html验证
2.如果在安装完WCF之后安装IIS,运行以下命令为IIS安装WCF扩展:
"%WINDIR%\Microsoft.Net\Framework\v3.0\Windows Communication Foundation\ServiceModelReg.exe" -r
3.关闭防火墙
4.访问此地址:
ms-help://MS.MSSDK.1033/MS.NETFX30SDK.1033/WCF_samples/html/a5848ffd-3eb5-432d-812e-bd948ccb6bca.htm
下载Setup.zip,解压到任意目录,使用管理员权限做后续的操作,
4.1.<注意只能运行一次>在命令行窗口中运行目录下的Setupvroot.bat,这将会在IIS目录下新建servicemodelsamples目录及其bin子目录
4.2.<注意不是cmd窗口>打开Visual Studio命令行窗口,转到解压目录下,运行Setupcerttool.bat,以创建某些示例所需的证书工具
5.某些示例需要日志输出,创建c:\logs目录,并为特定帐户指定访问权限:
windows 2003/Windows Vista : Network Service
Windows XP : ASPNET
6.确认Service(.svc)文件类型在IIS中正确注册。通过IIS管理器,查看servicemodelsamples虚拟目录所属网站的属性-->主目录-->配置-->映射
6.1查看后缀为.svc的注册类型。如果缺失则通过“添加...”按钮创建
6.2在弹出的窗口中选择执行文件为%windir%\Microsoft.NET\framework\v2.0.50727\aspnet_isapi.dll,后缀为.svc,动作为“GET,HEAD,POST,DEBUG”,取消“确认文件是否存在”的复选框
7.访问此地址:
ms-help://MS.MSSDK.1033/MS.NETFX30SDK.1033/WCF_samples/html/967a3d94-0261-49ff-b85a-20bb07f1af20.htm
下载示例程序,在VS2005中运行
8.正常运行client示例,将弹出计算结果的命令行窗口。如出现错误请检查上述步骤
===============================================================================================
Your first WCF service(IIS-Host)
===============================================================================================
1.新建WCF Service Library项目MyService
2.修改生成的services1.cs文件。增加命名空间,并删除inner class部分
3.新建MyService.svc文件,内容如下,注意替换命名空间部分:
<%@ServiceHost language=c# Debug="true" Service="#Namespace#.MyService" %>
4.修改/新建web.config文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<!--注意name为全名,behaviorConfiguration在behavior节,名称需对应-->
<service
name="Lee.NetFx3.MyService"
behaviorConfiguration="MyServiceBehavior">
<!-- ICalculator is exposed at the base address provided by host: http://localhost/servicemodelsamples/service.svc -->
<endpoint address=""
binding="wsHttpBinding"
contract="Lee.NetFx3.IMyService" />
<!-- the mex endpoint is exposed at http://localhost/servicemodelsamples/service.svc/mex -->
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
<!--For debugging purposes set the includeExceptionDetailInFaults attribute to true-->
<behaviors>
<behavior name="MyServiceBehavior" >
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
5.为此项目增加生成后事件,代码如下(将创建servicemodelsamples虚拟目录,并复制服务文件到此目录)
mkdir %SystemDrive%\inetpub\wwwroot\servicemodelsamples
mkdir %SystemDrive%\inetpub\wwwroot\servicemodelsamples\bin
copy "$(TargetDir)MyService.dll" %SystemDrive%\inetpub\wwwroot\servicemodelsamples\bin
copy "$(ProjectDir)MyService.svc" %SystemDrive%\inetpub\wwwroot\servicemodelsamples
copy "$(ProjectDir)web.config" %SystemDrive%\inetpub\wwwroot\servicemodelsamples
6.用SvcUtil工具生成Client类及Config文件:
svcutil.exe http://localhost/ServiceModelSamples/MyService.svc?wsdl
注意需用Visual Studio命令行,默认生成目录为当前命令行所在目录
7.新建WindowsApplication项目MyClient
8.复制生成的Client类到MyClient项目下,调用方式如下:
MyServiceClient client = new MyServiceClient();
之后便可以调用client的方法了
注意使用后关闭调用:
client.Close();
9.修改生成的config文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<client>
<endpoint address="http://#ServerName#/ServiceModelSamples/MyService.svc"
binding="wsHttpBinding"
contract="IMyService" />
</client>
</system.serviceModel>
</configuration>
10.客户端运行就可以看到效果了