A look on Database [SQL Azure] option of new Windows Azure portal

Windows Azure Development portal has been moved to new Silverlight based portal. It has all new look and many new features.

SQL Azure can be explored by selecting Database option from the left panel.

clip_image001

New SQL Azure portal is having very nice Silverlight based user interactive UI and many more operations can be performed through the UI. There is new database manager and it allows us to perform operations at table and row level.

Now we can perform many more operations through Database option of new Windows azure portal.

1. Create a database

2. Create/ delete a table

3. Create/edit/delete rows of table.

4. Create/edit stored procedure

5. Create/edit views

6. Create / execute queries etc. . . .

Let us have a walkthrough on various operations we can perform on Database option of new Windows Azure portal.

To create new Database, select Data base server and from Database section, select Create option.

clip_image003

You will get the below popup, select subscription and server from drop down

clip_image004

Give name of the Database, select Edition and maximum size of the database.

clip_image006

Once database got created, select Manage option from top to open Data Base Manager.

clip_image008

Accept given term and conditions and click Ok.

clip_image009

After Ok, you will get popup asking password to connect to database. Provide password and click on Connect .

clip_image010

After successful connection , you will get a cube providing all the information about the database you connected.

clip_image011

You can perform all the database operations from the top ribbon. You have option to create new query, new table, new view, and new stored procedure

clip_image012

If we want to create new table, click on New Table option from ribbon. We are creating a table with name Blogger. We are giving there columns ID, Name and Technology. From drop down we can select data type for the columns .

clip_image014

Then click on save button at top,

clip_image015

You can navigate between Data Base and Table tab. You can create and delete column from here also. Once you click on save button, you can see table listed at left panel.

clip_image016

At top you can see you have option to navigate either design or data view of table. Click on Data tab to navigate to Data view.

Click on Row to add a Row in the table.

clip_image017

After adding two rows, you can see table as below. You can add and delete rows from top button also.

clip_image019

Now to execute query, select Data Base tab at top and execute query as below,

clip_image021

When you click Execute button at top, you will get output as below,

clip_image023

In this way , you can perform almost all the basic operations from new SQL Azure Data Base Manager.

Windows Azure for Developers Task 10: Creating Storage Account in new Silverlight based Windows Azure Portal

1. Login to Windows Azure Development portal.

2. After login at left panel you can see number of storage and service account in your subscription.

clip_image002

3. Select subscription and storage account option to create new Storage Account. At top left, you will get New Storage Account option. To create new storage account click on new Storage Account.

clip_image004

4. Provide all the required information,

clip_image005

a. Choose subscription.

b. Give unique name URL. It must be combination of only lower letters and numbers.

c. Choose a region, and click on Create.

5. Now you can see storage account has been created.

clip_image006

6. Click on storage account and at right side panel you can see properties.

clip_image007

In properties window we can see URL for BLOB, Queue and Table access. We will have to use these URL to access table, queue or blob.

7. To view the Primary key and Secondary key click on View Button.

clip_image008

In this way , we can create a Storage Account in new Silverlight based Azure Portal.

Windows Azure for Developers Task 9: Hosting WCF Service Role in Windows Azure Hosted Service

In Previous Article We saw How to work with WCF Service Web Role

I am going to extend previous article to publish in hosted service.

Hosting WCF Service Role in hosted service is exactly the same as we host  ASP.Net Web Role.

1. Login to Azure Development portal.

2. Create a new hosted service. If you want you can upgrade any existing hosted service to host WCF Service role also.

Read here, Creating Hosted Service in Windows Azure

3. To upgrade the hosted service ; browse locally

clip_image002

4. Right click on Azure project and select Publish.

5. Now while upgrading hosted service, select browse locally option and navigate to folder containing the files.

6. Once publishing done, you can navigate to the URL. On navigation you may get below error in browser

clip_image004

Do not be panic and append Service1.svc with URL and you will get WCF Service Web Role up and running

clip_image006

7. We can consume service exactly in same way , we consume normal WCF Service.

Windows Azure for Developers Task 8: Working with WCF Service Web Role

WCF Service Role enables us to create WCF service and host in Windows Azure. In this article, we will create a WCF Service Role and host on local development fabric and consume in a console application. In second part of this article we will move WCF Service to Azure portal.

 

To start with,

1. Create a New Project

2. Navigate to cloud tab

