• 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);
            }
        }
    }
  • 相关阅读:
    WebApp触屏版网站开发要点
    Web前端开发规范手册
    Web页面切图和CSS注意事项
    javascript中的一些基本方法收藏
    浅谈浏览器兼容性问题
    html 5 标签分类
    前端meta知多少
    浏览器内核信息整理
    Javascript中的undefined、null、""、0值和false的区别总结
    JQUERY中的AJAX应用
  • 原文地址:https://www.cnblogs.com/kexb/p/7436397.html
Copyright © 2020-2023  润新知