WCF学习之三:WCF 入门级 使用教程 下
|
6.在WEB项目中,新建一个WCF目录,然后在该目录下Add-->new Item-->WCF Service,命名为CalculateService.svc,添加后,这里有一个关键步骤,把WCF目录下,除CalculateService.svc以外的文件都删除,然后双击CalculateService.svc,修改内容为 ,顺便给出web.config的一段关键配置 1 ![]() ![]() 2 <system.serviceModel>3 <behaviors>4 <serviceBehaviors>5 <behavior name="WEB.DemoServiceBehavior">6 <serviceMetadata httpGetEnabled="true"/>7 <serviceDebug includeExceptionDetailInFaults="false"/> 8 </behavior>9 </serviceBehaviors>10 </behaviors>11 <bindings>12 <wsHttpBinding>13 <binding name="WSHttpBinding_ICalculateService">14 <security mode="None"> 15 </security>16 </binding>17 </wsHttpBinding>18 </bindings>19 <services>20 <service behaviorConfiguration="WEB.DemoServiceBehavior" name="WCF.CalculateService">21 <endpoint address="wcf" binding="wsHttpBinding" contract="WCF.ICalculateService" bindingConfiguration="WSHttpBinding_ICalculateService" name="WCF.ICalculateService"/> 22 <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>23 </service>24 </services>25 </system.serviceModel>26 ![]()
7.WCF在IIS里的配置
8.刚才的WEB项目里,应该还有一个Default.aspx的页面,这里我们简单示例一下调用BLL层代码(Default.aspx.cs内容) 1 namespace WEB2 {3 public partial class _Default : System.Web.UI.Page4 {5 protected void Page_Load(object sender, EventArgs e)6 {7 //通过BLL层来调用WCF中的方法 8 BLL.Test _Test = new BLL.Test();9 double z= _Test.Add(5, 10);10 Response.Write(z.ToString()); 11 }12 }13 }编译浏览该页面,如果能显示15,表示ok了,Web项目完工
9.解决方案中,再添加一个Console Application,命名为04_Client,我们将在这个项目中,调用WEB中的WCF,注意要添加对System.ServiceModel的引用
10.关键步骤:浏览http://localhost:90/WCF/CalculateService.svc时,会发现页面上有一个提示: 若要测试此服务,需要创建一个客户端,并将其用于调用该服务。可以使用下列语法,从命令行中使用 svcutil.exe 工具来进行此操作:
svcutil.exe http://jimmycntvs:90/WCF/CalculateService.svc?wsdl /d:c:\123\ 完成后,查看c:\123目录,会生成二个文件CalculateService.cs,output.config 这一步的目的在于利用svcutil.exe这个工具,生成客户端调用所需的代理类和配置文件
1 using System;2 using System.Collections.Generic;3 using System.Linq;4 using System.Text;5 ![]() 6 namespace ConsoleTest7 {8 class Program9 {10 static void Main(string[] args)11 {12 CalculateServiceClient _client = new CalculateServiceClient();13 double x = 5, y = 10;14 double z = _client.Add(x, y);15 ![]() 16 Console.WriteLine("{0} + {1} = {2}", x.ToString(), y.ToString(), z.ToString());17 ![]() 18 Console.ReadLine();19 }20 }21 }22 ![]() 调用真的很简单吧,好了,总结一下: 当然WCF深入研究下去,远比这个复杂,这篇文章主要是为了消除大家对新技术的恐惧,快速上手WCF的使用,其实MS每次推出的新技术,听上去蛮吓人,用起来都很简单的. |





}
}

浙公网安备 33010602011771号