Design a site like this with WordPress.com
Get started

WCF Services, Shared Hosting and Windows Phone 7

Creating a Microsoft web service can be a straightforward thing, but add in the requirements of deploying a WCF service to a shared hosting provider (such as GoDaddy) and have it available to web applications and to a Windows Phone 7 device can add a few wrinkles.  Most examples I read only explained these concepts on a local system and did not go into the complications of deploying to a real hosting provider.  This post explains how to achieve those goals using Visual Studio 2010, .NET 4.0, WCF, and a Windows Phone 7 Silverlight project.

You’ll need to have Visual Studio 2010 and also have your shared hosting provider configured for .NET 4.0 and running IIS 7.0.  You’ll also have to download and install the Windows Phone 7 toolkit if you’ll be doing the phone portion also.  The hosting provider steps may take them a few hours to complete, so get that going first.  Once set up, go into the providers IIS admin console page and create a new directory and make sure to set the folder as “Application Root” and enable “Anonymous Access”.

 

The Windows Communication Foundation Steps

image

Below, you can see the new services folder in the IIS console:

image

 

Now, onto the coding.  When working with external systems, it’s best to first get things working locally then move one piece at a time up to the web and get that working.  In other words, only change one variable at a time….so that when you get the inevitable configuration error you can assume it’s a hosting issue rather than your own bug and simply Google for solutions.

The first step is to create a WCF service in Visual Studio.  This example service takes an integer and returns an object holding the number’s square, it’s square root, and a request timestamp field.  This slightly more complicated example shows how you can return complex objects via the web service.  Create a new Project using the WCF Service Application (C#) template.  Change the names if desired. My interface “IService1.cs” looks like this:

using System.ServiceModel;

namespace WcfService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetSquared(int value);

        [OperationContract]
        myClass.Numbers Process(int value);
    }
}

We’ll add the “myClass.Numbers” class next.  This holds the results of our service.  Add a new class item to the project, the example looks like this:

using System;

namespace myClass
{
    [Serializable]
    public class Numbers
    {
        DateTime requestTime { get; set; }
        public int squared { get; set; }
        public double root{get;set;}

        public Numbers()
        {
            squared = 0;
            root = 0;
        }

        public void ProcessNumber(int value)
        {
            requestTime = System.DateTime.Now;
            squared = value * value;
            root = Math.Sqrt(System.Convert.ToDouble( value));
        }
    }
}

 

The class implementation is simple- one field holds the request time, another the square, and a final one the square root of the supplied value.  Next, add a reference to the new class to your WCF project (right-click on references).

Implement the WCF service with the following code:

 

using myClass;

namespace WcfService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class myservice : IService1
    {

        public myClass.Numbers Process(int value)
        {
            myClass.Numbers item = new Numbers();
            item.ProcessNumber(value);
            return item;
        }
    }
}

The service only has one method “Process”- it takes the supplied value, creates a new “Numbers” object, processes the number, and returns the object to the caller.

Now to test your WCF service locally.  Set the WCF project as the startup project (right-click on the item in solution explorer), and also set the “myservice.svc” item as the startup page.  Press F5 (for a debug run) and VS 2010 should open a WCF test client for you:

image

Double click on “Process()”, type in a value in the request Value field, and click the “Invoke” button.  The response results should come back and you can see the request time, square root, and square values.  Great…it’s working so far!

You can also test the standard WSDL screen by right-clicking on the WCF service project and do a “View in Browser”, then in the directory listing choose the “myservice.svc” item.  You should get a page like this:

image

 

Before deploying to your provider, verify the following configuration setting (multipleSiteBindingsEnabled) in the Web.config…it allows your service to work in a shared hosting environment.  You might need additional settings depending on yours:

 

image

Now, let’s deploy this to the shared hosting provider (GoDaddy in this case).  I used FTP access to publish the project to the folder I previously set up as the application folder.  Right click on the WCF project and choose “Publish…”.  A dialog appears where you can enter your path and credentials:

 

image

Once finished, you can test by invoking the URL to the .svc file which will look like: http://techybit.com/programs1/services/myservice.svc (assuming your application folder configured above is services).  You should get the standard WSDL info page.  If an error occurs, check all details above and/or Google the error message.  If you get stuck, you might have to contact your service provider for details on how to configure WCF services for their environment, but these steps worked for me.

At this point, you could create a standard webforms application, add a service reference to the WCF service, and call the interface method.  I tried this, and no unusual steps were needed other than making sure the WCF binding is set to basicHttpBinding.  It’s more interesting to try this with a Windows Phone 7 system and so the web version is left as an extra exercise.

Click here to proceed to Part 2: The Windows Phone 7 Application Steps

 

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: