Eksporter (0) Skriv ut
Vis alt
EN
Dette innholdet er ikke tilgjengelig på ditt språk, men her er den engelske versjonen.
2 av 3 vurderte dette som nyttig - Vurder dette emnet

How to: Create a REST-based Service that Accesses the Service Bus

Windows Azure currently supports two different styles of applications that qualify as REST-based services: a traditional Service Bus application that complies with the Web programming model, and an HTTP-compliant application that uses a message buffer.

  • Traditional application

    This style of application uses the basic Windows Communication Foundation (WCF) programming model: defining and creating a service contract, using a binding and security credentials to connect to the Service Bus, and so on. The main addition is that a REST-based Service Bus application uses a service contract to whose members the [OperationContract, WebGet] or [OperationContract, WebInvoke] attributes are applied. These behaviors define the interface as a REST interface, and allow the Service Bus to interact with other REST-style applications. And therefore, the applications contain additional code that enables them to build HTTP-style messages. Finally, all these applications use the WebHttpRelayBinding binding. For more information, see How to: Expose a REST-based Web Service Through the Service Bus. For an extended example of a REST-based service application, see the Service Bus REST Tutorial, which is in turn based on the WebHttp sample in the Windows Azure SDK.

  • Message Buffer applications

    The Message Buffer is an Service Bus feature that exposes a REST interface to a buffer location. Sender applications can use this message buffer to temporarily store messages and events, similar to any other buffer. Similarly, client applications can subscribe to the message buffer to receive stored messages or events. Because the interface is exposed through the Service Bus, it is available to any application that can connect to the Internet. Because the message buffer is a REST-style interface, you can connect to it using non-WCF style applications, such as JavaScript applications, Web browsers, and other non-Microsoft products. This includes writing services in JavaScript. However, because this technology is so different from a typical Service Bus application, it has its own section of the documentation. For more information about creating applications that interact with the message buffer, see Message Buffer Overview.

Because message buffers have a dedicated section in the documentation, this topic focuses mainly on the details of creating a basic Service Bus application that uses the REST standard. The process of hosting a REST-based Service Bus application is very similar to hosting a standard Service Bus application. The main differences are in the contract and configuration: the actual hosting process is basically the same.

To host a Service Bus service application that complies with the REST standard

  1. Create the service by using the standard pattern as defined in Building a Service for the Service Bus; that is, define and implement a service contract, configure and implement the service host, and so on.

    1. When applying the OperationContractAttribute attribute to the service contract, make sure that you apply the relevant attributes to identify the REST-based members. For more information, see How to: How to: Expose a REST-based Web Service Through the Service Bus.

      The following example code shows how to tag an interface member as a REST-style GET member.

      public interface IImageContract
      {
          [OperationContract, WebGet]
          Stream GetImage();
      }
      
    2. When implementing the contract, set the appropriate content type header for the outgoing Web responses, as defined by the needs of your application.

      public ImageService() 
      {
          this.bitmap = Image.FromFile(imageFileName);
      }
      
      public Stream GetImage()
      {
          MemoryStream stream = new MemoryStream();
          this.bitmap.Save(stream, ImageFormat.Jpeg);
      
          stream.Position = 0;
          WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
      
          return stream;
      }        
      
      
      
  2. Create the address for the service by using the CreateServiceUri method:

    string serviceNamespace = “myServiceNamespace”
    Uri address = ServiceBusEnvironment.CreateServiceUri("https", serviceNamespace, "Image");
    
    
  3. Create a new instance of WebServiceHost.

    WebServiceHost host = new WebServiceHost(typeof(ImageService), address);
    

    The WebServiceHost class is derived from the ServiceHost class, and complements the WCF Web programming model. It also makes it easier to host REST-based services. It is recommended that you use WebServiceHost instead of ServiceHost in your REST-based Service Bus application implementation. For more information, see WCF REST Programming Model in the WCF documentation.

  4. Specify the address, binding, and contracts (also known as the "ABCs") used by the service endpoint.

        <services>
          <!-- Application Service -->
          <service name="Microsoft.ServiceBus.Samples.ImageService"
                   behaviorConfiguration="default">
            <endpoint name="RelayEndpoint"
                      contract="Microsoft.ServiceBus.Samples.IImageContract"
                      binding="webHttpRelayBinding"
                      bindingConfiguration="default"
                      behaviorConfiguration="sharedSecretClientCredentials"
                      address="" />
        </service>
    

    Here, the ABC is linked to the endpoint in the App.config file. For more information about configuring an application, see Configuring a WCF Service to Register with the Service Bus.

    The only binding to use for a service endpoint in a REST-based Service Bus application is WebHttpRelayBinding.

  5. If necessary, disable client authentication.

    
        <bindings>
          <!-- Application Binding -->
          <webHttpRelayBinding>
            <binding name="default">
                 <security relayClientAuthenticationType="None" />
            </binding>
          </webHttpRelayBinding>
        </bindings>
    
    

    By default, the WebHttpRelayBinding binding requires client authentication. This step describes how to disable it in the <binding> element in the App.config file, so that the client does not have to present credentials (for example, when you use a browser). For more information about authenticating with the Service Bus, see Securing and Authenticating a Service Bus Connection.

  6. Define the security for your application:

    
        <behaviors>
          <endpointBehaviors>
            <behavior name="sharedSecretClientCredentials">
              <transportClientEndpointBehavior credentialType="SharedSecret">
                <clientCredentials>
                  <sharedSecret issuerName="ISSUER_NAME" issuerSecret="ISSUER_SECRET" />
                </clientCredentials>
              </transportClientEndpointBehavior>
    
        </behaviors>
    

    In this example, the security is defined in the App.config file. For more information about security, see Securing and Authenticating a Service Bus Connection.

  7. Open the Service with a call to WebServiceHost.Open:

    host.Open()
    
  8. When you are finished, close the host with WebServiceHost.Close.

    host.Close();
    

Vurderte du dette som nyttig?
(1500 tegn igjen)
Takk for tilbakemeldingen

Fellesskapsinnhold

Legg til
Vis:
© 2014 Microsoft. Med enerett.