WCF是微软开发的一款通信框架。具有跨平台跨操作系统的特点,所以,WCF一般用于开发第三方接口或者在分布式系统用做数据交互。

WCF三要素分别是地址(Address)、绑定(Binding)、契约(Contract)。

地址:服务端与客户端通信的uri。

绑定:描述了服务端与客户端交互数据的协议,服务端和客户端必须保持一致才能交互数据。

契约:系统间进行交互的数据/消息结构、格式。

做个Demo演示一下。

项目结构:

1、定义契约(服务契约、数据契约)

 namespace WCFService  //服务契约
{
[ServiceContract]
public interface ICalculate
{
[OperationContract]
int Add(int a, int b);
[OperationContract]
int Subtract();
}
}
 namespace WCFModel  //数据契约
{
[DataContract]
public class OperationNumber
{
[DataMember]
public int Num1 { get; set; }
[DataMember]
public int Num2 { get; set; }
}
}

2、实现契约,定义服务

 namespace WCFService
{
public class Calculate : ICalculate
{
public int Add(int a, int b)
{
return a + b;
} OperationNumber oper = new OperationNumber() { Num1 = , Num2 = };
public int Subtract()
{
int result = oper.Num1 - oper.Num2;
return result;
}
}
}

3、服务端开发,把WCF寄宿到控制台应用程序中

3.1、创建控制台应用程序

 namespace WCFServer
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(WCFService.Calculate)))
{
host.Open();
Console.WriteLine("服务开启了");
Console.ReadKey();
}
}
}

3.2 配置服务端App.config文件

 <?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<system.serviceModel>
<services>
<service name="WCFService.Calculate" behaviorConfiguration="metadataBehavior" >
<host>
<baseAddresses>
<add baseAddress="http://127.0.0.1:9999/Calculate"/>
</baseAddresses>
</host>
<endpoint address="" binding="wsHttpBinding" contract="WCFService.ICalculate"></endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="metadataBehavior">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>

3.3、编译生成下服务端,让服务端处于运行状态。在浏览器中输入“http://127.0.0.1:9999/Calculate”,看到类似下面的图片,表示服务端开发完成。

4、实现客户端调用WCF,完成数据交互

4.1创建客户端控制台应用程序,客户端在应用程序的"引用"节点右键,选择"添加引用服务",在弹出来的对话框中的地址栏写入服务端的地址,选择“发现”即可看到客户顿获得服务端定义的方法。

namespace WCFClient
{
class Program
{
static void Main(string[] args)
{
using (ChannelFactory<ICalculate> channelFactory = new ChannelFactory<ICalculate>("WSHttpBinding_ICalculate"))
{
ICalculate proxy = channelFactory.CreateChannel();
using(proxy as IDisposable)
{
Console.WriteLine(proxy.Add(,));
Console.WriteLine(proxy.Subtract());
}
}
Console.ReadKey();
}
}

4.2配置客户端App.config文件

 <?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_ICalculate" />
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://127.0.0.1:9999/Calculate" binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_ICalculate" contract="WCFReference.ICalculate"
name="WSHttpBinding_ICalculate">
<identity>
<userPrincipalName value="SUN-PC\Administrator" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>

5、生成下客户端,在服务端程序运行的情况下,运行客户端,可以看到客户端运行结果。

备注:

  添加契约需要引用"System.ServiceModel"命名空间。

  数据契约除了引用上面的命名空间外,还需要引用“System.Runtime.Serialization”命名空间。

WCF(一)控制台寄宿的更多相关文章

  1. 创建WCF服务自我寄宿

    WCF服务的寄宿方式 WCF寄宿方式是一种非常灵活的操作,可以寄宿在各种进程之中,常见的寄宿有: IIS服务.Windows服务.Winform程序.控制台程序中进行寄宿,从而实现WCF服务的运行,为 ...

  2. WCF服务自我寄宿

    WCF服务的寄宿方式 WCF寄宿方式是一种非常灵活的操作,可以寄宿在各种进程之中,常见的寄宿有: IIS服务.Windows服务.Winform程序.控制台程序中进行寄宿,从而实现WCF服务的运行,为 ...

