构建一个简单的WCF应用
2011-04-25 12:40
741 查看
买了《WCF技术剖析》,按着书本的例子进行操作,写下我的操作过程。
参考博客:/article/1308643.html
![](https://oscdn.geek-share.com/Uploads/Images/Content/201908/30/8d9c0c7bcb8d7c0f72f18af65eb3cb66.gif)
无法直接启动带有“类库输出类型”的项目,如下图所示。
![](https://oscdn.geek-share.com/Uploads/Images/Content/201908/30/e5dfcf51932e4dad3cf537db5ee29d2e.gif)
这时我们需要右键Hosting,然后选择“设为启动项目”,再次执行就不会报错了。
如果采用了上诉的配置,服务寄宿代码将会得到极大的精简,只需包含下面几行代码:
在执行步骤四以后E:/ms_workplace/WCF1/Hosting/bin/Debug目录下会有一个“Hosting.exe”的应用程序,双击打开该应用程序:
![](https://oscdn.geek-share.com/Uploads/Images/Content/201908/30/606d9f38c23b8fbf5264f8443a12fad1.gif)
然后右键Client项目,选择“添加服务引用”
![](https://oscdn.geek-share.com/Uploads/Images/Content/201908/30/b0c819c87307beabe917ce87454fe5af.gif)
点击确定即可完成服务引用的添加,这时Client下就会多出一个Service Reference
![](https://oscdn.geek-share.com/Uploads/Images/Content/201908/30/001100fe5a6fef669d17d9080cbc5c91.gif)
双击CalculatorServices,在对象浏览器中能够看到如下视图
![](https://oscdn.geek-share.com/Uploads/Images/Content/201908/30/86a40d9af2fc6d595a2cdb5e2efc0920.gif)
编译Client,会在E:/ms_workplace/WCF1/Client/bin/Debug有Client.exe,双击打开这个应用程序,会有如下结果:
参考博客:/article/1308643.html
步骤一 构建整个解决方案
![](https://oscdn.geek-share.com/Uploads/Images/Content/201908/30/8d9c0c7bcb8d7c0f72f18af65eb3cb66.gif)
步骤二 创建服务契约:ICalculator.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; namespace xuwei.WcfServices.Contracts//服务契约 { [ServiceContract(Name="CalculatorService",Namespace="http://blog.csdn.net/xw13106209")]//将接口定义成服务契约 public interface ICalculator { [OperationContract] double Add(double x,double y); [OperationContract] double Subtract(double x,double y); [OperationContract] double Multiply(double x,double y); [OperationContract] double Divide(double x, double y); } }
步骤三 创建服务:CalculatorService.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using xuwei.WcfServices.Contracts; namespace xuwei.WcfServices.Services//实现服务契约 { public class CalculatorService:ICalculator { public double Add(double x,double y) { return x + y; } public double Subtract(double x, double y) { return x - y; } public double Multiply(double x, double y) { return x * y; } public double Divide(double x, double y) { return x / y; } } }
步骤四 通过自我寄宿的方式寄宿服务:Hosting控制台中的program.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using xuwei.WcfServices.Contracts; using xuwei.WcfServices.Services; using System.ServiceModel.Description; using System.ServiceModel; namespace xuwei.WcfServices.Hosting//自主服务寄宿 { class Program { static void Main(string[] args) { using (ServiceHost host=new ServiceHost(typeof(CalculatorService))) { host.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "http://127.0.0.1:9999/calculatorservice"); if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null) { ServiceMetadataBehavior behavior = new ServiceMetadataBehavior(); behavior.HttpGetEnabled = true; behavior.HttpGetUrl = new Uri("http://127.0.0.1:9999/calculatorservice/metadata"); host.Description.Behaviors.Add(behavior); } host.Opened += delegate { Console.WriteLine("CalculatorService已经启动,按任意键终止服务!"); }; host.Open(); Console.Read(); } } } }
注意1:
完成以后需要编译Hosting下的program.cs。但是在通过Ctrl+F5执行(其实可以通过右键解决方案->生成解决方案完成,不需要通过Ctrl+F5执行)的时候可能报错:无法直接启动带有“类库输出类型”的项目,如下图所示。
![](https://oscdn.geek-share.com/Uploads/Images/Content/201908/30/e5dfcf51932e4dad3cf537db5ee29d2e.gif)
这时我们需要右键Hosting,然后选择“设为启动项目”,再次执行就不会报错了。
注意2
在进行真正的WCF应用开发时,一般不会直接通过编码的方式进行终结点的添加和服务行为的定义,而是通过配置的方式进行。上面添加终结点和定义服务行为的代码可以通过如下方法进行。首先在Hosting项目中创建应用程序配置文件App.config,在App.config中添加如下配置:<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="metadataBehavior"> <serviceMetadata httpGetEnabled="true" httpGetUrl="http://127.0.0.1:9999/calculatorservice/metadata" /> </behavior> </serviceBehaviors> </behaviors> <services> <service behaviorConfiguration="metadataBehavior" name="xuwei.WcfServices.Services.CalculatorService"> <endpoint address="http://127.0.0.1:9999/calculatorservice" binding="wsHttpBinding" contract="xuwei.WcfServices.Contracts.ICalculator" /> </service> </services> </system.serviceModel> </configuration>
如果采用了上诉的配置,服务寄宿代码将会得到极大的精简,只需包含下面几行代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using xuwei.WcfServices.Contracts; using xuwei.WcfServices.Services; using System.ServiceModel.Description; using System.ServiceModel; namespace xuwei.WcfServices.Hosting//自主服务寄宿 { class Program { static void Main(string[] args) { using (ServiceHost host=new ServiceHost(typeof(CalculatorService))) { //host.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "http://127.0.0.1:9999/calculatorservice"); //if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null) //{ // ServiceMetadataBehavior behavior = new ServiceMetadataBehavior(); // behavior.HttpGetEnabled = true; // behavior.HttpGetUrl = new Uri("http://127.0.0.1:9999/calculatorservice/metadata"); // host.Description.Behaviors.Add(behavior); //} host.Opened += delegate { Console.WriteLine("CalculatorService已经启动,按任意键终止服务!"); }; host.Open(); Console.Read(); } } } }
步骤五 创建客户端调用服务:Client中的program.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using xuwei.WcfService.Client.CalculatorServices; namespace xuwei.WcfService.Client { class Program { static void Main(string[] args) { using (CalculatorServiceClient proxy = new CalculatorServiceClient()) { Console.WriteLine("x+y={2} when x={0} and y={1}",1,2,proxy.Add(1,2)); Console.WriteLine("x-y={2} when x={0} and y={1}", 1, 2, proxy.Subtract(1, 2)); Console.WriteLine("x*y={2} when x={0} and y={1}", 1, 2, proxy.Multiply(1, 2)); Console.WriteLine("x/y={2} when x={0} and y={1}", 1, 2, proxy.Divide(1, 2)); Console.Read(); } } } }
在执行步骤四以后E:/ms_workplace/WCF1/Hosting/bin/Debug目录下会有一个“Hosting.exe”的应用程序,双击打开该应用程序:
![](https://oscdn.geek-share.com/Uploads/Images/Content/201908/30/606d9f38c23b8fbf5264f8443a12fad1.gif)
然后右键Client项目,选择“添加服务引用”
![](https://oscdn.geek-share.com/Uploads/Images/Content/201908/30/b0c819c87307beabe917ce87454fe5af.gif)
点击确定即可完成服务引用的添加,这时Client下就会多出一个Service Reference
![](https://oscdn.geek-share.com/Uploads/Images/Content/201908/30/001100fe5a6fef669d17d9080cbc5c91.gif)
双击CalculatorServices,在对象浏览器中能够看到如下视图
![](https://oscdn.geek-share.com/Uploads/Images/Content/201908/30/86a40d9af2fc6d595a2cdb5e2efc0920.gif)
编译Client,会在E:/ms_workplace/WCF1/Client/bin/Debug有Client.exe,双击打开这个应用程序,会有如下结果:
![](https://oscdn.geek-share.com/Uploads/Images/Content/201908/30/b5de4d91094cc4eb594fe7ffe4426538.gif)
相关文章推荐
- 构建一个简单的WCF应用——WCF学习笔记(1)
- 构建一个简单的WCF应用
- 构建一个简单的WCF应用
- 构建一个简单的WCF应用
- 构建一个简单的WCF应用
- 重温WCF之构建一个简单的WCF(一)(2)通过Windows Service寄宿服务和WCF中实现操作重载
- 在Win10上使用VS2015构建一个简单的WCF项目
- 综合应用WPF/WCF/WF/LINQ之三十八:实现一个简单的DataGrid之总体介绍
- (WCF)示例一: 构建一个简单的WCF Service: MagicEightBall
- 综合应用WPF/WCF/WF/LINQ之三十九:实现一个简单的DataGrid之获取某格的原始(或当前)行(或列)的Index
- Angular5初探之--构建一个简单的单页应用,包含登录(login)和几个一级菜单
- 微信小程序入门之构建一个简单TODOS应用
- 基于Vue2.0+Vue-router构建一个简单的单页应用
- 综合应用WPF/WCF/WF/LINQ之四十:实现一个简单的DataGrid之CheckBox已勾选的项的保存
- 一步一个脚印学习WCF之二构建一个简单的WCF应用程序前必须掌握的WCF基础术语---地址(Addresses)
- 基于MINA构建简单高性能的NIO应用-一个简单的例子
- 学习构建一个简单的wcf服务
- 基于Vue2.0+Vue-router构建一个简单的单页应用
- 4 - Swift之2 - 使用xcode7构建一个简单的应用并在IOS9设备上真机运行
- 基于Vue2.0+Vue-router构建一个简单的单页应用