zoukankan      html  css  js  c++  java
  • 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]
            public interface IContract
            {
                [OperationContract(Name = "say1")]
                string say();
    
                [OperationContract(Name = "say2")]
                string say(string str);
            }
        
    }
    2.服务实现
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.Text;
    
    namespace WCF.Chapter2.Overloading.Host.Service
    {
        // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的类名“Service1”。
        public class Service1 : IContract
        {
            public string say()
            {
                return "老鼠扛刀,满街找猫";
            }
    
            public string say(string str)
            {
                return str;
            }
        }
    
    }
    3.服务寄宿
    using System;
    using System.ServiceModel;
    using WCF.Chapter2.Overloading.Host.Service;
    namespace WCF.Chapter2.Overloading.Host
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (ServiceHost host = new ServiceHost(typeof(Service1)))
                {
                    host.Opened += delegate
                    {
                        Console.WriteLine("服务已开启...");
                    };
                    host.Open();
                    Console.ReadLine();
                }
    
                Console.WriteLine("服务已关闭...");
                Console.ReadLine();
            }
        }
    }
    4.终结点设置
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
        <behaviors>
          <serviceBehaviors>
            <behavior name="MEX">
              <serviceMetadata httpGetEnabled="true"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
    
        <services>
          <service name="WCF.Chapter2.Overloading.Host.Service.Service1" behaviorConfiguration="MEX">
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:8000"/>
              </baseAddresses>
            </host>
    
            <endpoint address="http://localhost:8001/say" binding="basicHttpBinding" contract="WCF.Chapter2.Overloading.Host.IContract"></endpoint>
          </service>
        </services>
      </system.serviceModel>
    </configuration>
    b. 客户端
    1.客户端契约 这个很重要需要和服务端分开不能用同一个契约,而是实现了一个等效契约,所以此处说明终结点三要素A,B,C 的A,B必须一模一样但是C只要等效就可以
    using System;
    using System.ServiceModel;
    
    namespace WCF.Chapter2.Overloading.Client
    {
        [ServiceContract]
        public interface IContract
        {
            [OperationContract(Name = "say1")]
            string say();
    
            [OperationContract(Name = "say2")]
            string say(string str);
        }
    }
    2.客户端代理 本质也是channelfactory.createchannel
    using System;
    using System.ServiceModel;
    
    namespace WCF.Chapter2.Overloading.Client
    {
        public class ClientProxy : ClientBase<IContract>, IContract
        {
            public ClientProxy()
            { }
    
            public ClientProxy(string configurationName) :
                base(configurationName)
            { }
    
            public string say()
            {
                return base.Channel.say();
            }
    
            public string say(string str)
            {
                return base.Channel.say(str);
            }
        }
    }
    
    3.客户端终结点配置
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
        <client>
          <endpoint address="http://localhost:8001/say" binding="basicHttpBinding" contract="WCF.Chapter2.Overloading.Client.IContract"></endpoint>
        </client>
      </system.serviceModel>
    </configuration>
    4.调用代码
    using System;
    using System.ServiceModel;
    
    namespace WCF.Chapter2.Overloading.Client
    {
        public class ClientProxy : ClientBase<IContract>, IContract
        {
            public ClientProxy()
            { }
    
            public ClientProxy(string configurationName) :
                base(configurationName)
            { }
    
            public string say()
            {
                return base.Channel.say();
            }
    
            public string say(string str)
            {
                return base.Channel.say(str);
            }
        }
    }
  • 相关阅读:
    当公有云Azure拥抱Docker容器技术
    .NET AJAX实例
    漫谈Ajax在.Net中的使用
    .NET运用AJAX 总结及其实例
    Excel自动从身份证中提取生日、性别、年龄
    ASP.NET 与 Ajax 的实现方式
    windows下编辑器Emacs的安装与配置
    2013.10.26工作Fighting(1)
    Jquery操作下拉框(DropDownList)实现取值赋值
    js调用后台,后台调用前台等方法总结
  • 原文地址:https://www.cnblogs.com/kexb/p/7436397.html
Copyright © 2011-2022 走看看