-
Open Visual Studio 2010.
-
Click File | New Project, select Cloud, and then select
Windows Azure Cloud Service.
-
Provide a name for your service (such as AzureMortgageService), and click
OK.
-
When prompted with the New Cloud Service Project dialog
box, select WCF Service Web Role (as shown earlier in Figure 1) and click
the right arrow button to add a new service to the
project.
-
Before clicking OK, point to the new service and then
click the pencil icon to rename the service (for example,
AzureMortgage).
-
Click OK.
-
As an optional step, you can rename your service from the
default Service1 to another name (such as
AzureService). This is a best
practice, especially when you’re deploying multiple services to
Microsoft SharePoint. First, it’s much easier to work with
multiple services if their titles are intuitive, and second, if
you amend the web.config file in SharePoint, it’ll make it much
easier to distinguish the client endpoint references. To change
the service name, click the Service1 or
IService1 references in your code, click
the Refactor menu option, and then select Rename. Provide a new
name for your service contract and code references. Visual
Studio updates your project.
-
After the project has been created, open the Service
contract file (for example, IAzureService.cs) and amend the
existing service contract as shown in the bolded code in the
following code listing:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace AzureMortgage
{
[ServiceContract]
public interface IAzureService
{
[OperationContract]
double[] getDailyInterestRate();
}
}
-
Now open the core service code file (for example,
AzureService.svc.cs) and amend the service code with the bolded
code as shown here:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace AzureMortgage
{
public class AzureService : IAzureService
{
public double[] getDailyInterestRate()
{
double dblDailyInterestRate = 0.00;
dblDailyInterestRate = getTheDailyInterestRate();
double[] returnProductCosts = new double[6];
returnProductCosts[0] = dblDailyInterestRate + .24;
returnProductCosts[1] = dblDailyInterestRate + .20;
returnProductCosts[2] = dblDailyInterestRate + .19;
returnProductCosts[3] = dblDailyInterestRate + .17;
returnProductCosts[4] = dblDailyInterestRate + .16;
returnProductCosts[5] = dblDailyInterestRate + .13;
return returnProductCosts;
}
private double getTheDailyInterestRate()
{
return .13;
}
}
}
The service code returns a one-dimensional array made up
of six variables of type double. Each of these doubles
essentially represents an interest rate for a specific mortgage
product that is based on a daily interest rate (which would of
course fluctuate over time but in this example is hard-coded for
illustrative purposes). For example, each of the elements within
the array is initialized to be a specific rate based on the
daily interest rate (dblDailyInterestRate)
as shown in the following code snippet:
returnProductCosts[0] = dblDailyInterestRate + .24;
returnProductCosts[1] = dblDailyInterestRate + .20;
returnProductCosts[2] = dblDailyInterestRate + .19;
returnProductCosts[3] = dblDailyInterestRate + .17;
returnProductCosts[4] = dblDailyInterestRate + .16;
returnProductCosts[5] = dblDailyInterestRate + .13;
The elements in the array map to specific mortgage
products; for example:
-
returnProductCosts[0] maps to a
30-year fixed mortgage product.
-
returnProductCosts[1] maps to a
15-year fixed mortgage product.
-
returnProductCosts[2] maps to a
3-year adjustable-rate mortgage (ARM) product.
-
returnProductCosts[3] maps to a
5-year ARM product.
-
returnProductCosts[4] maps to a
7-year ARM product.
-
returnProductCosts[5] maps to a
10-year ARM product.
When you call the service later on, they way you use these
different interest rates will become more apparent.
At this point, you’ve created the code portion of your
service. However, you’ll need to first amend the web.config file for service deployment, and then
add a client access policy file to the service (so you can
consume the service from a Microsoft Silverlight
application).
-
To amend the web.config file, double-click the file and
then amend it as shown in the bolded code here—some of which has
been removed for brevity:
...
<system.serviceModel>
<services>...
</services>
<behaviors>
<serviceBehaviors>
<behavior name="AzureMortgage.AzureService">
<serviceMetadata httpGetEnabled="true" />
<useRequestHeadersForMetadataAddress>
<defaultPorts>
<add scheme="http" port="81" />
<add scheme="https" port="444" />
</defaultPorts>
</useRequestHeadersForMetadataAddress>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
...
-
You now need to add a client access policy file to
complete the code portion of your service. To do this,
right-click the project and select Add | New item.
-
Select Data and then XML, call the file
clientaccesspolicy.xml, and click
Add.
-
Copy and paste the following code into the file and save
the file.
<?xml version="1.0" encoding="utf-8"?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="*">
<domain uri="*"/>
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
At this point, you’ve built the service and amended or
added the necessary configuration files to support deploying the
service to Windows Azure and to support cross-domain calls
from a Silverlight application. What’s left now is to publish
the service to Windows Azure.
-
Press F6 to build the project, and make sure there are no
build errors. Then right-click the cloud project and select
Publish. This invokes the Deploy Windows Azure Project dialog box (see the
following graphic), in which you can choose to create a service
package or deploy your cloud service to Windows Azure (which
requires some preconfiguration). Select Create Service Package
Only.
-
Click OK. When Windows Explorer opens with the built and
published files displayed, copy the folder path to the
Clipboard.
-
Open your Internet browser and navigate to your Windows Azure developer portal.
-
Click Hosted Services in the main portal, and then select New
Hosted Service.
-
You’ll need to complete several fields for your service,
as shown in the following graphic. For example, you’ll need to
enter a name for your service, a unique URL to access your
service, and a deployment name for your service. Also, select a
region for your deployment (for example, Anywhere US) and a
Deployment Option (for example, Deploy To Production
Environment).
-
When you’ve completed these fields, you’ll then need to
browse for the service package and configuration file for your
service. Click Browse Locally, and then paste the folder path
you copied earlier in the exercise. Add both the service package
and the service configuration file.
-
Click OK.
Windows Azure will now deploy your WCF service for use with the hosted service you
created in your Windows Azure account. This might take a few
minutes to complete.
-
After the service web role has been created in the
developer portal, the Status field will display the status as
Ready.
-
Click the new service, and then click the DNS name in the
Properties pane.
-
Although your Internet browser will open, you will see an
error page. This is because you need to append the URL with your
service name to view the service’s Web Services Description Language (WSDL)
definition page (for example,
http://contosomortgage.cloudapp.net/AzureService.svc).
Your service is now ready to be consumed from other
applications.
If you’ve been following along, you have now completed the
creation and deployment of a WCF service web role to Windows Azure. Congratulations! Although this
service wasn’t extremely difficult code, the more important
result here was that you now understand the process of how to
create and publish the service to Windows Azure. You can tweak
this service by editing the existing method, adding web methods,
adding data constructs or connected entities, and more. And all
you need to do each time is republish to Windows Azure to update
the service as it exists in the cloud.
Now that you’ve got the hang of WCF service web role
deployment, let’s move on to building SharePoint applications
that consume the WCF service.