wcf服务契约继承

a. 服务端
1.契约 使用了继承
using System;
using System.ServiceModel;

namespace WCF.Chapter2.InheritanceReworked.Host
{
    [ServiceContract]
    public interface IHuman
    {
        [OperationContract]
        string HumanSay();
    }

    [ServiceContract]
    public interface IMan : IHuman
    {
        [OperationContract]
        string ManSay();
    }

    [ServiceContract]
    public interface IWoman : IHuman
    {
        [OperationContract]
        string WomanSay();
    }
}
2.服务实现 实现了自己的具体的接口
using System;
using System.ServiceModel;

namespace WCF.Chapter2.InheritanceReworked.Host
{
    public class ManService : IMan
    {
        public string HumanSay()
        {
            return " 我是人,我会思考!";
        }

        public string ManSay()
        {
            return "我是男人,我力气比较大!";
        }
    }

    public class WomanService : IWoman
    {
        public string HumanSay()
        {
            return " 我是人,我会思考!";
        }

        public string WomanSay()
        {
            return "我是女人,我爱漂亮!";
        }
    }
}

3.服务终结点配置
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="WCF.Chapter2.InheritanceReworked.Host.ManService" behaviorConfiguration="MEX">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8000"/>
          </baseAddresses>
        </host>
        <endpoint address="http://localhost:8001/Man" binding="basicHttpBinding" contract="WCF.Chapter2.InheritanceReworked.Host.IMan"></endpoint>
      </service>

      <service name="WCF.Chapter2.InheritanceReworked.Host.WomanService" behaviorConfiguration="MEX">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:9000"/>
          </baseAddresses>
        </host>
        <endpoint address="net.tcp://localhost:9001/Woman" binding="netTcpBinding" contract="WCF.Chapter2.InheritanceReworked.Host.IWoman"></endpoint>
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="MEX">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>
4.服务寄宿开启
using System;
using System.ServiceModel;

namespace WCF.Chapter2.InheritanceReworked.Host
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost hostMan = new ServiceHost(typeof(ManService)))
            {
                hostMan.Opened += delegate
                {
                    Console.WriteLine("Man服务已开启...");
                };
                hostMan.Open();

                using (ServiceHost hostWoman = new ServiceHost(typeof(WomanService)))
                {
                    hostWoman.Opened += delegate
                    {
                        Console.WriteLine("Woman服务已开启...");
                    };
                    hostWoman.Open();

                    Console.ReadLine();
                }
                Console.WriteLine("Woman服务已关闭...");

                Console.ReadLine();
            }
            Console.WriteLine("Man服务已关闭...");
        }
    }
}

b. 客户端
1.客户端等效契约 除了命名空间不一样其他的都一样
using System;
using System.ServiceModel;

namespace WCF.Chapter2.InheritanceReworked.Client
{
    [ServiceContract]
    public interface IHuman
    {
        [OperationContract]
        string HumanSay();
    }

    [ServiceContract]
    public interface IMan : IHuman
    {
        [OperationContract]
        string ManSay();
    }

    [ServiceContract]
    public interface IWoman : IHuman
    {
        [OperationContract]
        string WomanSay();
    }
}
2.人类代理 男人和女人在服务端都实现了他,所以既可以是男人代表人,也可以是女人去代表人
using System;
using System.ServiceModel;

namespace WCF.Chapter2.InheritanceReworked.Client
{
    public class HumanProxy : ClientBase<IHuman>, IHuman
    {
        public HumanProxy()
        { }

        public HumanProxy(string configurationName) :
            base(configurationName)
        { }