3. Create Windows Azure Project
4. Select WCF Service Role from given options.

 

clip_image001

 

If you give a look in solution explorer, you will find in WCF Service Role project contains exactly the same structure and files as of when you create a normal WCF Service Application. It contains ,

 

1. IService1.cs (Service Contract )
2. Service1.svc.cs ( Service definition file )
3. Web.config ( configuration for EndPoints)

 

We can modify these files accordingly for our purpose in exactly the same way; we do in usual WCF Service Application.

 

Let us modify Service Contract as below,

 

IService1.svc


using System.ServiceModel;

namespace WCFServiceWebRole1
{
 [ServiceContract]
 public interface IService1
 {

[OperationContract]
 string GetData(int value);

 }
}

 

And service definition would be,

 

Service1.svc.cs

namespace WCFServiceWebRole1
{

 public class Service1 : IService1
 {
 public string GetData(int value)
 {
 return string.Format("You entered: {0}", value);
 }

 }
}

 

 

Leave default configuration in Web.Config

 

Make sure you have set Windows Azure project as Startup project and run the application. In browser you will get below error message.

 

clip_image003

 

Ignore this message and append service.svc with URL, so now URL would be http://127.0.0.1:81/service1.svc. Service.svc is name of the service definition.
After appending you will get usual WCF Service message in browser.

 

clip_image005

 

To test this WCF Service role in a console client,

 

1. Create a console application project
2. Add Service Reference by providing URL http://127.0.0.1:81/service1.svc

 

clip_image006

 

Now we will make a normal service call,

 

Program.cs

using System;
using ConsoleApplication14.ServiceReference1;

namespace ConsoleApplication14
{
 class Program
 {
 static void Main(string[] args)
 {
 Service1Client proxy = new Service1Client();
 var result = proxy.GetData(99);
 Console.WriteLine(result);
 Console.ReadKey(true);
 }
 }
}

 

 

Now when you run you may or may not get below exception.

 

clip_image007

 

To solve above exception, we have to edit App.Config file. We need to change

 

clip_image009

 

127.0.0.1 to localhost. Because it might be the case console application is not able to resolve 127.0.0.1 . we need to change to localhost.
clip_image011

 

Now on running we will get below output

 

clip_image013

 

One behavior need to be noticed here is that sometime you may get time out exception after changing 127.0.0.1 to localhost also. In my further articles, I will drill down this unwanted behavior.

Type casting with is and as operator in C#

I have been seeing many developers are casting between two types using is operator. As we know, we do have as operator also for type casting.

For purpose of this article, we are going to use below two classes,

clip_image002

Very first let us try to understand, how is operator works?

clip_image004

So , if we are running below code , we will get output as true because ofccouse p is a Player.

clip_image006

Now let us modify code a bit and check whether p is compatible to Math class or not? Since p is instance of class Player and Player class does not have Math class in hierarchy tree, so output we will get false.

clip_image008

If we compare an object against null, we will get always an output as false.

Normally we use is operator like below,

clip_image010

In above snippet, C# checks type compatibility twice and it costs the performance. So to simplify above code and improve performance C# gives us as operator to compare.

clip_image012

as operator find type compatibility, If an object is not compatible with given type then as operator returns null.

clip_image013

Binding XML File to Data Grid in Silverlight

It is a common scenario when we need to bind or display data from XML File to Silverlight Data Grid. In this article, I have tried to demonstrate this with simple steps. What all we are going to do is

1. Download content of XML file as string using WebClient class.

2. Parse XML file using LINQ to XML

3. Bind parsed result as item source of Data Grid.

Prepare Data Source

Put your XML File in ClientBin folder. You can put XML file at any server location but in that case you will have to provide absolute URI of the XML file while downloading or reading XML file.

I have created a sample Data.xml file

Data.xml


<?xml version="1.0" encoding="utf-8" ?>
<School>
 <Student RollNumber="1" Name="John Papa" />
 <Student RollNumber="2" Name="Scott Gui" />
 <Student RollNumber="3" Name="Jessy Liberty" />
 <Student RollNumber="4" Name="Tim Huer" />
 <Student RollNumber="5" Name="Victor G" />
 <Student RollNumber="6" Name="Mahesh Chand" />
 <Student RollNumber="7" Name="Pinal Dave" />
 <Student RollNumber="8" Name="Suprotim Agarwal" />
 <Student RollNumber="9" Name="Dhananjay Kumar" />
 <Student RollNumber="10" Name="Kunal Chawudhary" />
 <Student RollNumber="11" Name="Abhijit Jana" />
 <Student RollNumber="12" Name="Shiv Prasad Koirala" />
