方法 : コントラクトを SOAP クライアントおよび Web クライアントに公開する

Windows Communication Foundation (WCF) では、既定でエンドポイントは SOAP クライアントでのみ利用できます。SOAP 以外のクライアントでもエンドポイントを利用できるようにする方法については、「方法 : 基本的な WCF Web HTTP サービスを作成する」を参照してください。状況によっては、同じコントラクトを Web エンドポイントと SOAP エンドポイントのどちらとしても利用できることが望ましい場合があります。ここでは、これを実現する方法の例について示します。

サービス コントラクトを定義するには

  1. 次のコードのように、ServiceContractAttributeWebInvokeAttribute および WebGetAttribute の各属性でマークされたインターフェイスを使用して、サービス コントラクトを定義します。

    <ServiceContract()> _
    Public Interface IService
    
        <OperationContract()> _
        <WebGet()> _
        Function EchoWithGet(ByVal s As String) As String
    
        <OperationContract()> _
        <WebInvoke()> _
        Function EchoWithPost(ByVal s As String) As String
    End Interface
    
    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        [WebGet]
        string EchoWithGet(string s);
    
        [OperationContract]
        [WebInvoke]
        string EchoWithPost(string s);
    }
    
    Bb412196.note(ja-jp,VS.100).gif注 :
    既定では、WebInvokeAttribute は POST 呼び出しを操作にマッピングします。ただし、"method=" パラメーターを指定することで、操作にマッピングするメソッドを指定できます。WebGetAttribute には "method=" パラメーターがないため、サービス操作には GET 呼び出しのみがマッピングされます。

  2. 次のコードに示すように、サービス コントラクトを実装します。

    Public Class Service
        Implements IService
        Public Function EchoWithGet(ByVal s As String) As String Implements IService.EchoWithGet
            Return "You said " + s
        End Function
    
        Public Function EchoWithPost(ByVal s As String) As String Implements IService.EchoWithPost
            Return "You said " + s
        End Function
    End Class
    
    public class Service : IService
    {
        public string EchoWithGet(string s)
        {
            return "You said " + s;
        }
    
        public string EchoWithPost(string s)
        {
            return "You said " + s;
        }
    }
    

サービスをホストするには

  1. 次のコードに示すように、ServiceHost オブジェクトを作成します。

    Dim host As New ServiceHost(GetType(Service), New Uri("https://localhost:8000"))
    
    ServiceHost host = new ServiceHost(typeof(Service), new Uri("https://localhost:8000"));
    
  2. 次のコードに示すように、SOAP 以外のエンドポイント用に、ServiceEndpointBasicHttpBinding を追加します。

    host.AddServiceEndpoint(GetType(IService), New BasicHttpBinding(), "Soap")
    
    host.AddServiceEndpoint(typeof(IService), new BasicHttpBinding(), "Soap");
    
  3. 次のコードに示すように、SOAP 以外のエンドポイント用に、ServiceEndpointWebHttpBinding を追加し、エンドポイントに WebHttpBehavior を追加します。

    Dim endpoint As ServiceEndpoint
    endpoint = host.AddServiceEndpoint(GetType(IService), New WebHttpBinding(), "Web")
    endpoint.Behaviors.Add(New WebHttpBehavior())
    
    ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(IService), new WebHttpBinding(), "Web");
    endpoint.Behaviors.Add(new WebHttpBehavior());
    
  4. 次のコードに示すように、Open()ServiceHost インスタンスで呼び出して、サービス ホストを開きます。

    host.Open()
    
    host.Open();
    

Internet Explorer で GET にマッピングされたサービス操作を呼び出すには

  1. Internet Explorer を開いて「https://localhost:8000/Web/EchoWithGet?s=Hello, world!」と入力し、Enter キーを押します。この URL には、サービスのベース アドレス ("https://localhost:8000/") が含まれており、エンドポイントの相対アドレス ("")、呼び出しを行うサービス操作 ("EchoWithGet")、疑問符の後にアンパサンド (&) で区切られた名前付きパラメーターのリストが続きます。

コードから Web エンドポイントにあるサービス操作を呼び出すには

  1. 次のコードに示すように、WebChannelFactory のインスタンスを using ブロック内に作成します。

    Using wcf As New WebChannelFactory(Of IService)(New Uri("https://localhost:8000/Web"))
    
    using (WebChannelFactory<IService> wcf = new WebChannelFactory<IService>(new Uri("https://localhost:8000/Web")))
    
Bb412196.note(ja-jp,VS.100).gif注 :
Close()using ブロックの末尾のチャネルで自動的に呼び出されます。

  1. 次のコードに示すように、チャネルを作成してサービスを呼び出します。

    Dim channel As IService = wcf.CreateChannel()
    
    Dim s As String
    
    Console.WriteLine("Calling EchoWithGet by HTTP GET: ")
    s = channel.EchoWithGet("Hello, world")
    Console.WriteLine("   Output:  {0}", s)
    
    Console.WriteLine("")
    Console.WriteLine("This can also be accomplished by navigating to")
    Console.WriteLine("https://localhost:8000/Web/EchoWithGet?s=Hello, world!")
    Console.WriteLine("in a web browser while this sample is running.")
    
    Console.WriteLine("")
    
    Console.WriteLine("Calling EchoWithPost by HTTP POST: ")
    s = channel.EchoWithPost("Hello, world")
    Console.WriteLine("   Output:  {0}", s)
    
    IService channel = wcf.CreateChannel();
    
    string s;
    
    Console.WriteLine("Calling EchoWithGet by HTTP GET: ");
    s = channel.EchoWithGet("Hello, world");
    Console.WriteLine("   Output: {0}", s);
    
    Console.WriteLine("");
    Console.WriteLine("This can also be accomplished by navigating to");
    Console.WriteLine("https://localhost:8000/Web/EchoWithGet?s=Hello, world!");
    Console.WriteLine("in a web browser while this sample is running.");
    
    Console.WriteLine("");
    
    Console.WriteLine("Calling EchoWithPost by HTTP POST: ");
    s = channel.EchoWithPost("Hello, world");
    Console.WriteLine("   Output: {0}", s);
    

SOAP エンドポイントにあるサービス操作を呼び出すには

  1. 次のコードに示すように、ChannelFactory のインスタンスを using ブロック内に作成します。

    Using scf As New ChannelFactory(Of IService)(New BasicHttpBinding(), "https://localhost:8000/Soap")
    
    using (ChannelFactory<IService> scf = new ChannelFactory<IService>(new BasicHttpBinding(), "https://localhost:8000/Soap"))
    
  2. 次のコードに示すように、チャネルを作成してサービスを呼び出します。

    Dim channel As IService = scf.CreateChannel()
    
    Dim s As String
    
    Console.WriteLine("Calling EchoWithGet on SOAP endpoint: ")
    s = channel.EchoWithGet("Hello, world")
    Console.WriteLine("   Output:  {0}", s)
    
    Console.WriteLine("")
    
    Console.WriteLine("Calling EchoWithPost on SOAP endpoint: ")
    s = channel.EchoWithPost("Hello, world")
    Console.WriteLine("   Output:  {0}", s)
    
    IService channel = scf.CreateChannel();
    
    string s;
    
    Console.WriteLine("Calling EchoWithGet on SOAP endpoint: ");
    s = channel.EchoWithGet("Hello, world");
    Console.WriteLine("   Output: {0}", s);
    
    Console.WriteLine("");
    
    Console.WriteLine("Calling EchoWithPost on SOAP endpoint: ");
    s = channel.EchoWithPost("Hello, world");
    Console.WriteLine("   Output: {0}", s);
    

サービス ホストを閉じるには

  1. 次のコードに示すように、サービス ホストを閉じます。

    host.Close()
    
    host.Close();
    

このトピックの完全なコードの一覧を以下に示します。

Imports System
Imports System.Collections.Generic
Imports System.ServiceModel
Imports System.ServiceModel.Description
Imports System.ServiceModel.Web
Imports System.Text

<ServiceContract()> _
Public Interface IService

    <OperationContract()> _
    <WebGet()> _
    Function EchoWithGet(ByVal s As String) As String

    <OperationContract()> _
    <WebInvoke()> _
    Function EchoWithPost(ByVal s As String) As String
End Interface

Public Class Service
    Implements IService
    Public Function EchoWithGet(ByVal s As String) As String Implements IService.EchoWithGet
        Return "You said " + s
    End Function

    Public Function EchoWithPost(ByVal s As String) As String Implements IService.EchoWithPost
        Return "You said " + s
    End Function
End Class
Module Program

    Sub Main()
        Dim host As New ServiceHost(GetType(Service), New Uri("https://localhost:8000"))
        host.AddServiceEndpoint(GetType(IService), New BasicHttpBinding(), "Soap")
        Dim endpoint As ServiceEndpoint
        endpoint = host.AddServiceEndpoint(GetType(IService), New WebHttpBinding(), "Web")
        endpoint.Behaviors.Add(New WebHttpBehavior())

        Try
            host.Open()

            Using wcf As New WebChannelFactory(Of IService)(New Uri("https://localhost:8000/Web"))

                Dim channel As IService = wcf.CreateChannel()

                Dim s As String

                Console.WriteLine("Calling EchoWithGet by HTTP GET: ")
                s = channel.EchoWithGet("Hello, world")
                Console.WriteLine("   Output:  {0}", s)

                Console.WriteLine("")
                Console.WriteLine("This can also be accomplished by navigating to")
                Console.WriteLine("https://localhost:8000/Web/EchoWithGet?s=Hello, world!")
                Console.WriteLine("in a web browser while this sample is running.")

                Console.WriteLine("")

                Console.WriteLine("Calling EchoWithPost by HTTP POST: ")
                s = channel.EchoWithPost("Hello, world")
                Console.WriteLine("   Output:  {0}", s)
                Console.WriteLine("")
            End Using
            Using scf As New ChannelFactory(Of IService)(New BasicHttpBinding(), "https://localhost:8000/Soap")

                Dim channel As IService = scf.CreateChannel()

                Dim s As String

                Console.WriteLine("Calling EchoWithGet on SOAP endpoint: ")
                s = channel.EchoWithGet("Hello, world")
                Console.WriteLine("   Output:  {0}", s)

                Console.WriteLine("")

                Console.WriteLine("Calling EchoWithPost on SOAP endpoint: ")
                s = channel.EchoWithPost("Hello, world")
                Console.WriteLine("   Output:  {0}", s)
                Console.WriteLine("")
            End Using


            Console.WriteLine("Press <Enter> to terminate")
            Console.ReadLine()
            host.Close()
        Catch cex As CommunicationException
            Console.WriteLine("An exception occurred:  {0}", cex.Message)
            host.Abort()
        End Try

    End Sub
End Module
using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Web;
using System.Text;

namespace Microsoft.ServiceModel.Samples.BasicWebProgramming
{
    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        [WebGet]
        string EchoWithGet(string s);

        [OperationContract]
        [WebInvoke]
        string EchoWithPost(string s);
    }

    public class Service : IService
    {
        public string EchoWithGet(string s)
        {
            return "You said " + s;
        }

        public string EchoWithPost(string s)
        {
            return "You said " + s;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            ServiceHost host = new ServiceHost(typeof(Service), new Uri("https://localhost:8000"));
            host.AddServiceEndpoint(typeof(IService), new BasicHttpBinding(), "Soap");
            ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(IService), new WebHttpBinding(), "Web");
            endpoint.Behaviors.Add(new WebHttpBehavior());
            
            try
            {
                host.Open();

                using (WebChannelFactory<IService> wcf = new WebChannelFactory<IService>(new Uri("https://localhost:8000/Web")))
                {
                    IService channel = wcf.CreateChannel();

                    string s;

                    Console.WriteLine("Calling EchoWithGet by HTTP GET: ");
                    s = channel.EchoWithGet("Hello, world");
                    Console.WriteLine("   Output: {0}", s);

                    Console.WriteLine("");
                    Console.WriteLine("This can also be accomplished by navigating to");
                    Console.WriteLine("https://localhost:8000/Web/EchoWithGet?s=Hello, world!");
                    Console.WriteLine("in a web browser while this sample is running.");

                    Console.WriteLine("");

                    Console.WriteLine("Calling EchoWithPost by HTTP POST: ");
                    s = channel.EchoWithPost("Hello, world");
                    Console.WriteLine("   Output: {0}", s);
                    Console.WriteLine("");
                }
                using (ChannelFactory<IService> scf = new ChannelFactory<IService>(new BasicHttpBinding(), "https://localhost:8000/Soap"))
                {
                    IService channel = scf.CreateChannel();

                    string s;

                    Console.WriteLine("Calling EchoWithGet on SOAP endpoint: ");
                    s = channel.EchoWithGet("Hello, world");
                    Console.WriteLine("   Output: {0}", s);

                    Console.WriteLine("");

                    Console.WriteLine("Calling EchoWithPost on SOAP endpoint: ");
                    s = channel.EchoWithPost("Hello, world");
                    Console.WriteLine("   Output: {0}", s);
                    Console.WriteLine("");
                }


                Console.WriteLine("Press [Enter] to terminate");
                Console.ReadLine();
                host.Close();
            }
            catch (CommunicationException cex)
            {
                Console.WriteLine("An exception occurred: {0}", cex.Message);
                host.Abort();
            }
        }
    }
}

コードのコンパイル方法

Service.cs のコンパイル時には、System.ServiceModel.dll と System.ServiceModel.Web.dll を参照します。

参照

リファレンス

WebHttpBinding
WebGetAttribute
WebInvokeAttribute
WebServiceHost
ChannelFactory
WebHttpBehavior

その他のリソース

WCF Web HTTP プログラミング モデル