        public string HumanSay()
        {
            return base.Channel.HumanSay();
        }

    }
}
3.由2的结论这里给出终结点配置
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <client>
      <endpoint name="human_man" address="http://localhost:8001/Man" binding="basicHttpBinding" contract="WCF.Chapter2.InheritanceReworked.Client.IHuman"></endpoint>
      <endpoint name="man" address="http://localhost:8001/Man" binding="basicHttpBinding" contract="WCF.Chapter2.InheritanceReworked.Client.IMan"></endpoint>
      <endpoint name="human_woman" address="net.tcp://localhost:9001/Woman" binding="netTcpBinding" contract="WCF.Chapter2.InheritanceReworked.Client.IHuman"></endpoint>
      <endpoint name="woman" address="net.tcp://localhost:9001/Woman" binding="netTcpBinding" contract="WCF.Chapter2.InheritanceReworked.Client.IWoman"></endpoint>
    </client>
  </system.serviceModel>
</configuration>
3.manProxy
using System;
using System.ServiceModel;

namespace WCF.Chapter2.InheritanceReworked.Client
{
    public class ManProxy : ClientBase<IMan>, IMan
    {
        public ManProxy()
        { }

        public ManProxy(string configurationName) :
            base(configurationName)
        { }

        public string HumanSay()
        {
            return base.Channel.HumanSay();
        }

        public string ManSay()
        {
            return base.Channel.ManSay();
        }
    }
}
4.womenproxy
using System;
using System.ServiceModel;

namespace WCF.Chapter2.InheritanceReworked.Client
{
    public class WomanProxy : ClientBase<IWoman>, IWoman
    {
        public WomanProxy()
        { }

        public WomanProxy(string configurationName) :
            base(configurationName)
        { }

        public string HumanSay()
        {
            return base.Channel.HumanSay();
        }

        public string WomanSay()
        {
            return base.Channel.WomanSay();
        }

    }
}

5.客户端调用代理
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WCF.Chapter2.InheritanceReworked.Client
{
    class Program
    {
        static void Main(string[] args)
        {
            using (HumanProxy humanProxy_man = new HumanProxy("human_man"))
            {
                Console.WriteLine("humanProxy_man:");
                Console.WriteLine(humanProxy_man.HumanSay());
                Console.WriteLine("");
            }

            using (HumanProxy humanProxy_woman = new HumanProxy("human_woman"))
            {
                Console.WriteLine("humanProxy_woman:");
                Console.WriteLine(humanProxy_woman.HumanSay());
                Console.WriteLine("");
            }

            using (ManProxy manProxy = new ManProxy("man"))
            {
                Console.WriteLine("manProxy_human:");
                Console.WriteLine(manProxy.HumanSay());
                Console.WriteLine("");

                Console.WriteLine("manProxy_man:");
                Console.WriteLine(manProxy.ManSay());
                Console.WriteLine("");
            }

            using (WomanProxy womanProxy = new WomanProxy("woman"))
            {
                Console.WriteLine("womanProxy_human:");
                Console.WriteLine(womanProxy.HumanSay());
                Console.WriteLine();

                Console.WriteLine("womanProxy_woman:");
                Console.WriteLine(womanProxy.WomanSay());
                Console.WriteLine();
            }

            Console.ReadLine();
        }
    }
}
时间: 2024-08-05 06:47:14

wcf服务契约继承的相关文章

WCF分布式开发步步为赢(6):WCF服务契约继承与分解设计

上一节我们学习了WCF分布式开发步步为赢(5)服务契约与操作重载部分.今天我们来继续学习WCF服务契约继承和服务分解设计相关的知识点.WCF服务契约继承有何优势和缺点?实际项目里契约设计有什么原则和依据?面向对象的设计经验有何值得借鉴的地方?这里我们会一一给出详细的介绍.本文首先介绍的是WCF服务中契约继承的一些概念.例子代码分析,其次来讲解服务契约的设计问题.首先介绍的也是进行服务设计的必要性,服务设计的原则,示例代码分析.最后是全文的总结部分.结构如下:[1]OO面向对象设计原则,[2]服务

wcf服务契约代理链

