.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> Windows 7 >> Post New Resource Bookmark and Share   

 Subscribe to Articles

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

Posted By:Dhananjay Kumar       Posted Date: July 14, 2010    Points: 25    Category: Windows 7    URL: http://www.dotnetspark.com  

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

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

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 http://bit.ly/cVTHtF

Expected output



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.



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.



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



Step 4

Publishing to cloud (azure)

Right Click on Cloud Service 1 project and select Publish.



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



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



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.



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.
 



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.






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



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.



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.





Step 4

Consuming the service hosted in Windows Azure in Windows 7 phone application

1.  Create  a Windows 7 phone application




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



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.






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




 
Output



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



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



For your reference Full source code is given below,

MainPage.xaml



    
        
            
            
        

        
        
            
            
        

        
        
            


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(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.


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend