共用方式為


HOW TO:將合約公開給 SOAP 和 Web 用戶端

根據預設,Windows Communication Foundation (WCF) 只會讓 SOAP 用戶端使用端點。 在 HOW TO:建立基本 WCF Web HTTP 服務中,端點可提供給非 SOAP 用戶端使用。 有時候您可能會想要讓兩者都有機會使用相同合約,也就是同時當做 Web 端點和 SOAP 端點。 本主題說明如何執行此操作的範例。

若要定義服務合約

  1. 透過加上 ServiceContractAttributeWebInvokeAttributeWebGetAttribute 屬性標示的介面來定義服務合約,如下列程式碼所示。

    <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(zh-tw,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 端點加入具有 BasicHttpBindingServiceEndpoint,如下列程式碼所示。

    host.AddServiceEndpoint(GetType(IService), New BasicHttpBinding(), "Soap")
    
    host.AddServiceEndpoint(typeof(IService), new BasicHttpBinding(), "Soap");
    
  3. 針對非 SOAP 端點加入具有 WebHttpBindingServiceEndpoint,並將 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. ServiceHost 執行個體上呼叫 Open() 以開啟服務主機,如下列程式碼所示。

    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. using 區塊內建立 WebChannelFactory 的執行個體,如下列程式碼所示。

    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(zh-tw,VS.100).gif注意:
using 區塊結尾,會在通道上自動呼叫 Close()

  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. using 區塊內建立 ChannelFactory 的執行個體,如下列程式碼所示。

    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 程式設計模型