zoukankan      html  css  js  c++  java
  • WCF客户端和服务的实现

    WCF客户端和服务

    ?服务器端:

    – 定义和实现服务契约

    – 为服务类型构建ServiceHost实例,暴露endpoints

    – 打开通讯通道

    ?客户端:

    – 需要服务契约的一个副本和关于endpoints的信息

    – 为特定的endpoint构建通信通道并且调用操作

    clip_image002

    客户端的客户进程中,有一个代理,该代理主要功能是完成客户进程和主机进程之间的通信。Proxy并不直接和主机的Endpoint通信,而是由客户端自己提供一个Endpoint和主机的Endpoint通信。

    服务端有一个主机Host,提供服务,服务由Endpoint向外发布接口。

    消息的通信由两端的Endpoint通信。

    代码示例:

    服务端代码

    using System;

    using System.ServiceModel;

    namespace HelloIndigo

    {

    [ServiceContract(Namespace="http://www.monkeyfu.net")]

    public interface IHelloIndigoService

    {

    [OperationContract]

    string HelloIndigo(string message);

    }

    public class HelloIndigoService : IHelloIndigoService

    {

    #region IHelloIndigoService Members

    public string HelloIndigo(string message)

    {

    return string.Format("Receivied message at{0}:{1}", DateTime.Now, message);

    }

    #endregion

    }

    }

    此处主要定义了服务契约。在WCF中,定义一个契约使用的是ServiceContract。只有定义了OperationContract的方法才会被放入服务中。

    宿主程序

    通常情况,服务宿主程序需要使用ServiceHost类,当使用IIS或者WAS作为宿主程序的时候IIS和WAS会自动创建ServiceHost类型。当自定义宿主服务的时候,需要手动创建ServiceHost对象。

    namespace Host

    {

    class Program

    {

    static void Main(string[] args)

    {

    using (ServiceHost host = new ServiceHost(typeof(HelloIndigo.HelloIndigoService)))//手动创建ServiceHost

    {

    host.AddServiceEndpoint(typeof(HelloIndigo.IHelloIndigoService), new NetTcpBinding(), "net.tcp://localhost:9009/HelloIndigo");

    host.Open();

    Console.ReadLine();

    }

    }

    }

    }

    客户端

    客户端的契约要和服务端的保持一致

    namespace Client

    {

    //和服务端的契约定义要一致。

    [ServiceContract(Namespace = "http://www.monkeyfu.net")]

    public interface IHelloIndigoService

    {

    [OperationContract]

    string HelloIndigo(string message);

    }

    class Program

    {

    static void Main(string[] args)

    {

    IHelloIndigoService proxy = ChannelFactory<IHelloIndigoService>.CreateChannel(new NetTcpBinding(), new EndpointAddress("net.tcp://localhost:9009/HelloIndigo"));

    string s = proxy.HelloIndigo("Hello from client...");

    Console.WriteLine(s);

    Console.ReadLine();

    }

    }

    }

    Endpoint

    Endpoint实际上由ABC三个要素组成Address,Binding,Contract

    clip_image004

    使用配置文件实现服务和Endpoint

    前面的方法是通过手工编程实现的,事实上,可以不用写代码而通过配置文件实现服务调用。

    只使用代码而不用配置文件的情况不适合IIS为宿主的情况,IIS宿主必须使用配置文件配置WCF的ServiceHost。

    主要步骤如下:

    配置Host的配置文件如下:

    <?xml version="1.0" encoding="utf-8" ?>

    <configuration>

    <system.serviceModel>

    <services>

    <service name="HelloIndigo.HelloIndigoService" behaviorConfiguration="serviceBehavior">

    <endpoint binding="basicHttpBinding" contract="HelloIndigo.IHelloIndigoService" address="HelloIndigo" />

    <endpoint binding="mexHttpBinding" contract="IMetadataExchange" address="mex" />

    <host>

    <baseAddresses>

    <add baseAddress="http://localhost:8008"/>

    </baseAddresses>

    </host>

    </service>

    </services>

    <behaviors>

    <serviceBehaviors>

    <behavior name="serviceBehavior">

    <serviceMetadata httpGetEnabled="true" />

    </behavior>

    </serviceBehaviors>

    </behaviors>

    </system.serviceModel>

    </configuration>

    配置完毕后,宿主代码可以如下写法, 不再需要添加endpoint

    static void Main(string[] args)

    {

    using (ServiceHost host = new ServiceHost(typeof(HelloIndigo.HelloIndigoService)))//手动创建ServiceHost

    {

    //host.AddServiceEndpoint(typeof(HelloIndigo.IHelloIndigoService), new NetTcpBinding(), "net.tcp://localhost:9009/HelloIndigo");

    host.Open();

    Console.ReadLine();

    }

    }

    为客户端添加Service Reference。对于客户端代码,可以不必要再写契约定义,因为添加完Service Reference之后,系统自动生成很多相关代码。按如下方法写即可。

    添加完毕后可以适当修改app.config

    namespace Client

    {

    ////和服务端的契约定义要一致。

    //[ServiceContract(Namespace = "http://www.monkeyfu.net")]

    //public interface IHelloIndigoService

    //{

    // [OperationContract]

    // string HelloIndigo(string message);

    //}

    class Program

    {

    static void Main(string[] args)

    {

    //IHelloIndigoService proxy = ChannelFactory<IHelloIndigoService>.CreateChannel(new NetTcpBinding(), new EndpointAddress("net.tcp://localhost:9009/HelloIndigo"));

    Client.ServiceReference1.HelloIndigoServiceClient proxy = new Client.ServiceReference1.HelloIndigoServiceClient();

    string s = proxy.HelloIndigo("Hello from client...");

    Console.WriteLine(s);

    Console.ReadLine();

    }

    }

    }





















    本文转自cnn23711151CTO博客,原文链接: http://blog.51cto.com/cnn237111/572975,如需转载请自行联系原作者

  • 相关阅读:
    ASP.NET 备份恢复SqlServer数据库
    ASP.NET MVC3.0 Razor 视图模板 语法
    ASP.NET 缓存
    代码生成框架
    C#中HashTable的用法
    C# 概念 委托和事件
    Web Service 系列 → Web Service 简介
    CDN 内容分发网络
    HarmonyOS开发者创新大赛
    #2020征文手表#【图解鸿蒙】多组示例演示三个样式的组合用法
  • 原文地址:https://www.cnblogs.com/twodog/p/12138599.html
Copyright © 2011-2022 走看看