转基于net.tcp的WCF配置实例解析

来源:互联网 发布:淘宝优惠券秒杀九块 编辑:程序博客网 时间:2024/06/15 19:42

转基于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,然后将本机的配置文件中的地址也修改为这个,最后运行,依然能够得到返回的结果。

原创粉丝点击