Create a WCF Service to Call the On-Premises Service
To create the WCF service, you’ll use the Cloud template in
Visual Studio 2010.
-
Open the Visual Studio 2010 solution file you used in the
previous exercise, right-click the solution, and select Add |
New Project.
-
Select the Cloud project template, provide a name for the
service (such as SharePointCallingSvc), and then click
OK.
-
In the New Windows Azure Project dialog box, add the WCF
Service Web Role to the Windows Azure Solution pane. You can opt
to rename the WCF service by clicking the small edit
icon.
-
After adding the new project to the solution, rename the
service and interface contracts from their default
Service1 and IService1
to something more descriptive (such as SharePointCallingService and ISharePointCallingService).
-
Right-click the project and select Add Reference. Click
the Projects tab, and select Resources. You can also right-click
the newly added project and select Project Dependencies to
select Resources.
-
Right-click the project and select Add Reference. Add a
reference to the Microsoft.ServiceBus.dll to the project.
Right-click the library after you add it to the project, and
select Properties. Select True in the Copy To Local drop-down
list.
-
Right-click the project and select Add | New Item. In the
Data category, select XML, and then name the file
clientaccesspolicy.xml. You need to add
this file when calling this service from Silverlight to enable cross-domain
calls. Edit the clientaccesspolicy.xml file to match the XML
code here:
<?xml version="1.0" encoding="utf-8"?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="SOAPAction">
<domain uri="*"/>
</allow-from>
<grant-to>
<resource path="/"
include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
-
Double-click the service interface file (for example,
ISharePointCallingService.cs) and update the code in the
interface file as shown in bold text here. This is the service
contract code:
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
namespace SharePointCallingSvc
{
[ServiceContract]
public interface ISharePointCallingService
{
[OperationContract]
List<Resources.Sales> GetSales();
}
public interface ICloudToSharePointChannel : ISharePointCallingService,
IClientChannel { }
}
-
Double-click the main service file (for example,
SharePointCallingService.cs) and edit the code to match the bold
code here. This code is similar to the service you created
earlier but is now implemented within the WCF Web Role template
in a Windows Azure project:
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using Resources;
using System.Collections.Generic;
using Microsoft.ServiceBus;
namespace SharePointCallingSvc
{
[AspNetCompatibilityRequirements(RequirementsMode =
AspNetCompatibilityRequirementsMode.Allowed)]
public class SharePointCallingService : ISharePointCallingService
{
string svcNmspc = "servicebusexample";
string svcBusName = "owner";
string svcBusSecret = "<your secret here>";
string svcName = "SPListenerService";
public List<Sales> GetSales()
{
Uri svcURI = Microsoft.ServiceBus.ServiceBusEnvironment.
CreateServiceUri("sb", svcNmspc, svcName);
TransportClientEndpointBehavior svcBusCreds = new
TransportClientEndpointBehavior();
svcBusCreds.CredentialType = TransportClientCredentialType.SharedSecret;
svcBusCreds.Credentials.SharedSecret.IssuerName = svcBusName;
svcBusCreds.Credentials.SharedSecret.IssuerSecret = svcBusSecret;
NetTcpRelayBinding binding = new NetTcpRelayBinding();
ChannelFactory<ISalesDataChannel> channelFactory = new
ChannelFactory<ISalesDataChannel>(binding,
new EndpointAddress(svcURI));
channelFactory.Endpoint.Behaviors.Add(svcBusCreds);
ISalesDataChannel channel = channelFactory.CreateChannel();
channel.Open();
List<Sales> listFromOnPrem = channel.GetSales();
channel.Close();
return listFromOnPrem;
}
}
}
-
Finally, you’ll need to make sure that your
DiagnosticsConnectionString is not
configured to use your local development storage; you must edit
these settings to deploy your service properly, as shown in the following
code:
<?xml version="1.0"?>
<ServiceConfiguration serviceName="CloudService1" xmlns="http://schemas.microsoft.com/
ServiceHosting/2008/10/ServiceConfiguration">
<Role name="SharePointCallingSvc">
<Instances count="2" />
<ConfigurationSettings>
<Setting name="DiagnosticsConnectionString"
value="DefaultEndpointsProtocol=https;
AccountName=<your cloud storage settings here e.g. servicebusexample> />
</ConfigurationSettings>
</Role>
</ServiceConfiguration>
At this point, your Solution Explorer should look something
like Figure 7.
That is, you should have a Resources class library for your shared resources; a console application that is your
local, on-premises service; and a WCF service (a WCF web role) that
you’ll be deploying to Windows Azure.
You can now build and test your WCF service. An easy first test would be to deploy it to
your local Internet Information Services (IIS) and test how the two
services communicate with one another by using a simple Windows
Forms test application. This is not necessary, but I’ve found that
it saves time when debugging service code. You don’t need to change
your code to do this. To deploy to your local IIS instance, create a
folder on your local system (virtual directory), create a new IIS
website, and then right-click the service project (for example,
SharePointCallingSvc) and select Publish.
Note
Be sure not to select the Cloud project when publishing;
you’ll do that when deploying to Windows Azure.
When you’re ready to publish your WCF service to Windows
Azure, right-click the cloud project and select Publish. This will
build and package your WCF service into a configuration file and a cloud package file.
You’ve done this before, but the following steps provide a quick
recap:
-
Copy the directory path when Visual Studio opens Windows
Explorer with the location of your two Cloud files.
-
Navigate to your Windows Azure developer portal.
-
Click Hosted Services, Storage Accounts & CDN. Click
New Hosted Service in the New group on the ribbon.
-
Provide a name for your service, a URL prefix, and the
region to which you want to deploy the service. Select the
Deploy To Production Environment check box, provide a deployment
name, and then click Browse to navigate to the configuration
file and cloud package file.
The WCF service should take a few minutes to upload and deploy
correctly, and then you should be able to click the name of the
service and in the DNS name find the main service namespace. Add the
name of the service to your DNS name. You should now see something
similar to Figure 8—the service
definition page. Copy the service URI—you’ll use this in the next
exercise.
At this point, you have an on-premises service that, when
called, executes some code against SharePoint (specifically querying the SharePoint Sales list), and you have deployed a WCF
service to Windows Azure that you can call from any application that
is WCF conversant. When called, the WCF service initiates a
connection to the service bus and then communicates with the on-premises
service, which enables you to interact remotely with
SharePoint.