WCFの1つのサービスを再温して複数の契約を実現する(二)

7695 ワード

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

            }