码迷,mamicode.com
首页 > 其他好文 > 详细

重温WCF之一个服务实现多个契约(二)

时间:2014-05-31 11:08:00      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:des   c   style   class   blog   code   

bubuko.com,布布扣
 public class ServiceImp : IService1,IService2,IService3
    {
        public string SayHelloA()
        {
            return "你好,这是第一个服务协定。";  
        }

        public string SayHelloB()
        {
            return "你好,这是第二个服务协定。";  
        }

        public string SayHelloC()
        {
            return "你好,这是第三个服务协定。";  
        }
    }
   

    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string SayHelloA();
    }

    [ServiceContract]
    public interface IService2
    {
        [OperationContract]
        string SayHelloB();
    }

    [ServiceContract]
    public interface IService3
    {
        [OperationContract]
        string SayHelloC();
    }  
bubuko.com,布布扣

使用代码的方式:

bubuko.com,布布扣
using (ServiceHost host = new ServiceHost(typeof(ServiceImp)))
            {
                host.AddServiceEndpoint(typeof (IService1), new WSHttpBinding(), "http://127.0.0.1:8888/service1");
                host.AddServiceEndpoint(typeof(IService2), new WSHttpBinding(), "http://127.0.0.1:8888/service2");
                host.AddServiceEndpoint(typeof(IService3), new WSHttpBinding(), "http://127.0.0.1:8888/service3");
                ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
                behavior.HttpGetEnabled = true;
                behavior.HttpGetUrl = new Uri("http://127.0.0.1:8888/service");  //httpGetUrl客户端引用的地址
                host.Description.Behaviors.Add(behavior);  
                host.Opened += delegate
                {
                    MessageBox.Show("Service2已经启动");
                };
                host.Open();
            }
bubuko.com,布布扣

使用配置文件的方式:

bubuko.com,布布扣
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="serviceBehavior">
          <!--httpGetUrl客户端引用的地址-->
          <serviceMetadata httpGetEnabled="true" httpGetUrl="http://127.0.0.1:8888/service"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="WindowsFormsApplication2.ServiceImp" behaviorConfiguration="serviceBehavior">
        <endpoint address="http://127.0.0.1:8888/service1" binding="wsHttpBinding" contract="WindowsFormsApplication2.IService1"></endpoint>
        <endpoint address="http://127.0.0.1:8888/service2" binding="wsHttpBinding" contract="WindowsFormsApplication2.IService2"></endpoint>
        <endpoint address="http://127.0.0.1:8888/service3" binding="wsHttpBinding" contract="WindowsFormsApplication2.IService3"></endpoint>
      </service>
    </services>
  </system.serviceModel>
</configuration>
bubuko.com,布布扣
bubuko.com,布布扣
   using (ServiceHost host = new ServiceHost(typeof(ServiceImp)))
            {
                host.Opened += delegate
                {
                    MessageBox.Show("Service2已经启动");
                };
                host.Open();
            }
bubuko.com,布布扣

 

重温WCF之一个服务实现多个契约(二),布布扣,bubuko.com

重温WCF之一个服务实现多个契约(二)

标签:des   c   style   class   blog   code   

原文地址:http://www.cnblogs.com/yxlblogs/p/3761424.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!