How to: Host a Service on Windows Azure that Accesses the Service Bus
Creating a service application that runs on Windows Azure follows the programming patterns for both a Windows Azure and a traditional Windows Azure Service Bus application: you define and implement the service contract, configure the endpoint, create the credentials, and then start the host. Once you are finished, you stop the host. However, note that there are two unique issues when you create a service in Windows Azure:
Windows Azure does not store Service Bus and Access Control configuration information in the Machine.config file
Because Windows Azure does not install the Windows Azure SDK, the Machine.config file on a Windows Azure computer has no information about Service Bus bindings or endpoints. As stated previously, Windows Azure security restrictions prevent you from modifying the Windows Azure Machine.config file. Therefore, there are two options to make Service Bus and Access Control configuration information available to your Service Bus applications.
The recommended solution is to use the Service Bus APIs to programmatically configure your application. For example, although you could store name and password information in the App.config file, you would programmatically set any relay binding configurations. For more information about setting configuration programmatically, see Configuring a WCF Service to Register with the Service Bus.
The second solution is to manually modify the App.config file for your application by adding all of the relevant information. Once you do this, you can use the App.config file to configure bindings and endpoints. To do so, you can see the Machine.config file on a computer that has the Windows Azure SDK installed, find all Windows Azure-related configuration information, and copy them to your application App.config file. While this lets you use the App.config file on the host service, it will be difficult to test your code: you may encounter duplication issues with the Machine.config file of the local test computer, which will already have the Windows Azure SDK installed. Therefore, we recommend that you use the previous option, and set everything programmatically.
- The recommended solution is to use the Service Bus APIs to programmatically configure your application. For example, although you could store name and password information in the App.config file, you would programmatically set any relay binding configurations. For more information about setting configuration programmatically, see Configuring a WCF Service to Register with the Service Bus.
The Service Bus Service Bus must have Full Trust authorization to run on Windows Azure
As with all other Service Bus applications, you must make sure that the operating system is running with Full Trust authorization. This can be set in the ServiceDefinition.csdef file of your Windows Azure project, using the following procedure.
To package the Service Bus assembly with your application
In Solution Explorer, under the WorkerRole or WebRole node (depending on where your code is located), add the Microsoft.ServiceBus.dll assembly to your Windows Azure project as a reference.
This step is the standard process for adding a reference to an assembly.
In the Reference folder, right-click Microsoft.ServiceBus. Then click Properties.
In the Properties dialog, set Copy Local to True.
Doing so makes sure that the Microsoft.ServiceBus.dll assembly is copied to the local \bin path and available to your application when it is running on Windows Azure.
To set the Windows Azure application to Full Trust
In the ServiceDefinition.csdef file in your project, set the
"true", as shown in the following code. Replace
"ApplicationNameHere"with the name of your application:
<?xml version="1.0" encoding="utf-8"?> <ServiceDefinition name="ApplicationNameHere" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition"> <WebRole name="WebRole" enableNativeCodeExecution="true"> <InputEndpoints> <!-- Must use port 80 for http and port 443 for https when running in the cloud --> <InputEndpoint name="HttpIn" protocol="http" port="80" /> </InputEndpoints> </WebRole> <WorkerRole name="WorkerRole" enableNativeCodeExecution="true"> </WorkerRole> </ServiceDefinition>