Windows Azure Platform AppFabric
服務 > 服務匯流排

服務匯流排是要讓 Windows Azure 可作為企業服務應用程式前端 (顯露服務與轉接訊息等) 的一個服務，它本身是由 WCF 所組成，同時由於服務匯流排是負責處理使用者呼叫的前端介面，基於服務匯流排的特性，AppFabric Service Bus 會將要求訊息轉送給服務應用程式，這個程序被稱為 Relay，因此 AppFabric SDK 中內含了一個 Microsoft.ServiceBus.dll 組件，它封裝了改寫 WCF 通訊協定的各項必要元件，像是 TCP 與 HTTP 均可以透過 Service Bus 進行轉傳，但並不是所有的 WCF 協定都可以被轉傳，像是 MSMQ 以及具名管道等都不在受支援之列。
AppFabric Service Bus 由於是利用 WCF 進行通訊，因此使用它的服務應用程式也需要以 WCF 來開發。
下列程式是一個 AppFabric Service Bus 服務應用程式的簡單範例：
using System;
using System.ServiceModel;
using System.ServiceModel.Description;
using Microsoft.ServiceBus;
using Microsoft.ServiceBus.Description;
using System.Text;

namespace Microsoft.ServiceBus.Samples
{

#region Service Contract

    [ServiceContract(Name = "IEchoContract", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/EchoV1")]

    public interface IEchoContract
    {
        [OperationContract]
        String Echo(string text);
    }

    public interface IEchoChannel : IEchoContract, IClientChannel { };

#endregion

#region Echo Service

    [ServiceBehavior(Name = "EchoService", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/EchoV1")]

    public class EchoService : IEchoContract
    {
        public string Echo(string text)
        {
            Console.WriteLine("Echoing: {0}", text);
            return text;
        }
    }

#endregion

#region Host Service

    class Program
    {
        static void Main(string[] args)
        {

            ServiceBusEnvironment.SystemConnectivity.Mode = ConnectivityMode.AutoDetect;

            
            // Hard-code the service namespace, issuer name, and issuer secret into the application.
            // Note: The credentials are hard-coded in this sample for simplicity purposes but doing so is not considered a secure programming practice.
            string serviceNamespace = "putServiceNamespaceHere";
            string issuerName= "putIssuerNameHere";
            string issuerSecret = "putIssuerSecretHere";

            // Create the service URI based on the solution name.
            Uri address = ServiceBusEnvironment.CreateServiceUri("sb", serviceNamespace, "EchoService");



           // create the credentials object for the endpoint
            TransportClientEndpointBehavior sharedSecretServiceBusCredential = new TransportClientEndpointBehavior();
            sharedSecretServiceBusCredential.CredentialType = TransportClientCredentialType.SharedSecret;
            sharedSecretServiceBusCredential.Credentials.SharedSecret.IssuerName = issuerName;
            sharedSecretServiceBusCredential.Credentials.SharedSecret.IssuerSecret = issuerSecret;

            // create the service host reading the configuration
            ServiceHost host = new ServiceHost(typeof(EchoService), address);

            // create the ServiceRegistrySettings behavior for the endpoint
            IEndpointBehavior serviceRegistrySettings = new ServiceRegistrySettings(DiscoveryType.Public);

            // add the Service Bus credentials to all endpoints specified in configuration
            foreach (ServiceEndpoint endpoint in host.Description.Endpoints)
            {
                endpoint.Behaviors.Add(serviceRegistrySettings);
                endpoint.Behaviors.Add(sharedSecretServiceBusCredential);
            }
            
            // open the service
            host.Open();

            Console.WriteLine("Service address: " + address);
            Console.WriteLine("Press [Enter] to exit");
            Console.ReadLine();

            // close the service
            host.Close();
        }
    }

    #endregion

}
AppFabric Service Bus 除了基本的服務通訊轉送之外，它還支援了服務登錄 (Service Registration)，服務搜尋 (Service Discovery) 以及訊息緩衝區 (Message Buffer，類似 queue 服務)  等功能。