开通黄钻

基于net.tcp的WCF配置实例解析
本文主要通过文件配置来讲解如何编写一个基于net.tcp的Windows Form小程序。

使用的工具

涉及的工具有:

SvcUtil.exe

WCF Service Configuration Editor

服务器端的配置步骤

首先让我们来开始编写服务器端,服务器端用的是Console Application。

由于我们的软件是一个能够通过学生ID来获取学生姓名的小程序,所以,首先必须得有一个StudentInfo实体类:

View Code

using System.Runtime.Serialization; namespace WCFServiceGeneratedByConfig{ [DataContract] public class StudentInfo { int studentID; string lastName; string firstName; [DataMember] public int StudentID { get { return studentID; } set { studentID = value; } } [DataMember] public string FirstName { get { return firstName; } set { firstName = value; } } [DataMember] public string LastName { get { return lastName; } set { lastName = value; } } }}

在这个实体类中,DataContract代表数据契约,DataMeber代表数据成员,加上这些Attribute后,能够传送给Client端。

然后是一个接口,这个接口定义了服务契约和操作契约:

View Code

using System.Collections.Generic; using System.ServiceModel; namespace WCFServiceGeneratedByConfig{ [ServiceContract] public interface IStudentService { [OperationContract] string GetStudentFullName( int studentId); [OperationContract] IEnumerable<StudentInfo> GetStudentInfo( int studentId); }}

然后是具体的实现方法:

View Code

using System.Collections.Generic; using System.Linq; namespace WCFServiceGeneratedByConfig{ public class StudentService : IStudentService { List<StudentInfo> list = new List<StudentInfo>(); public StudentService() { list.Add( new StudentInfo { StudentID = 10010, FirstName = " Shi ", LastName = " Chaoyang " }); list.Add( new StudentInfo { StudentID = 10011, FirstName = " Liu ", LastName = " Jieus " }); list.Add( new StudentInfo { StudentID = 10012, FirstName = " Cheung ", LastName = " Vincent " }); list.Add( new StudentInfo { StudentID = 10013, FirstName = " Yang ", LastName = " KaiVen " }); } public string GetStudentFullName( int studentId) { IEnumerable< string> Student = from p in list where p.StudentID == studentId select p.FirstName + " " + p.LastName; return Student.Count() != 0 ? Student.First() : string.Empty; } public IEnumerable<StudentInfo> GetStudentInfo( int studentId) { IEnumerable<StudentInfo> Student = from p in list where p.StudentID == studentId select p; return Student; } }}

这个方法里,我们有两个函数,一个能够根据学生点获取学生全名,另一个是根据学生点获取学生的实体对象。

好了,让我们来编译这个项目,得到一个WCFServiceGeneratedByConfig.exe文件。

然后,我们需要配置文件来让服务器端启动,所以这里我们要用WCF Service Configuration Editor

工具来进行,由于在VS2008 和VS2010中带有这个软件,我们可以直接通过菜单->Tools->WCF Service Configuration Editor来打开。

首先,点击File->New config, 打开Service的Configuration界面。

然后,点击Create a new service…,在弹出的界面中,我们选择刚才生成的那个WCFServiceGeneratedByConfig.exe文件。双击之后,软件自动显示出了里面含有的Service:

点选那个Service,然后点击两次next,我们会看到出现了选择Communation Mode的界面,这里由于我们用的是net.tcp,所以我选择了第一个:TCP。

然后点击Next,我们会看到要我们填写EndPoint,这里我随便填写了一个:

之后,点击Next知道Finish,然后,我们的最基本的配置就结束了。

回到Config界面之后,我们点击Advanced->Service Behaviors->New Service Behavior Configuration,在弹出的界面中,我们点击Add->serviceMetadata:

然后点击Add,我们就添加了一个Behavior Element。点击刚刚生成的serviceMetadata节点,在显示的界面中,设置HttpGetEnabled为true。

然后点击原来的Service节点下的Host节点,在Base Address栏目下单击Add,添加如下的Base Address:

最后点击OK。然后点击菜单File->Save As 保存到项目文件夹下即可。

这里是生成的代码:

View Code

