Exporteren (0) Afdrukken
Alles uitvouwen
EN
Deze inhoud is niet beschikbaar in uw taal, maar wel in het Engels.

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

Updated: September 3, 2014

Microsoft Azure Service Bus currently supports applications that qualify as REST-based services: a traditional Service Bus application that complies with the Web programming model. 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 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 Service Bussample.

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.

  1. Create the service by using the standard pattern as defined in Building a Service for 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 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="sharedAccessSignatureClientCredentials"
                      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="sharedAccessSignatureClientCredentials">
              <transportClientEndpointBehavior>
                <tokenProvider>
                  <sharedAccessSignature keyName=”RootManageSharedAccessKey” key="**key**" />
                </tokenProvider>
              </transportClientEndpointBehavior>
            </behavior>
          </endpointBehaviors>
        </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();
    

Weergeven:
© 2014 Microsoft