Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Discover the top 5 tips for understanding .NET Interop
Search :       Advanced Search »
Home » Windows Phone » Hosting WCF service in Windows Azure and consuming in Window 7 phone app

Hosting WCF service in Windows Azure and consuming in Window 7 phone app

This article will give step by step walkthrough of hosting a WCF service in windows azure and then consuming that in windows7 phone application.

Author Rank :
Page Views : 6365
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
DevExpress Free UI Controls
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


Objective 

This article will give step by step walkthrough of hosting a WCF service in windows azure and then consuming that in windows7 phone application. 

You can see video of this article here.

Expected output 

1.gif 

Step 1

Open Visual Studio 2010 as an administrator. Select File -> New -> Cloud Service. Click on Windows Azure cloud service template. Give name of the cloud service. After creating the cloud service select the type of role. There are many roles to choose from. For our purpose, we will choose ASP.Net Web Role.

2.gif

Now examine the solution explorer. There should be 2 projects. Called WebRole1 and CloudServices1. These names may different basis of the name you given at time of creation of cloud service project in step1. 

3.gif

Step 2: Creating the WCF Service  

Right click on WebRole1 project and add new item then select WCF service from the WEB project template. 

Modify contract and service implementation as per you. For my purpose , I am making it simple service as below. 

Contract 

IService1.svc

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WebRole1
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string  GetMyName();
    }
}

Service Implementation 

Service1.svc.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WebRole1
{
    public class Service1 : IService1
    {
        public string GetMyName()
        {
            return "Hello Windows7 Phone from Cloud ";
        }
    }
}

Change the Binding to basicHttpBinding. Make sure to do this.

Note: make sure you have set WebRole1 as  startup project.

Step 3 

Running on Local Development Fabric  (Press F5)

Run the application. This will run on the local development fabric. When we install azure SDK, local development fabric got installed. And when we run the cloud service local development fabric get started and host the application. This provides exactly the same environment as of azure in the cloud.  This local development fabric could be used to debug the application before hosting into the cloud. 

Running the application on local development fabric 

See the URL in address bar of browser, in my case this is 

http://localhost:9375/Service1.svc

4.gif

Step 4: Publishing to cloud (azure)

Right Click on Cloud Service 1 project and select Publish.

5.gif 

It will open the azure site and ask for the login. 

6.gif 

If first time, you are login to azure site for publishing browse to account section to redeem your token. 

7.gif 

Now either create a project for windows azure or select existing project.  I have already a project created with the name of DJProject . I am selecting that. 

8.gif 

Since, I have already hosted in the project before. So I am getting the upgrade option. First time, you won't get upgrade option.  First time on creation of Windows azure project it will ask you to give an URL for your project . Just give an URL, which you will use to access your application across. 
 
9.gif

Click on upgrade option . It will ask for the package file and config file. Browse to CloudService1\bin\publish folder.  CloudService1 is name of the application. It may be different in your case , depending on what name you have given in step1. 

 10.gif

11.gif 

After uploading package and configuration file , give a label name and click on deploy. 

12.gif 

It will take some time for deployment.  After   successfully deployment you will see a green sign against Web Role saying it is in ready state. You could directly click on Web site URL to test your application on cloud. 

13.gif 

Now when you open the URL , you will get the same page as output you got on the local development fabric.  And now you successfully hotsed in cloud. 
 
14.gif

Step 5

Consuming the service hosted in Windows Azure in Windows 7 phone application 
  1. Create a Windows 7 phone application 

    15.gif 

  2. Design the content grid as below; Put a button and a text block. 

    16.gif 

  3. While adding service reference first adds URL of the service hosted on local development fabric. Copy the URL from Step 3 and right click on Console application and select Add service reference. In service URL paste the URL from step 3. 

    17.gif

    18.gif 

    After clicking OK, you can see ServiceReference1 is being added to the project. 

  4. Call the service hosted in local development fabric like below on the click event of button 

    19.gif 

Output 

20.gif 

Open the ServiceReferenceClient.config and modify the address to address of service in azure. 

21.gif
 
Again run the application and you will get the same output. 

22.gif 

For your reference Full source code is given below, 

MainPage.xaml

<phoneNavigation:PhoneApplicationPage
    x:Class="WindowsPhoneApplication4.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phoneNavigation="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Navigation"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="800"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}">
    <Grid x:Name="LayoutRoot" Background="{StaticResource PhoneBackgroundBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions
       <!--TitleGrid is the name of the application and page title-->
        <Grid x:Name="TitleGrid" Grid.Row="0">
            <TextBlock Text="MY APPLICATION" x:Name="textBlockPageTitle" Style="{StaticResource PhoneTextPageTitle1Style}"/>
            <TextBlock Text="Winodws 7" x:Name="textBlockListTitle" Style="{StaticResource PhoneTextPageTitle2Style}"/>
        </Grid>
        <!--ContentGrid is empty. Place new content here-->
        <Grid x:Name="ContentGrid" Grid.Row="1">
            <Button x:Name="btnGetData"
                    Height="100"
                    Content="Get Data"
                    Margin="136,47,156,505" />
            <TextBlock x:Name="txtData"
                       Height="100"
                       Margin="20,276,21,276"
                       FontSize="26"
                       FontWeight="SemiBold" />
        </Grid>
    </Grid>
</phoneNavigation:PhoneApplicationPage>

MainPage.Xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using WindowsPhoneApplication4.ServiceReference1;

namespace WindowsPhoneApplication4
{
    public partial class MainPage : PhoneApplicationPage
    {
        public MainPage()
        {
            InitializeComponent();
            SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape;
            btnGetData.Click += new RoutedEventHandler(btnGetData_Click);
        }
        void btnGetData_Click(object sender, RoutedEventArgs e)
        {
            Service1Client proxy = new Service1Client();
            proxy.GetMyNameCompleted += new EventHandler<GetMyNameCompletedEventArgs>(proxy_GetMyNameCompleted);
            proxy.GetMyNameAsync();
        }
        void proxy_GetMyNameCompleted(object sender, GetMyNameCompletedEventArgs e)
        {
            txtData.Text = e.Result.ToString();
        }
    }
}

Conclusion 

I hope this article was useful. Thanks for reading. Happy coding. 

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Dhananjay Kumar
Dhananjay Kumar is a developer who blogs at http://debugmode.net/. He is Microsoft MVP ,Telerik MVP and Mindcracker MVP. You can follow him on twitter  @debug_mode
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
DevExpress Free UI Controls
Become a Sponsor
 Comments
Good work by Mahesh On October 28, 2010
Good work DK.
Reply | Email | Modify 
gr8 by mahesh On January 21, 2011
gr8 article
Reply | Email | Modify 
6 Months Free & No Setup Fees ASP.NET Hosting!
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.