<? xml version="1.0" encoding="utf-8" ?> < configuration > < system.serviceModel > < behaviors > < serviceBehaviors > < behavior name ="StudentBehavior" > < serviceMetadata httpGetEnabled ="true" httpsGetEnabled ="false" /> </ behavior > </ serviceBehaviors > </ behaviors > < services > < service name ="WCFServiceGeneratedByConfig.StudentService" > < endpoint address ="net.tcp://127.0.0.1:50001/StudentServiceEndPoint" binding ="netTcpBinding" bindingConfiguration ="" contract ="WCFServiceGeneratedByConfig.IStudentService" /> < host > < baseAddresses > < add baseAddress ="net.tcp://127.0.0.1:50001/StudentServiceEndPoint" /> </ baseAddresses > </ host > </ service > </ services > </ system.serviceModel > </ configuration >

这一步做完后,我们需要让服务能够启动,怎么启动呢?请看下面的代码:

View Code

using System; using System.ServiceModel; using System.ServiceModel.Description; namespace WCFServiceGeneratedByConfig{ class Program { static void Main( string[] args) { using (ServiceHost host = new ServiceHost( typeof(StudentService))) { ServiceMetadataBehavior smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>(); if (smb == null) host.Description.Behaviors.Add( new ServiceMetadataBehavior()); // 暴露出元数据,以便能够让SvcUtil.exe自动生成配置文件 host.AddServiceEndpoint( typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), " mex "); // 开启服务 host.Open(); Console.WriteLine( " Service listen begin to listen... "); Console.WriteLine( " press any key to teriminate... "); Console.ReadKey(); host.Abort(); host.Close(); } } }}

代码中的注释部分非常重要,我们一定要添加,否则下面的步骤不能进行,具体的原因,参加我的另一篇文章:在net.tcp模式下,由SvcUtil.exe生成代理类文件和配置文件

然后运行这个ConsoleApplication。

接下来,找到SvcUtil.exe,C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\SvcUtil.exe,在CMD窗口下运行如下命令:

C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\SvcUtil.exe net.tcp: // 127.0.0.1:50001/StudentServiceEndPoint

这样,这个小工具就会自动的给我们生成代理类和配置文件

Microsoft (R) Service Model Metadata Tool[Microsoft (R) Windows (R) Communication Foundation,版本 3.0. 4506.2152]版权所有(c) Microsoft Corporation。保留所有权利。 正在尝试使用 WS-Metadata Exchange 从“net.tcp:// 127.0. 0.1: 50001/StudentServiceEndPoint”下载元数据。此 URL 不支持 DISCO。正在生成文件...E:\WCF\WCF_ChatRoom\StudentService.csE:\WCF\WCF_ChatRoom\output.config请按任意键继续. . .

客户端的配置步骤

接下来,新建一个WindowsFormsApplication程序,将这个代理类拷入,配置文件修改名称为App.config拷入,

然后在Form1.cs中拖入一个文本框,一个按钮,一个DataGridView,后台代码如下:

View Code

using System; using System.Collections.Generic; using System.Windows.Forms; using WCFServiceGeneratedByConfig; namespace WindowsFormsApplication3{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click( object sender, EventArgs e) { Action action = new Action(Bind); action.BeginInvoke( new AsyncCallback((iar) => { Action actionEnd = (Action)iar.AsyncState; actionEnd.EndInvoke(iar); }), action); } private void Bind() { StudentServiceClient client = new StudentServiceClient(); IEnumerable<StudentInfo> x = client.GetStudentInfo(Int32.Parse(textBox1.Text)); dataGridView1.Invoke((Action)(() => { dataGridView1.DataSource = x; })); } }}

启动这个实例,输入学生ID,我们成功得到了服务端返回的值。

在本机和公网上的运行结果

那么能不能在公网上使用呢?呵呵,这个当然,将服务端拷贝到外网的一台机器上,然后修改服务器端的配置文件中的地址为:net.tcp://169.*.*.124:50001/ StudentServiceEndPoint,然后将本机的配置文件中的地址也修改为这个,最后运行,依然能够得到返回的结果。

源代码下载

点击这里下载源代码

基于net.tcp的WCF配置实例解析相关推荐

  1. java tcp 编程实例_Java实现基于TCP的通讯程序实例解析

    Java中的TCP通信程序 TCP可以实现两台计算机之间的数据交互通信的两端,要严格区分客户端与服务端 两端通信时的步骤: 1.服务端程序,需要事先启动,等待客户端连接 2.客户端主动连接服务器端,才 ...

  2. 详解:基于nginx tcp模块基本配置

    2019独角兽企业重金招聘Python工程师标准>>> The ngx_http_upstream_module module is used to define groups of ...

  3. 华为[ENSP]ACL配置实例(访问控制列表配置实例)

    ACL配置实验一(高级ACL)(简单)的拓扑图如下: 一.配置PC1.PC2和Server   二.配置Router(运用高级ACL配置) 相关命令解析: [Router]acl 3000(进入高级A ...

  4. 基于FRR全面解析BGP协议(八):FRR配置实例

    FRR配置实例 环境:centos 7.6.1810 版本:FRR 7.3 服务器: 1台 Namespace配置实例 FRR的vrf功能支持同一个进程在不同ns内创建bgp对等体.首先需要配置zeb ...

  5. tcp网络通信教程 java_基于java TCP网络通信的实例详解

    JAVA中设计网络编程模式的主要有TCP和UDP两种,TCP是属于即时通信,UDP是通过数据包来进行通信,UDP当中就会牵扯到数据的解析和传送.在安全性能方面,TCP要略胜一筹,通信过程中不容易出现数 ...

  6. 基于WT2003H语音芯片在LCD屏幕驱动的应用实例解析

    基于WT2003H语音芯片在LCD屏幕驱动的应用实例解析 随着现代生活节奏的加快,简单快捷的产品越来越受到人们的青睐,产品也越来越注重人机交互和简单明了的用户体验,一款具备LCD屏幕的产品往往更具有竞 ...

  7. ASA防火墙之基于用户MPF配置实例

    ASA防火墙之基于用户MPF 本篇继续介绍ASA防火墙,上篇我们讲了URL的过滤,那么本篇我们在这个基础下设置不同的用户,来控制一个可以访问,一个过滤掉. 配置实例 需求: ASA本地有两个用户:分别 ...

  8. 一个基于PoS共识算法的区块链实例解析(升级版)

    一个基于PoS共识算法的区块链实例解析(升级版) 一.前言 前面我们简单的介绍了一个基于PoS共识算法的例子,今天我们来解析一个升级版的例子.如果喜欢博主的话,记得点赞,关注,收藏哦~ 二.本例中的一 ...

  9. WCF配置(net.tcp协议)

    与Socket相比,WCF真是爽得不得了,其基本指导思想为SOA--面向服务. 其基本配置在于ABC(Address,Binding,Contract),通常,只要这三个因素配置对了,那么,基本上就无 ...

最新文章

  1. CentOS下Docker 安装
  2. Docker基本原理概述
  3. Ubuntu13.04 下源码安装Rapidsvn
  4. 用Alpha生成Trimp图的方法(python)
  5. c++ split 方法(转)
  6. java 昨天今天明天
  7. oracle 转换成csv文件,如何将csv转换为oracle中的表
  8. java 默认排序方式_Java Collections.sort()实现List排序的默认方法和自定义方法
  9. iLogtail 与Filebeat 性能对比
  10. 检测到目标url存在内部ip地址泄露_Cendertron,动态爬虫与敏感信息泄露检测
  11. 帮你排雷Jmeter分布式性能测试那些坑~轻轻松松去实战
  12. 学术 | 如何写一篇学术论文?(下)
  13. 国外大神整理的 2019 年 Java 权威开发路线图,Java大神养成记
  14. 通过CloudXplorer打断异常的VHD lease连接
  15. 计算机考试准考证怎么下载
  16. 20个值得研究的vue项目
  17. 计算机用户原始密码是多少,administrator初始密码是多少
  18. recovery 工作流程
  19. 学习Fiddler安全测试
  20. Minecraft Mod 开发:2-Hello, Minecraft Mod World!

热门文章

  1. 【mysql+RBAC】RBAC权限处理(转载:http://www.cnblogs.com/xiaoxi/p/5889486.html 平凡希)...
  2. 在人的一生,有些细微之事,本身毫无意义可言,却具有极大的重要性。事过境迁之后,回顾其因果关系,
  3. 01 leading
  4. 我这个程序员是如何找到女朋友的
  5. 一致性hash环原理
  6. Maglev 一致性Hash调研
  7. 用纸箱做出来的机器人铠甲_萌萌哒!用废旧纸箱制作时装 小小“机器人”要当地球守护者...
  8. H5+CSS3玩转骰子
  9. 组饭团应用程序的说明
  10. UEFI使用rEFInd引导Win10+Deepin双系统