代码改变世界

第一个WCF实例——HelloWCF

2013-03-26 20:36  Keiven_LY  阅读(203)  评论(0)    收藏  举报
1.1 项目说明

      宿主程序为控制台应用程序,客户端也为控制台应用程序

1.2 项目创建过程

 

第一步:创建一个空的解决方案,取名WCFTest_4(可以任意取名);

第二步:在上述解决方案中,文件——>添加——>新建项目,选择控制台应用程序,取名Host,这个控制台程序用来作为服务的宿主程序;

第三步:在解决方案中,右键Host——>添加——>新建项,选择WCF服务,取名HelloWCFService,系统自动生成IHelloWCFService.csHelloWCFService.csApp.config。其中IHelloWCFService.cs为接口,在此添加契约,相当于web service中的methordHelloWCFService.cs为契约的实现方法;

 

IHelloWCFService.cs中的代码:

using System;

using System.ServiceModel;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.Text;

 

namespace Host

{

    // 注意如果更改此处的接口名称 "IHelloWCFService",也必须更新 App.config 中对 "IHelloWCFService" 的引用。

    [ServiceContract]

    public interface IHelloWCFService

    {

        //[OperationContract]

        //void DoWork();

        [OperationContract]

        string HelloWCF(string message);

    }

}

HelloWCFService.cs中的代码:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

 

namespace Host

{

    // 注意如果更改此处的类名 "HelloWCFService",也必须更新 App.config 中对 "HelloWCFService" 的引用。

    public class HelloWCFService : IHelloWCFService

    {

        //public void DoWork()

        //{

        //}

        public string HelloWCF(string message)

        {

            return string.Format("你在{0}收到信息:{1}"DateTime.Now, message);

        }

    }

}

 

App.config文件原则上可以不用改,但是address太长了

(默认的为baseAddress=http://localhost:8731/Design_Time_Addresses/Host/HelloWCFService/

修改为baseAddress=http://localhost:8731/HelloWCFService/

 

 

Program.cs中的代码:

using System;

using System.ServiceModel;  //自己添加的引用,必须添加

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace Host

{

    class Program

    {

        static void Main(string[] args)

        {

            using (ServiceHost host = new ServiceHost(typeof(Host.HelloWCFService)))

//Host为宿主名,HelloWCFService为服务名

            {

                host.Open();

                Console.ReadLine();

                host.Close();

            }

        }

    }

}

第四步:保存,启动项目,系统在项目保存文件夹下自动生成Host.exe ,路径为:项目保存文件夹——>Host——>bin——>Debug——>Host.exe,双击Host.exe文件,启动服务;

 

第五步:添加客户端应用程序,文件——>添加——>新建项目,选择控制台应用程序,取名Client。接下来,添加服务引用,将服务端的地址填入地址(A)栏,点击前往,出现HelloWCFService服务,修改命名空间为HelloWCF,如下图,点击“确定”即可。

接下来,在Client的Program.cs中添加引用

using Client.HelloWCF;//"HelloWCF为引用服务的命名空间",这个引用必须添加

在程序中实例化一个代理“Proxy

  HelloWCF.HelloWCFServiceClient proxy = new HelloWCFServiceClient();

客户端通过这个代理与服务端进行通信。

代码如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Client.HelloWCF;//"HelloWCF为引用服务的命名空间",这个引用必须添加

 

namespace Client

{

    class Program

    {

        static void Main(string[] args)

        {

            HelloWCF.HelloWCFServiceClient proxy = new HelloWCFServiceClient();

            string str = proxy.HelloWCF("欢迎来到WCF村!");

            Console.WriteLine(str);

            Console.ReadLine();

        }

    }

}

 

 

第六步:运行结果如下: