zoukankan      html  css  js  c++  java
  • 重温WCF之一个服务实现多个契约(二)

     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();
        }  

    使用代码的方式:

    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();
                }

    使用配置文件的方式:

    <?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>
       using (ServiceHost host = new ServiceHost(typeof(ServiceImp)))
                {
                    host.Opened += delegate
                    {
                        MessageBox.Show("Service2已经启动");
                    };
                    host.Open();
                }
  • 相关阅读:
    性能测试培训:性能瓶颈分析思路
    (国内)完美下载Android源码Ubuntu版
    (国内)完美下载android源代码(文章已经丢失)
    【翻译】Ext JS最新技巧——2015-10-21
    ubuntu 中 eclipse 的菜单栏 显示问题
    谷歌代码库已超过 20 亿行代码,他们是如何管理的?
    架构方面的资料集锦
    Android Studio 使用 Gradle 打包 Jar
    【翻译】Ext JS最新技巧——2015-8-11
    【翻译】在Ext JS 6通用应用程序中使用既共享又特定于视图的代码
  • 原文地址:https://www.cnblogs.com/yxlblogs/p/3761424.html
Copyright © 2011-2022 走看看