  3. WCF绑定netTcpBinding寄宿到IIS

    继续沿用上一篇随笔中WCF服务类库 Wettery.WcfContract.Services WCF绑定netTcpBinding寄宿到控制台应用程序 服务端 添加WCF服务应用程序 Wettery. ...

  4. WCF绑定netTcpBinding寄宿到控制台应用程序

    契约 新建一个WCF服务类库项目,在其中添加两个WCF服务:GameService,PlayerService 代码如下: [ServiceContract] public interface IGa ...

  5. WCF服务自我寄宿 Windows服务

    WCF寄宿有自我寄宿跟IIS寄宿 服务代码: [ServiceContract] ---服务契约 public interface ICustomerService { [OperationContr ...

  6. 通过代码的方式完成WCF服务的寄宿工作

    使用纯代码的方式进行服务寄宿 服务寄宿的目的是为了开启一个进程,为WCF服务提供一个运行的环境.通过为服务添加一个或者多个终结点,使之暴露给潜在的服务消费,服务消费者通过匹配的终结点对该服务进行调用, ...

  7. WCF :IIS寄宿方式的Web地址、BaseAddress和EndPoint Address的关系

    对于在IIS中通过W3SVC或WAS寄宿的WCF Service,其在浏览器中显示的地址(Web地址),与其配置文件中的BaseAddress和EndPoint Address有什么关系呢?让我们来分 ...

  8. WCF 非http寄宿IIS

    摘要 从IIS 7 开始, IIS增加了对非HTTP协议的支持. 因此, 自IIS 7之后, 可以将NetTcpBinding等非HTTP协议的Bindings直接寄宿在IIS上面. 本文将介绍如何在 ...

  9. 使用C#创建WCF服务控制台应用程序

    本文属于原创,转载请注明出处,谢谢! 一.开发环境 操作系统:Windows 10 开发环境:VS2015 编程语言:C# IIS版本:10.0.0.0 二.添加WCF服务.Internet Info ...

随机推荐

  1. Ubuntu 16.04 安装python3.6 环境并设置为默认

    1.添加python3.6安装包,并且安装 sudo apt-get install software-properties-common 2.下载python3.6 sudo add-apt-rep ...

  2. windows 命令行 for 用法

    for /r 目录名 %i in (匹配模式1,匹配模式2) do @echo %i for /r SATA %i in (*.txt) do @echo %i D:\REY\test>for ...

  3. CF787A - The Monster

    /* CF787A - The Monster http://codeforces.com/contest/787/problem/A 数学 扩展欧几里得 注意x或y为0的时候要特判 并且结果要大于b ...

  4. [Linux]第一部分-认识Linux及Linux主机规划与安装

    ctrl + alt + f1~f6 切换六个终端ctrl + alt + f7 图形化界面 startx 开启x-window桌面 ls -al /root 列出root目录 date日期 +%y/ ...

  5. rails undefined method error_messages

    rails undefined method error_messages 学习了:http://stackoverflow.com/questions/10002140/use-error-mess ...

  6. [React] Reference a node using createRef() in React 16.3

    In this lesson, we look at where we came from with refs in React. Starting with the deprecated strin ...

  7. hdoj 5092 Seam Carving 【树塔DP变形 + 路径输出】 【简单题】

    Seam Carving Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...

  8. tomcat为什么要禁用session?

    转载请标明出处:http://blog.csdn.net/goldenfish1919/article/details/47829755 我们先来做一个实验,用jmeter对tomcat下的一个jsp ...

  9. SOA概念具体解释

    1.概述 1.1基本定义 SOA(Service-Oriented Architecture)既面向服务的体系结构,是一个组件模型.它将应用程序猿的不同功能可是(称为服务)通过定义良好的接口联系起来. ...

  10. SharePoint 创建网站地图树视图及格式枚举截图

    SharePoint 创建网站地图树视图及格式枚举截图         SharePoint首页隐藏掉左側导航以后,假设要以树视图呈现站点地图也非常easy.         仅仅须要复制v4.mas ...