您的位置:首页 > 其它

【转载】WCF入门级使用教程

2008-11-22 11:19 531 查看
开发环境:vs2008英文版(SP1) + IIS + Windows2003

整个解决方案有4个项目
01.WCF ---Class Libary项目,用于生成WCF所需的类/接口文件
02.BLL ---Class LIbary项目,演示用的业务逻辑层(仅做分层演示用,无实际意义)
03.WEB ---Web Application,WCF服务将发布在这个项目中(即本例是把WCF宿主在IIS里)
04.Client--Console Application,命令行程序,用于演示调用WCF的客户端程序

using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Runtime.Serialization;
5using System.ServiceModel;
6using System.Text;
7
8namespace WCF
9using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Runtime.Serialization;
5using System.ServiceModel;
6using System.Text;
7
8namespace WCF
9using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5
6namespace BLL
7
2<system.serviceModel>
3 <behaviors>
4 <serviceBehaviors>
5 <behavior name="WEB.DemoServiceBehavior">
6 <serviceMetadata httpGetEnabled="true"/>
7 <serviceDebug includeExceptionDetailInFaults="false"/>
8 </behavior>
9 </serviceBehaviors>
</behaviors>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_ICalculateService">
<security mode="None">
</security>
</binding>
</wsHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="WEB.DemoServiceBehavior" name="WCF.CalculateService">
<endpoint address="wcf" binding="wsHttpBinding" contract="WCF.ICalculateService" bindingConfiguration="WSHttpBinding_ICalculateService" name="WCF.ICalculateService"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
</system.serviceModel>

7.WCF在IIS里的配置
iis里新建一个站点,指向WEB项目所在的目录,端口在本例中设置为90,如果在浏览器直接浏览http://localhost:90/WCF/CalculateService.svc,表明IIS的环境没问题,如果不行,建议重新安装.net framework3.5(当然也有其它办法,就是增加svc后缀的映射,这个百度一下N多,就不重复了)

8.刚才的WEB项目里,应该还有一个Default.aspx的页面,这里我们简单示例一下调用BLL层代码(Default.aspx.cs内容)

namespace WEB
2using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5
6namespace ConsoleTest
7
调用真的很简单吧,好了,总结一下:
本例中,先编写了一个简单的WCF服务,然后把它宿主在IIS中运行,然后用控制台程序来调用这个WCF.

当然WCF深入研究下去,远比这个复杂,这篇文章主要是为了消除大家对新技术的恐惧,快速上手WCF的使用,其实MS每次推出的新技术,听上去蛮吓人,用起来都很简单的.

(转载来自"菩提树下的杨过")
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: