WCF 配置文件工具

来源:互联网 发布:翻拍 网络视频 编辑:程序博客网 时间:2024/06/07 01:06

http://www.cnblogs.com/scy251147/archive/2012/11/23/2784902.html



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

使用的工具

涉及的工具有:

SvcUtil.exe

WCF Service Configuration Editor

服务器端的配置步骤

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

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

View Code

using System.Runtime.Serialization;

 

namespace WCFServiceGeneratedByConfig

{

    [DataContract]

    public class StudentInfo

    {

        intstudentID;

 

        stringlastName;

 

        stringfirstName;

 

        [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]

        stringGetStudentFullName(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>();

 

        publicStudentService()

        {

            list.Add(newStudentInfo { StudentID =10010, FirstName ="Shi", LastName ="Chaoyang" });

            list.Add(newStudentInfo { StudentID =10011, FirstName ="Liu", LastName ="Jieus" });

            list.Add(newStudentInfo { StudentID =10012, FirstName ="Cheung", LastName ="Vincent" });

            list.Add(newStudentInfo { StudentID =10013, FirstName ="Yang", LastName ="KaiVen" });

        }

 

        public string GetStudentFullName(intstudentId)

        {

            IEnumerable<string>Student =from pin list

                                          where p.StudentID == studentId

                                          select p.FirstName + " " + p.LastName;

            returnStudent.Count() !=0 ? Student.First() :string.Empty;

        }

 

        publicIEnumerable<StudentInfo> GetStudentInfo(intstudentId)

        {

            IEnumerable<StudentInfo>Student = from p in list

                                               where p.StudentID == studentId

                                               select p;

            returnStudent;

        }

    }

}

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

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

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

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

首先,点击File->Newconfig,  打开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 BehaviorConfiguration,在弹出的界面中,我们点击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>

                <behaviorname="StudentBehavior">

                    <serviceMetadatahttpGetEnabled="true" httpsGetEnabled="false"/>

                </behavior>

            </serviceBehaviors>

        </behaviors>

        <services>

            <servicename="WCFServiceGeneratedByConfig.StudentService">

                <endpointaddress="net.tcp://127.0.0.1:50001/StudentServiceEndPoint"

                    binding="netTcpBinding" bindingConfiguration=""contract="WCFServiceGeneratedByConfig.IStudentService"/>

                <host>

                    <baseAddresses>

                        <addbaseAddress="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 tolisten...");

                Console.WriteLine("press any key to teriminate...");

                Console.ReadKey();

                host.Abort();

                host.Close();

            }

        }

    }

}

 

 

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

然后运行这个ConsoleApplication。

接下来,找到SvcUtil.exe,在VS命令提示输入:

svcutil net.tcp://127.0.0.1:50001/StudentServiceEndPoint

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

Microsoft (R) ServiceModel Metadata Tool

 

[Microsoft (R) Windows(R) Communication Foundation,版本 3.0.4506.2152]

 

版权所有(c) MicrosoftCorporation。保留所有权利。

 

 

 

正在尝试使用 WS-MetadataExchange 从“net.tcp://127.0.0.1:50001/StudentServiceEndPoint”下载元数据。此 URL 不支持DISCO。

 

正在生成文件...

 

E:\WCF\WCF_ChatRoom\StudentService.cs

 

E:\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

    {

        publicForm1()

        {

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



源码下载


原创粉丝点击