</School>

Design xaml page

I am going to create a simple page. This page is going to have a Button and a Data Grid. On click event of the Button, Data from XML file will get bind to Data Grid.

MainPage.xaml


<UserControl xmlns:sdk= http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk
 x:Class="SilverlightApplication5.MainPage"
 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
 xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
 xmlns:d=http://schemas.microsoft.com/expression/blend/2008
 xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006
 mc:Ignorable="d"
 d:DesignHeight="300" d:DesignWidth="400">
 <Grid x:Name="LayoutRoot" Background="White">
 <StackPanel Orientation="Vertical" Margin="50,50,50,50">
 <Button x:Name="btnDemo" Content="Click To get Data From  XML File" Height="62" Width="362" />
 <sdk:DataGrid x:Name="grdXmlData" Height="Auto" Width="Auto" AutoGenerateColumns="True" />
 </StackPanel>
 </Grid>
</UserControl>

Down Load Data from XML File

I am using WebClient class to download content of XML file as string. On button click event downloading content of XML file.

clip_image002

Do not forget to include namespace System.Net to work with WebClient class.

Parse XML Data and bind to Data Grid

We are going to use LINQ to XML to parse data. Include Namespace System.Xml.Linq . To parse data from xml file , we have written below function .

clip_image004

Explanation

1. Function is taking string as input parameter. Here we will pass e.Result from Downloadcompletedstring event.

2. Creating an instance of XDocument by parsing string

3. Reading each descendants or element on Xml file and assigning value of each attribute to properties of Entity class (Student).

We need to create an Entity class to map the data from XML File. I am going to create a class Student with properties exactly as the same of attributes of Student Element in XML file.

Student .cs


namespace SilverlightApplication5
{
 public class Student
 {

public string RollNumber { get; set; }
 public string Name { get; set; }

}
}

For reference full source code is as below,

MainPage.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Xml.Linq;

namespace SilverlightApplication5
{
 public partial class MainPage : UserControl
 {
 List<Student> lstStudents = null;
 public MainPage()
 {
 InitializeComponent();
 btnDemo.Click += new RoutedEventHandler(btnDemo_Click);
 }

private void btnDemo_Click(object sender, RoutedEventArgs e)
 {
 WebClient client = new WebClient();
 Uri uritoXML = new Uri("Data.xml", UriKind.Relative);
 client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
 client.DownloadStringAsync(uritoXML);

}

void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
 {

if (e.Error != null)
 {
 MessageBox.Show("There is Error Downloading Data from XML File ");
 }
 else
 {

ParseXMLFile(e.Result);
 }
 }

void ParseXMLFile(string  dataInXmlFile)
 {

lstStudents = new List<Student>();

XDocument xmlDoc = XDocument.Parse(dataInXmlFile);
 lstStudents  = (from r in xmlDoc.Descendants("Student")
 select new Student
 {
 Name = (string) r.Attribute("Name").Value,
 RollNumber =(string) r.Attribute("RollNumber").Value
 }).ToList();

grdXmlData.ItemsSource = lstStudents;
 }

}
}

On running we will get output as below,

clip_image006

Windows Azure for Developers Task 7: Creating Hosted Service in new Silverlight based Windows Azure Portal

Windows Azure Development portal is in new look now.  It is Silverlight based and all operation can be performed from one page. New portal is having Ribbon at top, left panel and right panel. From configuring guest Operating system to stopping or restarting service can be performed from one page using new portal. In this article we will walkthrough, how could we create new Hosted Service in new Azure Portal?

Follow the steps below,

1. Log in to Azure Portal

2. On top left click on New Storage Account

clip_image002

3. You will get below Popup Window to create new hosted service.

clip_image003

4. Very first choose the Subscription from drop down. I have two subscription listed there. I am choosing debugmode subscription.

clip_image004

5. Next name of the service need to be provided. You are free to provide any name for the hosted service. I am providing here name HosteTempService of the service. After provide URL of the service. I am providing URL as HosteTempService.cloudapp.net . This hosted service will be accessible at http://debugmode.cloudapp.net/

