Instrukcje: Tworzenie podstawowej, opartej na protokole HTTP usługi sieci Web programu WCF

Program Windows Communication Foundation (WCF) umożliwia utworzenie usługi, która uwidacznia internetowy punkt końcowy. Internetowe punkty końcowe wysyłają dane według kodu XML lub JSON, nie ma koperty protokołu SOAP. W tym temacie pokazano, jak uwidocznić taki punkt końcowy.

Uwaga

Jedynym sposobem zabezpieczenia internetowego punktu końcowego jest uwidocznienie go za pośrednictwem protokołu HTTPS przy użyciu zabezpieczeń transportu. W przypadku korzystania z zabezpieczeń opartych na komunikatach informacje o zabezpieczeniach są zwykle umieszczane w nagłówkach protokołu SOAP, a komunikaty wysyłane do punktów końcowych innych niż SOAP nie zawierają koperty protokołu SOAP, nie ma miejsca do umieszczenia informacji zabezpieczających i musisz polegać na zabezpieczeniach transportu.

Aby utworzyć internetowy punkt końcowy

  1. Zdefiniuj kontrakt usługi przy użyciu interfejsu oznaczonego atrybutami ServiceContractAttributei WebInvokeAttributeWebGetAttribute .

    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        [WebGet]
        string EchoWithGet(string s);
    
        [OperationContract]
        [WebInvoke]
        string EchoWithPost(string s);
    }
    
    <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
    
    

    Uwaga

    Domyślnie WebInvokeAttribute mapuje wywołania POST na operację. Można jednak określić metodę HTTP (na przykład HEAD, PUT lub DELETE), aby zamapować na operację, określając parametr "method=". WebGetAttribute nie ma parametru "method=" i mapuje tylko wywołania GET do operacji usługi.

  2. Zaimplementuj kontrakt usługi.

    public class Service : IService
    {
        public string EchoWithGet(string s)
        {
            return "You said " + s;
        }
    
        public string EchoWithPost(string s)
        {
            return "You said " + s;
        }
    }
    
    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
    

Aby hostować usługę

  1. Utwórz WebServiceHost obiekt.

    WebServiceHost host = new WebServiceHost(typeof(Service), new Uri("http://localhost:8000/"));
    
    Dim host As WebServiceHost = New WebServiceHost(GetType(Service), New Uri("http://localhost:8000/"))
    
  2. Dodaj element ServiceEndpoint z elementem WebHttpBehavior.

    ServiceEndpoint ep = host.AddServiceEndpoint(typeof(IService), new WebHttpBinding(), "");
    
    Dim ep As ServiceEndpoint = host.AddServiceEndpoint(GetType(IService), New WebHttpBinding(), "")
    

    Uwaga

    Jeśli nie dodasz punktu końcowego, WebServiceHost automatycznie utworzy domyślny punkt końcowy. WebServiceHost Dodaje WebHttpBehavior również i wyłącza stronę Pomocy HTTP oraz funkcje GET (Web Services Description Language) i GET, dzięki czemu punkt końcowy metadanych nie zakłóca domyślnego punktu końcowego HTTP.

    Dodanie punktu końcowego innego niż SOAP z adresem URL "" powoduje nieoczekiwane zachowanie podczas próby wywołania operacji w punkcie końcowym. Przyczyną tego jest identyfikator URI nasłuchiwania punktu końcowego jest taki sam jak identyfikator URI strony pomocy (strona wyświetlana podczas przeglądania adresu podstawowego usługi WCF).

    Aby zapobiec temu, możesz wykonać jedną z następujących czynności:

    • Zawsze określ niepusty identyfikator URI dla punktu końcowego innego niż SOAP.
    • Wyłącz stronę pomocy. Można to zrobić za pomocą następującego kodu:
    ServiceDebugBehavior sdb = host.Description.Behaviors.Find<ServiceDebugBehavior>();
    sdb.HttpHelpPageEnabled = false;
    
    Dim sdb As ServiceDebugBehavior = host.Description.Behaviors.Find(Of ServiceDebugBehavior)()
    sdb.HttpHelpPageEnabled = False
    
  3. Otwórz hosta usługi i zaczekaj na naciśnięcie klawisza ENTER przez użytkownika.

    host.Open();
    Console.WriteLine("Service is running");
    Console.WriteLine("Press enter to quit...");
    Console.ReadLine();
    host.Close();
    
    host.Open()
    Console.WriteLine("Service is running")
    Console.WriteLine("Press enter to quit...")
    Console.ReadLine()
    host.Close()
    

    W tym przykładzie pokazano, jak hostować usługę w stylu sieci Web przy użyciu aplikacji konsolowej. Taką usługę można również hostować w usługach IIS. W tym celu określ klasę WebServiceHostFactory w pliku svc, jak pokazano w poniższym kodzie.

    <%ServiceHost
        language=c#
        Debug="true"
        Service="Microsoft.Samples.Service"
        Factory=System.ServiceModel.Activation.WebServiceHostFactory%>
    

