Hade du nytta av den här sidan?
Din feedback om det här innehållet är viktig. Berätta vad du tycker.
Ytterligare feedback?
1500 tecken kvar
How to: Create a REST-based Service that Accesses Service Bus
Det här innehållet finns inte tillgängligt på ditt språk men här finns den engelska versionen,

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

Updated: August 26, 2015

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 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 Service Bus to interact with other REST-style applications. As such, 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 example of a REST-based service application, see the REST Tutorial for the AppFabric Service Bus, which is in turn based on the WebHttp Service Bus sample.

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.

          <!-- Application Service -->
          <service name="Microsoft.ServiceBus.Samples.ImageService"
            <endpoint name="RelayEndpoint"
                      address="" />

    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 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.

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

    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:

            <behavior name="sharedAccessSignatureClientCredentials">
                  <sharedAccessSignature keyName=”RootManageSharedAccessKey” key="**key**" />

    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:

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


© 2015 Microsoft