clip_image005

6. After providing Service name and URL , we need to choose the region or affinity group . So far Microsoft provided 9 regions. If you want you can create your own Affinity group here also.

clip_image006

To create Affinity group, select Create or Choose affinity group radio button .

clip_image007

From drop down select, create a new affinity group option. Give name of the Affinity group and select the desired location for affinity group.

clip_image008

7. Select the Deployment option. You can choose option not to deploy also. Later you can configure the deployment. Check the checkbox, if you want to start newly created service just after successful deployment.

clip_image009

8. Give name of the Deployment. Select configuration file and package file of the application to be hosted in this service and click on Ok button.

clip_image010

In later posts we will see how we could add a certificate in hosted service. After clicking on Ok button you will see newly created hosted service is listed.

Windows Azure for Developers Task 6: Hosting ASP.Net Application in New Silverlight based Azure Portal

Windows Azure Portal is now all new. It is made in Silverlight. Almost all information is on one page and almost all operation can be performed from same page. Properties Windows for Deployment, Role Instances, and Hosting Service provide extensive operation. Configuring OS to SWAP VIP can be performed from top ribbon controls. Left panel contains online How to guide and options to perform other operations.

I will cover in detail in later article about new look portal. Purpose of this article to walkthrough how we can host ASP.Net Application in new Azure Portal.

To Start with,

1. Open Visual Studio.

2. Create new project and select Windows Azure Project Template from Cloud tab.

3. Select ASP.Net Web Role and click on Right arrow key.

clip_image001

4. Just modify something to test in Default.aspx.

5. Right click on Cloud Project and select Publish. From two options given select Create Service package only.

clip_image002

On click of Ok you will get published success message and two files created.

1. Package file (.cspkg)

2. Configuration file (.csdef)

We need to provide full location of these files to host application in azure. So make a note of this path.

clip_image004

Application is up and running on local app fabric. Login to Azure Development portal. After successful login you will get option to move to new Azure portal. Choose that option. There is no need to fear while moving to new portal because any time you can switch back to old portal.

In left panel you will get Hosted Service listed. I have one Hosted Service created and that is listed as count next to Hosted service option. To create new service click on Hosted Service , Storage Account and CDN option.

clip_image005

You can create a new hosted service

clip_image007

I have already a Hosted Service created called FirstApplication. Here we are going to deploy Asp.Net Web Application in this hosted service.

Click on FirstApplication then click on New Staging Deployment at top.

clip_image009

Note here is debugmode is name of my subscription.

On click of New staging Deployment on top ribbon below window will popup.

clip_image010

We need to provide Deployment name and need to browse to package file and configuration file. We created and packaged ASP.Net Application on local system. So we will select Browse Locally option. If we have configuration fie and package file in some of storage like BLOB then we will choose Browse Storage option.

On Browsing Locally, give path to exact location where you have packaged and configuration file saved.

clip_image011

We provided Deployment name as Test1Demo. Once browsing to package and configuration file Click on Ok.

Once I click on Ok button, I got below warning.

clip_image012

When I clicked See more details, I come to know this warning is about only one instance of Web Role.

clip_image013

Ignore warning and click on Yes. If you want you can increase count of Web Role instance.

Once clicking ok you can see changing messages about various Deployment stage.

clip_image014

After some time you will get message Ready as below.

clip_image015

Now your application is hosted in Azure Staging server. Click on Deployment name [Here it is Test1Demo] . After clicking on that click on DN Server URL in Properties tab to open the Azure hosted ASP.Net Web Application in browser.

clip_image017

Sharing port between WCF Services in netTcpBinding

WCF allows us to share the port between Services in TCP connection. For HTTP connection is allowed on IIS using HTTP.SYS Service.

By default port sharing for TCP connections are disabled. So to use port sharing between services using netTcpBinding , we need to first enable this windows service on the server .

To enable service we have two options.

First option is through command prompt

1. Open command prompt as administrator

2. Run the command c:\Windows\System32> net start NetTcpPortSharing

clip_image002

Second option is

1. Click on Start Button

2. Select Run option

3. Type Services.msc in Run window

clip_image003

4. In all listed service select and right click Net.Tcp Port Sharing Service .Choose properties. If Startup type is disabled then change it to Automatic. After changing Start up type click on Start button to Start the service.

