-
Open Visual Studio 2010, create a new blank solution, and
name it AzureWCFSolution.
-
Right-click the solution and select Add | New
Project.
-
In the Cloud templates section, select the Windows Azure
Project template. Name the project CustomerInformation, and click OK.
-
Select the WCF Service Web Role template. Click the small
pencil icon to edit the default name, and name the new service
GetCustomersFromAzure.
-
Click OK. After Visual Studio completes the project
structure, right-click the project and select Add | Class. Name
the class file Customer.cs and
then click Add. You’ll use this class as an in-memory object to
create and store data. It exposes six properties that correspond
to a customer ID, title, first name, last name, email, and phone
number, as shown in the bold code here:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace GetCustomrsFromAzure
{
public class Customer
{
public string custID { get; set; }
public string custTitle { get; set; }
public string custFirstName { get; set; }
public string custLastName { get; set; }
public string custEmail { get; set; }
public string custPhone { get; set; }
}
}
-
Double-click the Service1.cs file and then rename
Service1 to GetCustomers and
IService1 to IGetCustomers. Then right-click the file
names and select Rename to ensure that the class and interface
file names are the same as the class and interface
namespaces.
-
In the GetCustomers service class,
edit the code as shown in the following bold code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace GetCustomersFromAzure
{
public class GetCustomers : IGetCustomers
{
List<Customer> myCustomerList = new List<Customer>();
List<Customer> returnListOfCustomers = new List<Customer>();
#region Customer Methods
public List<Customer> getACustomer(int custID)
{
generateCustomerData();
string strCustID = custID.ToString();
var returnListOfData = (from customer in myCustomerList
where customer.custID == strCustID
select customer).ToArray();
List<Customer> returnCustomer = returnListOfData.ToList();
return returnCustomer;
}
public List<Customer> getAllCustomers()
{
generateCustomerData();
var returnListOfData = (from customer in myCustomerList
select customer).ToArray();
returnListOfCustomers = returnListOfData.ToList();
return returnListOfCustomers;
}
private void generateCustomerData()
{
Customer cust1 = new Customer();
cust1.custTitle = "Dr.";
cust1.custID = "1";
cust1.custFirstName = "Ben";
cust1.custLastName = "Smith";
cust1.custEmail = "[email protected]";
cust1.custPhone = "425-555-0177";
myCustomerList.Add(cust1);
...
Customer cust10 = new Customer();
cust10.custTitle = "Dr.";
cust10.custID = "10";
cust10.custFirstName = "Dorena";
cust10.custLastName = "Paschke";
cust10.custEmail = "[email protected]";
cust10.custPhone = "425-555-0156";
myCustomerList.Add(cust10);
}
#endregion
#region Credit Score Method
public int GetPersonalCreditScore()
{
int creditScore = 0;
try
{
Random random = new Random();
creditScore = random.Next(500, 800);
return creditScore;
}
catch (Exception ex)
{
return 0;
}
}
#endregion
}
}
The preceding code is the core service code that uses the
Customers class to create an in-memory data
construct that is returned when the service is called. The
getACustomer and
getAllCustomers methods both return a list
collection, but GetACustomer returns a
single record from the list collection. Note that the
generateCustomerData method code has been
trimmed to reduce redundancy. The code creates 10 customers that
the service passes back to the calling application as a list
collection object—you could choose to create more or fewer
customer records, because the list is an in-memory
object.
Note
Alternatively, you could choose to use the Entity Data
Model framework to bind a database such as Microsoft SQL Azure to your service . Rather than using an in-memory object, you’d use a data
context and Microsoft Language-Integrated Query (LINQ) to
query and return the appropriate records by using a list collection or other data construct.
This code also returns a random integer value
(creditScore). In a real-world application,
you’d include some processing here that runs against a customer
record (using a Social Security number, for example) to get the
credit score, but for this example, the important point is to
show how the service is used.
-
You’re now finished with the core service code.
Double-click the IGetCustomers file to edit the service
contract. Amend the service contract code as shown in the
following bold code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.ServiceModel.Activation;
namespace GetCustomersFromAzure
{
[ServiceContract]
public interface IGetCustomers
{
[OperationContract]
List<Customer> getACustomer(int custID);
[OperationContract]
List<Customer> getAllCustomers();
[OperationContract]
int GetPersonalCreditScore();
}
}
-
You now want to ensure that your service is configured to
deploy to Windows Azure. To make sure this is possible, edit
the web.config file to match the following bold code. Note that
the commented-out code in the
<serviceBehaviors> section is the
default code created by Visual Studio, and the bold code shows
the amendments you need to make to handle the load-balancing
workaround for services deployed to Windows Azure:
<system.serviceModel>
<services>
<service behaviorConfiguration="GetCustomersFromAzure.Service1Behavior"
name="GetCustomersFromAzure.GetCustomers">
<endpoint address="" binding="basicHttpBinding"
contract="GetCustomersFromAzure.IGetCustomers">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract=
"IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<!--<behavior name="GetCustomersFromAzure.Service1Behavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>-->
<behavior name="GetCustomersFromAzure.Service1Behavior">
<serviceMetadata httpGetEnabled="true" />
<useRequestHeadersForMetadataAddress>
<defaultPorts>
<add scheme="http" port="81" />
<add scheme="https" port="444" />
</defaultPorts>
</useRequestHeadersForMetadataAddress>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
-
Press F6 to build the changes. After the application
compiles successfully, right-click the cloud project and select
Publish. This invokes the Deploy Windows Azure dialog box. Select Create
Service Package Only and click OK. This invokes Windows Explorer
with the necessary configuration and package files that need to
get deployed to Windows Azure. Copy the folder path to the
Clipboard.
-
Now navigate to your Windows Azure developer portal (https://windows.azure.com/Default.aspx) and
create a new service. To do this, click New Hosted Service, and
then complete the information in the New Hosted Service dialog box (enter a name for
the service, the URL prefix, region, and version, and then click
Browse twice to load your package and configuration files). When
you’ve finished, click OK. Windows Azure publishes your
WCF service.