意图:为了是客户端代理呈现出面向对象的多态的特征 a. 服务端 1.契约 实现了契约的继承这个在服务端是一点问题没有,因为oprationcontract可以继承,虽然DataContract不能实现继承,注意IAnimal和IDog都是契约,但是我们通常喜欢用最 具体的那个契约来发布服务,因为他最丰富 using System; using System.ServiceModel; namespace WCF.Chapter2.InheritanceProxyChaining.Host { [

wcf服务契约的重载

a. 服务端 1.服务端 契约用OperationContract的Name实现重载 using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace WCF.Chapter2.Overloading.Host { [ServiceContract

跟我一起学WCF(6)——深入解析服务契约[下篇]

一.引言 在上一篇博文中,我们分析了如何在WCF中实现操作重载,其主要实现要点是服务端通过ServiceContract的Name属性来为操作定义一个别名来使操作名不一样,而在客户端是通过重写客户端代理类的方式来实现的.在这篇博文中将分享契约继承的实现. 二.WCF服务契约继承实现的限制 首先,介绍下WCF中传统实现契约继承的一个方式,下面通过一个简单的WCF应用来看看不做任何修改的情况下是如何实现契约继承的.我们还是按照之前的步骤来实现下这个WCF应用程序. 步骤一:实现WCF服务 在这里,我

跟我一起学WCF(5)——深入解析服务契约[上篇]

一.引言 在上一篇博文中,我们创建了一个简单WCF应用程序,在其中介绍到WCF最重要的概念又是终结点,而终结点又是由ABC组成的.对于Address地址也就是告诉客户端WCF服务所在的位置,而Contract又是终结点中比较重要的一个内容,在WCF中,契约包括服务契约.数据契约.消息契约和错误契约,在本篇博文将解析下数据契约的内容,关于其他三种契约将会后面的博文中陆续介绍. 二.引出问题——WCF操作重载限制 C#语言是支持操作重载的,然而在WCF实现操作重载有一定的限制.错误的操作重载实例:

WCF探索之旅(四)——程序中WCF服务整合

我们在之前的博客已经完成过实例,大家也看到了如何使用WCF服务: 添加服务引用-->输入服务地址-->实例化服务-->调用服务方法 那么今天为什么要再次说"程序中WCF服务整合"这个话题? 用过WebService的人都知道,随着服务的增多,管理WebService是一个非常繁重的工作.好了,今天我们的目标来了--让WCF服务变得整齐.有序.易管理! 首先,我们建立一个工厂类,这个工厂用来实例化我们的服务.这样做的好处是,所有的服务都是由工厂实例化的. 然后我们要建立

WCF服务寄宿IIS与Windows服务

WCF是Windows平台下程序间通讯的应用程序框架.整合和 .net Remoting,WebService,Socket的机制,是用来开发windows平台上分布式开发的最佳选择.wcf程序的运行需要一个宿主ServiceHost,我们可以选用控制台应用程序,也可以选择IIS寄宿,还可以选择windows 服务寄宿.相较与控制台程序,IIS,和Windows服务比较稳定.而且大家不会时不时的去重启下IIS下的网站,或者windows服务. 在IIS下寄宿Wcf 我们新建一个类库项目 在项目下

实现jquery.ajax及原生的XMLHttpRequest调用WCF服务的方法

废话不多说,直接讲解实现步骤 一.首先我们需定义支持WEB HTTP方法调用的WCF服务契约及实现服务契约类(重点关注各attribute),代码如下: //IAddService.cs namespace WcfService1 { [ServiceContract] public interface IAddService { [OperationContract] [WebInvoke(Method="POST",RequestFormat=WebMessageFormat.Js

WCF服务编程 读书笔记——第2章 服务契约

操作重载诸如 C++ 和 C# 等编程语言都支持方法重载,即允许具有相同名称的两个方法可以定义不同的参数.例如,如下的 C# 接口就是有效的定义: interface ICalculator { int Add(int arg1,int arg2); double Add(double arg1,double arg2); } 然而,基于 WSDL 的操作却不支持操作重载.因此,在编译如下的契约定义时,装载服务宿主就会抛出 InvalidOperationException异常: // 无效的契