clip_image004

Net.Tcp Port sharing service is running. Next step we need to do is to configure WCF service to share the port.

clip_image006

In this way we can enable port sharing .

Reading files asynchronously using WebClient class in Silverlight

WebClient class is used in Silverlight to asynchronously download or read a file from a particular URI.

1. WebClient class is under System.Net namespace.

2. This can retrieve data from any format of service. This can retrieve data in JSON, POX, and REST etc. format.

3. WebClient makes the entire request asynchronously. So it does not block any other operation.

4. On basis of data type of response from URI, we can choose class to parse the response data. If response is JOSN, we can use DataContractJSONSerliazer to parse response data.

5. Any type of file including Media, Images, and XML etc. can be downloaded using WebClient class.

Let us read a XML file asynchronously using WebClient class.

1. Add XML file named Data.xml in the client bin folder. Right click on client Bin folder and add a new item then select XML file from Data tab. Give name of the file as Data.xml.

Data.xml

<?xml version="1.0" encoding="utf-8" ?>
<School>
 <Student>
 <RollNumber>1</RollNumber>
 <Name>John Papa</Name>
 </Student>
 <Student>
 <RollNumber>1</RollNumber>
 <Name>John Papa</Name>
 </Student>
 <Student>
 <RollNumber>2</RollNumber>
 <Name>Scott Gui</Name>
 </Student>
 <Student>
 <RollNumber>3</RollNumber>
 <Name>Jessy Liberty</Name>
 </Student>
 <Student>
 <RollNumber>4</RollNumber>
 <Name>Tim Huer</Name>
 </Student>
 <Student>
 <RollNumber>5</RollNumber>
 <Name>Victor Gud</Name>
 </Student>
 <Student>
 <RollNumber>6</RollNumber>
 <Name>Machesh Chand</Name>
 </Student>
 <Student>
 <RollNumber>7</RollNumber>
 <Name>Pinal Dave</Name>
 </Student>
 <Student>
 <RollNumber>8</RollNumber>
 <Name>Suprotim Agarwal</Name>
 </Student>
 <Student>
 <RollNumber>9</RollNumber>
 <Name>Dhananjay Kumar</Name>
 </Student>
 <Student>
 <RollNumber>10</RollNumber>
 <Name>Kunal Chawudhary</Name>
 </Student>
 <Student>
 <RollNumber>11</RollNumber>
 <Name>Abhijit Jana</Name>
 </Student>
 <Student>
 <RollNumber>12</RollNumber>
 <Name>Shiv Prasad Koirala</Name>
 </Student>
</School>

2. Once Data.xml file is in place inside client bin folder, let us design XAML page. We will put one Button and one TextBlock.

On click event of button, we will make asynchronous call to read Data.xml file. After successful reading, we will bind xml response to TextBlock.

MainPage.xaml



<Grid x:Name="LayoutRoot" Background="White">
 <StackPanel Orientation="Vertical" Margin="50,50,50,50">
 <Button x:Name="btnDemo" Content="Click To get Data From  XML using WebClient" Height="62" Width="362" />
 <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" Height="200" Width="300">
 <TextBlock x:Name="txtDataFromXml"  Width="Auto" Height="Auto" />
 </ScrollViewer>
 </StackPanel>
 </Grid>
</UserControl>

3. On click event of button, we will read Data.xml file asynchronously.

clip_image002

At time of creation of URL either we can provide absolute URI or relative. In our example Data.xml file is in client bin folder so we are providing relative address.

In OpenReadCompleted event handler, we will parse XML file and bind response to TextBlock.

clip_image004

For reference full code is given below,

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 System.IO;

namespace SilverlightApplication5
{
 public partial class MainPage : UserControl
 {
 public MainPage()
 {
 InitializeComponent();
 btnDemo.Click +=new RoutedEventHandler(btnDemo_Click);
 }

private void btnDemo_Click(object sender, RoutedEventArgs e)
 {
 WebClient client = new WebClient();
 Uri uritoXML = new Uri(&quot;Data.xml&quot;,UriKind.Relative);
 client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
 client.OpenReadAsync(uritoXML);

}

void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
 {

Stream s = e.Result;
 StreamReader strReader = new StreamReader(s);
 txtDataFromXml.Text = strReader.ReadToEnd();
 s.Close();
 }
 }
}

Output

clip_image005