Поделиться через


Как предоставить контракты SOAP- и веб-клиентам

По умолчанию Windows Communication Foundation (WCF) делает конечные точки доступными только для клиентов SOAP. В разделе Как создать простую веб-службу WCF HTTP доступ к конечной точке предоставляется клиентам, не использующим протокол SOAP. Иногда может потребоваться сделать один и тот же контракт доступным обоими способами: в качестве сетевой конечной точки и в качестве конечной точки SOAP. В данном разделе приводится пример того, как это сделать.

Определение контракта службы

  1. Определите контракт службы с использованием интерфейса, отмеченного атрибутами ServiceContractAttribute, WebInvokeAttribute и 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(ru-ru,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. Добавьте объект ServiceEndpoint с привязкой BasicHttpBinding для конечной точки SOAP, как показано в следующем коде.

    host.AddServiceEndpoint(GetType(IService), New BasicHttpBinding(), "Soap")
    
    host.AddServiceEndpoint(typeof(IService), new BasicHttpBinding(), "Soap");
    
  3. Добавьте объект ServiceEndpoint с привязкой WebHttpBinding для конечной точки, не использующей протокол SOAP, и добавьте в эту точку объект 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();
    

Вызов операций службы, сопоставленных с операцией GET, в Internet Explorer

  1. Откройте окно Internet Explorer, введите адрес https://localhost:8000/Web/EchoWithGet?s=Hello, world! и нажмите клавишу ВВОД. Этот URL-адрес содержит базовый адрес службы (https://localhost:8000/), относительный адрес конечной точки (""), вызываемую операцию службы (EchoWithGet), вопросительный знак и следующий за ним список именованных параметров, в качестве разделителя между которыми используется амперсанд (&).

Вызов операций службы в сетевой конечной точке в коде

  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(ru-ru,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

Другие ресурсы

Модель веб-программирования HTTP WCF