共用方式為


建立 WS-I Basic Profile 1.1 互通服務

將 WCF 服務端點設成與 ASP.NET Web 服務用戶端互通:

您可選擇啟用 HTTPS 支援及對繫結的傳輸層級用戶端驗證。

BasicHttpBinding 類別的下列功能需要 WS-I Basic Profile 1.1 以外的功能:

若要讓 WCF 服務的中繼資料可供 ASP.NET 使用,請使用 Web 服務用戶端產生工具:Web 服務描述語言工具 (Wsdl.exe)Web 服務探索工具 (Disco.exe),以及 Visual Studio 中的新增 Web 參考功能。 請啟用中繼資料發行。 如需詳細資訊,請參閱發行中繼資料端點

範例

描述

下列範例程式碼示範如何在程式碼中,或是設定檔中新增與 ASP.NET Web 服務用戶端相容的 WCF 端點。

程式碼

using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;

[ServiceContract]
public interface IEcho
{
    [OperationContract]
    string Echo(string s);
}

public class MyService : IEcho
{
    public string Echo(string s)
    {
        return s;
    }
}

class Program
{
    static void Main(string[] args)
    {
        string baseAddress = "http://localhost:8080/wcfselfhost/";
        ServiceHost host = new ServiceHost(typeof(MyService), new Uri(baseAddress));

        // Create a BasicHttpBinding instance
        BasicHttpBinding binding = new BasicHttpBinding();

        // Add a service endpoint using the created binding
        host.AddServiceEndpoint(typeof(IEcho), binding, "echo1");

        host.Open();
        Console.WriteLine("Service listening on {0} . . .", baseAddress);
        Console.ReadLine();
        host.Close();
    }
}

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

<ServiceContract()> _
Public Interface IEcho

    <OperationContract()> _
    Function Echo(ByVal s As String) As String

End Interface

Public Class MyService
    Implements IEcho

    Public Function Echo(ByVal s As String) As String Implements IEcho.Echo
        Return s
    End Function

End Class

Friend Class Program

    Shared Sub Main(ByVal args() As String)
        Dim baseAddress = "http://localhost:8080/wcfselfhost/"
        Dim host As New ServiceHost(GetType(MyService), _
                                    New Uri(baseAddress))

        ' Add a service endpoint using the created binding
        With host
            .AddServiceEndpoint(GetType(IEcho), _
                                New BasicHttpBinding(), _
                                "echo1")
            .Open()
            Console.WriteLine("Service listening on {0} . . .", _
                              baseAddress)
            Console.ReadLine()
            .Close()
        End With
    End Sub
End Class
<configuration>
  <system.serviceModel>
    <services>
      <service name="MyService" behaviorConfiguration="HttpGetMetadata">
        <endpoint address="echo2" contract="IEcho" binding="basicHttpBinding" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="HttpGetMetadata">
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

另請參閱