3. 通过编码方式实现基于 WCF 服务的 HelloWorld

2019-05-13  本文已影响0人  璎珞纨澜

项目基本结构

图 1 的 Client 工程对应图 2 的 Client App。
图 1 的 HelloWorldService 工程对应图 2 的 Service,具体的 WCF 服务是由 HelloWorldService 提供的。
图 1 的 Host 工程对应图 2 的 ServiceHost,要为HelloWorldService这个服务来提供一个运行的环境。

图1:项目目录
图2:WCF 架构

1.WCF 服务创建

1:IHelloWorldService.cs

using System;
using System.ServiceModel;

namespace HelloWorldService
{
    /*加了契约的接口*/
    [ServiceContract(Namespace="http://www.monkeyfu.net/")]
    public interface IHelloWorldService
    {
        [OperationContract]
        string GetHelloWorld();
    }
}
  1. HelloWorldService.cs
using System;
using System.ServiceModel;

namespace HelloWorldService
{
    public class HelloWorldService : IHelloWorldService
    {
        public string GetHelloWorld()
        {
            return string.Format("消息收到了在{0}:{1}", DateTime.Now, "Hello World!");
        }
    }
}

2. 托管 WCF 服务

using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using HelloWorldService;

namespace Host
{
    class Program
    {
        static void Main(string[] args)
        {
            //step1 启动了一个新的主机,这个主机里面现在运行着一个服务
            using (ServiceHost host = new ServiceHost(typeof(HelloWorldService.HelloWorldService)))
            {
                //step2 用来提供Service向外发布的接口以实现客户端和服务端之间的通信
                host.AddServiceEndpoint(typeof(IHelloWorldService), new NetTcpBinding(), "net.tcp://localhost:9000/HelloWorldService");
                //step3 打开主机
                host.Open();
                Console.ReadLine();
            }
        }
    }
}

3. WCF 服务的调用

using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;

namespace Client
{
    //step1 创建服务契约的一个副本
    [ServiceContract(Namespace = "http://www.monkeyfu.net/")]
    public interface IHelloWorldService
    {
        [OperationContract]
        string GetHelloWorld();
    }

    class Program
    {
        static void Main(string[] args)
        {
            //step2 创建Proxy代理
            IHelloWorldService proxy = ChannelFactory<IHelloWorldService>.CreateChannel(new NetTcpBinding(), new EndpointAddress("net.tcp://localhost:9000/HelloWorldService"));
            //step3 通过代理访问服务
            string Result = proxy.GetHelloWorld();
            Console.WriteLine(Result);
            Console.ReadLine();
        }
    }
}

Client端通过Tcp协议去访问了Host端里面的HelloWorldService服务里面的GetHelloWorld这个方法, 这样我们就完成了一个简单的基于Tcp协议的分布式的调用:

  1. 首先启动 Host 端
  2. 然后启动 Client 端


    运行时效果

总结

  1. 定义和实现服务契约:
    在服务端,不但要完成具体服务的业务逻辑,还要完成这个服务相应的契约
  2. 为服务类型创建 ServiceHost 实例,暴露 Endpoint:
    创建 ServiceHost 实例可以理解为我们创建了一个服务,并把这个服务放到服务器里来处理,同时要暴露出 Endpoint 端点,使得客户端来访问 ServiceHost 里面对应的 Service 服务
  3. 打开通信的通道:
    使得客户端能够访问服务端的这些服务
  1. 需要服务契约的一个副本和关于 Endpoints 端点的信息:
    服务契约的一个副本可以理解为在这个服务当中包含的契约的一个接口,虽然说客户端不需要服务的具体的业务逻辑,但是客户端需要这个服务当中包含契约的一个接口,来告诉客户端它如何与服务端进行通信应该遵循哪些规则,Endpoint端点的信息主要包含了服务器端服务的具体地址以及使用什么样的协议进行具体的通信
  2. 为特定的Endpoints构建通信通道并且执行调用操作已完成我们具体的服务:
    下面这个图三更好的说明了客户端和服务端具体的Endpoint的一个作用

我们就以ABC来进行一下阐述,一个消息要想传送出去或者想要接收到的话必须要有ABC这三样东西的,这三样是缺一不可的:

上一篇 下一篇

猜你喜欢

热点阅读