刚开始学习wcf,根据官方网站的说明写下的代码

第一步:

建立一个类库项目GettingStartedLib,首先添加wcf引用System.ServiceModel; 添加接口ICalculator,添加类CalculatorService实现接口ICalculator

代码:ICalculator

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
namespace GettingStartedLib
{
[ServiceContract] //契约声明
public interface ICalculator
{
[OperationContract] //操作契约
double Add(double n1, double n2);
[OperationContract]
double Subtract(double n1, double n2);
[OperationContract]
double Multiply(double n1, double n2);
[OperationContract]
double Divide(double n1, double n2);
}
}

代码:CalculatorService

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace GettingStartedLib
{
public class CalculatorService : ICalculator
{
public double Add(double n1, double n2)
{
double result = n1 + n2;
Console.WriteLine("相加数:({0},{1})", n1, n2);
// 输出结果到控制台
Console.WriteLine("结果: {0}", result);
return result;
} public double Subtract(double n1, double n2)
{
double result = n1 - n2;
Console.WriteLine("相减数:({0},{1})", n1, n2);
Console.WriteLine("结果: {0}", result);
return result;
} public double Multiply(double n1, double n2)
{
double result = n1 * n2;
Console.WriteLine("相乘数:({0},{1})", n1, n2);
Console.WriteLine("结果: {0}", result);
return result;
} public double Divide(double n1, double n2)
{
double result = n1 / n2;
Console.WriteLine("相除数:({0},{1})", n1, n2);
Console.WriteLine("结果: {0}", result);
return result;
}
}
}

然后在这个类库中添加app.config文件。刚开始做不知道文件格式,可以另建立一个wcf类库服务,找到自动生成的配置文件当做模板,复制到当前的项目中,少做修改就行了。

代码:app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration> <appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" />
</system.web>
<!-- 部署服务库项目时,必须将配置文件的内容添加到
主机的 app.config 文件中。System.Configuration 不支持库的配置文件。-->
<system.serviceModel>
<services>
<service name="GettingStartedLib.CalculatorService">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8100" />
</baseAddresses>
</host>
<!-- Service Endpoints -->
<!-- 除非完全限定,否则地址将与上面提供的基址相关 -->
<endpoint address="" binding="basicHttpBinding" contract="GettingStartedLib.ICalculator">
<!--
部署时,应删除或替换下列标识元素,以反映
用来运行所部署服务的标识。删除之后,WCF 将
自动推断相应标识。
-->
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<!-- Metadata Endpoints -->
<!-- 元数据交换终结点供相应的服务用于向客户端做自我介绍。 -->
<!-- 此终结点不使用安全绑定,应在部署前确保其安全或将其删除-->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- 为避免泄漏元数据信息,
请在部署前将以下值设置为 false -->
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
<!-- 要接收故障异常详细信息以进行调试,
请将以下值设置为 true。在部署前设置为 false
以避免泄漏异常信息-->
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel> </configuration>

第二步:

建立服务启动程序,这个创建一个控制台应用程序GettingStartedHost,首先添加wcf引用System.ServiceModel,添加GettingStartedLib类库的引用。

代码:GettingStartedHost

using GettingStartedLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;
namespace GettingStartedHost
{
class Program
{
static void Main(string[] args)
{
// Step 1 创建一个基地址
Uri baseAddress = new Uri("http://localhost:8100/GettingStarted");
// Step 2 创建 ServiceHost 实例
ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress);
try
{
// Step 3 增加服务终结点类型
selfHost.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "CalculatorService");
// Step 4 允许媒体类型转换.
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
selfHost.Description.Behaviors.Add(smb);
// Step 5 启动服务.
selfHost.Open();
Console.WriteLine("服务已经启动.");
Console.WriteLine("按Enter键终止服务.");
Console.WriteLine();
Console.ReadLine();
// 关闭服务.
selfHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine("异常出现: {0}", ce.Message);
selfHost.Abort();
}
} }
}

第三步:

建立控制台应用程序GettingStartedClient,首先添加引用System.ServiceModel,和GettingStartedLib类库

代码:GettingStartedClient.Program

using GettingStartedClient.ServiceReference1;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace GettingStartedClient
{
class Program
{
static void Main(string[] args)
{
//Step 1: 创建一个Wcf代理实例.
CalculatorClient client = new CalculatorClient(); // Step 2: 请求服务操作.
// 请求加法操作.
double value1 = 100.00D;
double value2 = 15.99D;
double result = client.Add(value1, value2);
Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); //请求减法操作.
value1 = 145.00D;
value2 = 76.54D;
result = client.Subtract(value1, value2);
Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result); // 请求乘法操作.
value1 = 9.00D;
value2 = 81.25D;
result = client.Multiply(value1, value2);
Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result); // 请求除法操作.
value1 = 22.00D;
value2 = 7.00D;
result = client.Divide(value1, value2);
Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result); //Step 3:关闭服务
client.Close();
}
}
}

这里建立好客户端程序还是不可以使用,需要与wcf服务添加到客户端,我是通过vs的命令行工具 切换到客户端的项目目录输入:

svcutil.exe /language:cs /out:generatedProxy.cs /config:app.config http://localhost:8000/ServiceModelSamples/service
,最后会自动生成引用文件。
界面:

第四步:

启动应用测试:首先启动Host程序,找到bin目录打开生成的.exe程序,启动服务。然后打开客户端程序。

代码下载:http://files.cnblogs.com/files/dearbeans/GettingStartedLib.rar

wcf 入门示例的更多相关文章

  1. [WCF编程]1.WCF入门示例

    一.WCF是什么? Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,整合了原有的windows通讯的 .net Remotin ...

  2. WCF入门教程(三)定义服务协定--属性标签

    WCF入门教程(三)定义服务协定--属性标签 属性标签,成为定义协议的主要方式.先将最简单的标签进行简单介绍,以了解他们的功能以及使用规则. 服务协定标识,标识哪些接口是服务协定,哪些操作时服务协定的 ...

  3. WCF入门教程(二)如何创建WCF服务

    WCF入门教程(二)从零做起-创建WCF服务 通过最基本的操作看到最简单的WCF如何实现的.这是VS的SDK默认创建的样本 1.创建WCF服务库 2.看其生成结构 1)IService1.cs(协议) ...

  4. WCF入门教程(二)从零做起

    通过最基本的操作看到最简单的WCF如何实现的.这是VS的SDK默认创建的样本 1.创建WCF服务库 2.看其生成结构 1)IService1.cs(协议) 定义了协议,具体什么操作,操作的参数和返回值 ...

  5. WCF入门教程(图文)VS2012

    WCF入门教程(图文)VS2012 上一遍到现在已经有一段时间了,先向关注本文的各位“挨踢”同仁们道歉了.小生自认为一个ITer如果想要做的更好,就需要将自己的所学.所用积极分享出来,接收大家的指导和 ...

  6. 【WCF系列一】WCF入门教程(图文) VS2012

    WCF的全称是Windows Communication Foundation,从英文名称上看,WCF就是基于Windows下一种通讯的基础架构.利用WCF能够实现基于Windows下的各种通讯技术的 ...

  7. WCF入门的了解准备工作

    了解WCF, 及WCF入门需要掌握哪里基本概念? 1.准备工作 >1.1 . XML >1.2 . Web Service >1.3 . 远程处理 (RPC) >1.4.  消 ...

  8. WCF入门教程(二)从零做起-创建WCF服务

    通过最基本的操作看到最简单的WCF如何实现的.这是VS的SDK默认创建的样本 1.创建WCF服务库 2.看其生成结构 1)IService1.cs(协议) 定义了协议,具体什么操作,操作的参数和返回值 ...

  9. C# -- HttpWebRequest 和 HttpWebResponse 的使用 C#编写扫雷游戏 使用IIS调试ASP.NET网站程序 WCF入门教程 ASP.Net Core开发(踩坑)指南 ASP.Net Core Razor+AdminLTE 小试牛刀 webservice创建、部署和调用 .net接收post请求并把数据转为字典格式

    C# -- HttpWebRequest 和 HttpWebResponse 的使用 C# -- HttpWebRequest 和 HttpWebResponse 的使用 结合使用HttpWebReq ...

随机推荐

  1. poj 3348(凸包面积)

    Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8063   Accepted: 3651 Description ...

  2. Android AutoCompleteTextView控件实现类似百度搜索提示,限制输入数字长度

    Android AutoCompleteTextView 控件实现类似被搜索提示,效果如下 1.首先贴出布局代码 activity_main.xml: <?xml version="1 ...

  3. Java程序执行时间

    第一种是以毫秒为单位计算的.  Java代码  //伪代码 long startTime=System.currentTimeMillis();   //获取开始时间 doSomeThing();  ...

  4. quailty's Contest #1 道路修建 EXT(启发式合并)

    题目链接  道路修建 EXT 考虑并查集的启发式合并,合并的时候小的子树的根成为大的子树的根的儿子. 可以证明这样整棵树的深度不会超过$logn$. 两个根合并的时候,产生的新的边的边权为当前的时间. ...

  5. springMVC笔记:jsp页面获取后台数据记录列表

    1.读取数据库中的记录List<HashMap<String,String>> attributes; 2.Controller构造Model如下: @RequestMappi ...

  6. HDU 4251 The Famous ICPC Team Again(划分树)

    The Famous ICPC Team Again Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  7. ReactiveCocoa(一)

    前言 之前总听别人说什么Reactive Cocoa + MVVM,但是没有找到讲解Reactive Cocoa相关的资料.结果进入新公司,项目里面有部分代码使用到了Reactive Cocoa,所以 ...

  8. POJ 3041 Asteroids (二分图匹配)

    [题目链接] http://poj.org/problem?id=3041 [题目大意] 一个棋盘上放着一些棋子 每次操作可以拿走一行上所有的棋子或者一列上所有的棋子 问几次操作可以拿完所有的棋子 [ ...

  9. MySQL类型转换 使用CAST将varchar转换成int类型排序

    --使用CAST将varchar转换成int类型排序 select distinct(zone_id) from guild_rank_info order by CAST(zone_id as SI ...

  10. C++多重继承时调用相应的父类函数

    C++中没有super或parent关键字,想要调父类方法,只能使用明确的[父类名称::方法名] 假如要求A和B是C的父类的前提下,要使如下代码能够分别输出A和B的相关信息(虽然这个要求很少遇到... ...