wcf服务契约的重载

Posted 听哥哥的话

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
        }
    }
}

 

以上是关于wcf服务契约的重载的主要内容,如果未能解决你的问题,请参考以下文章

WCF - WCF基本概念和简单WCF服务端创建

wcf服务契约代理链

wcf服务契约继承

WCF技术剖析之十八:消息契约(Message Contract)和基于消息契约的序列化

C# WCF之用接口创建服务契约部署及客户端连接

WCF服务编程一书中写到:契约操作不能使用引用对象参数,只允许使用基本类型或者数据契约?