Aby wywołać operacje usługi mapowane na metodę GET w przeglądarce

  1. Otwórz przeglądarkę internetową, wprowadź adres URL "http://localhost:8000/EchoWithGet?s=Hello, world!", a następnie naciśnij klawisz Enter. Adres URL zawiera podstawowy adres usługi (http://localhost:8000/), względny adres punktu końcowego (""), operację usługi do wywołania ("EchoWithGet") oraz znak zapytania, po którym następuje lista nazwanych parametrów rozdzielonych ampersand (&).

Aby wywołać operacje usługi w kodzie

  1. Utwórz wystąpienie ChannelFactory<TChannel> w using bloku.

    using (ChannelFactory<IService> cf = new ChannelFactory<IService>(new WebHttpBinding(), "http://localhost:8000"))
    
    Using cf As New ChannelFactory(Of IService)(New WebHttpBinding(), "http://localhost:8000")
    
  2. Dodaj WebHttpBehavior do punktu końcowego ChannelFactory<TChannel> wywołania.

    cf.Endpoint.Behaviors.Add(new WebHttpBehavior());
    
    cf.Endpoint.Behaviors.Add(New WebHttpBehavior())
    
  3. Utwórz kanał i wywołaj usługę.

    IService channel = cf.CreateChannel();
    
    string s;
    
    Console.WriteLine("Calling EchoWithGet via 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("http://localhost:8000/EchoWithGet?s=Hello, world!");
    Console.WriteLine("in a web browser while this sample is running.");
    
    Console.WriteLine("");
    
    Console.WriteLine("Calling EchoWithPost via HTTP POST: ");
    s = channel.EchoWithPost("Hello, world");
    Console.WriteLine("   Output: {0}", s);
    
    Dim channel As IService = cf.CreateChannel()
    
    Dim s As String
    
    Console.WriteLine("Calling EchoWithGet via 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("http://localhost:8000/EchoWithGet?s=Hello, world!")
    Console.WriteLine("in a web browser while this sample is running.")
    
    Console.WriteLine("")
    
    Console.WriteLine("Calling EchoWithPost via HTTP POST: ")
    s = channel.EchoWithPost("Hello, world")
    Console.WriteLine("   Output: {0}", s)
    
  4. Zamknij plik WebServiceHost.

    host.Close();
    
    host.Close()
    

Przykład

Poniżej znajduje się pełna lista kodu dla tego przykładu.

// Service.cs
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)
        {
            WebServiceHost host = new WebServiceHost(typeof(Service), new Uri("http://localhost:8000/"));
            try
            {
                ServiceEndpoint ep = host.AddServiceEndpoint(typeof(IService), new WebHttpBinding(), "");
                host.Open();
                using (ChannelFactory<IService> cf = new ChannelFactory<IService>(new WebHttpBinding(), "http://localhost:8000"))
                {
                    cf.Endpoint.Behaviors.Add(new WebHttpBehavior());

                    IService channel = cf.CreateChannel();

                    string s;

                    Console.WriteLine("Calling EchoWithGet via 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("http://localhost:8000/EchoWithGet?s=Hello, world!");
                    Console.WriteLine("in a web browser while this sample is running.");

                    Console.WriteLine("");

                    Console.WriteLine("Calling EchoWithPost via HTTP POST: ");
                    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
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 WebServiceHost = New WebServiceHost(GetType(Service), New Uri("http://localhost:8000/"))
        Try
            Dim ep As ServiceEndpoint = host.AddServiceEndpoint(GetType(IService), New WebHttpBinding(), "")
            host.Open()
            Using cf As New ChannelFactory(Of IService)(New WebHttpBinding(), "http://localhost:8000")

                cf.Endpoint.Behaviors.Add(New WebHttpBehavior())

                Dim channel As IService = cf.CreateChannel()

                Dim s As String

                Console.WriteLine("Calling EchoWithGet via 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("http://localhost:8000/EchoWithGet?s=Hello, world!")
                Console.WriteLine("in a web browser while this sample is running.")

                Console.WriteLine("")

                Console.WriteLine("Calling EchoWithPost via HTTP POST: ")
                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

Kompilowanie kodu

Podczas kompilowania Service.cs odwołań System.ServiceModel.dll i System.ServiceModel.Web.dll.

Zobacz też