Publish Azure Project using Windows Azure SDK 1.5 Publish Setting wizard

In this post, I will show you step by step walkthrough to publish a cloud project in Windows Azure

Right click on the cloud project and select Publish

clip_image001

Windows Azure publish setting wizard will be open.

clip_image002

First you need to set the credentials. I assume here that you have not set any credential before so choose Add from the drop down

clip_image003

You will get Windows Azure Project Management authentication wizard. First task you need to do is either selected or create certificate for authentication.

Read here to create X.509 certificate

Read here to upload Management certificate

In drop down you will get all created certificate to choose. Select the desired certificate. Select certificate and copy your subscription Id. Give some name to this authentication setting such that you may use it later.

clip_image004

After giving all the required values, it will verify the service management authentication setting from Azure portal.

clip_image005

Once setting is verified, next you will have to choose the hosted service environment to publish the project. I am choosing PivotTestDebugMode-staging environment to publish.

clip_image006

After choosing hosted service environment, you will have to choose storage account from the drop down.

clip_image007

Now choose service configuration would be from Cloud or Local in dropdown.

clip_image008

Now choose build configuration in dropdown.

clip_image009

In last step you need to provide label for publish You can choose either to append date time with label or not.

clip_image010

If you want you can set the debug settings as well. In later posts we will see more on Debug settings J

clip_image011

And after providing the entire required information click on Publish button. You will see Role Modules are being configured.

clip_image012

In bottom you can see the log and status of the deployment.

clip_image014

I hope this post is useful. Thanks for reading Smile

If you find my posts useful you may like to follow me on twitter http://twitter.com/debug_mode or may like Facebook page of my blog http://www.facebook.com/DebugMode.Net If you want to see post on a particular topic please do write on FB page or tweet me about that, I would love to help you.

Code to Search in Windows Phone 7

In this post I will show you how you could search a particular item on windows phone 7. You can achieve search using launchers. SearchTask class is used to search on Windows Phone 7.

Expected Output

User will type the word to search and click on Search on Phone button. Then application will search on device and web both and give the search result.

image

 

To search an item you will have to use launcher SearchTask. This class is defines as below,

image

You can see in the definition of class that you can set the SearchQuery. SearchQuery is defined as string property in the class.

To work with SearchTask launcher first make instance of this class as below,

clip_image001

Then set the search criteria

clip_image002

In above code we are reading the search criteria from a text box.

For your reference full code to search is as below,


using System.Windows;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
namespace abc
{
public partial class MainPage : PhoneApplicationPage
{

public MainPage()
{
InitializeComponent();

}
private void btnSearch_Click(object sender, RoutedEventArgs e)
{
SearchTask searchonPhoneTask = new SearchTask();
searchonPhoneTask.SearchQuery = txtSearchItem.Text;
searchonPhoneTask.Show();

}
}
}

Design

Design of the page is simple. Application is having one textbox and one button. On click event of the button, it would search the text entered in the textbox.


<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>

<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="search phone" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="search on phone" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle2Style}"/>
</StackPanel>

<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<Button Grid.Row="1" x:Name="btnSearch" Content="Search on Phone" Height="77" Click="btnSearch_Click" Width="auto" Margin="0,23,0,0" />
<TextBox x:Name="txtSearchItem" Width="auto" Height="77" Margin="0,0,0,83" Grid.RowSpan="2" />
</Grid>
</Grid>

This is all you require to search on web and device from your windows phone application. I hope this post is useful. Thanks for reading Smile

 

If you find my posts useful you may like to follow me on twitter http://twitter.com/debug_mode or may like Facebook page of my blog http://www.facebook.com/DebugMode.Net If you want to see post on a particular topic please do write on FB page or tweet me about that, I would love to help you.

Code to select photo on Windows Phone 7

In this post we will see how could we select a photo saved on device and bind it as source of image control on our application.

Expected Output

image

 

image

To work with photo on device you need to work with Choosers. To work with Choosers you need to add Microsoft.Phone.Tasks namespaces.

clip_image001

PhotoChooserTask class allows us to choose a photo on the device. To choose a photo you need to make an object of this class. PhotoChooserTask class is defined as below,

image

You can see in definition of class that either you can choose photo from camera by setting ShowCamera value to true . In this post we will choose save photo from the device. You have option to set pixel height and width value of selected photo as well.

So very first, you need to create instance of PhtoChooserTask ,

image

And then set the height and width of the selected photo. By setting these values user will able to select only 400×400 pixels of the photo.

image

Next show the chooser to user to select the photo

image

PhtoChooserTask returns selected photo as byte array. In completed event, you need to decode the byte array as writeable bitmap and then set as the source of Image control. For that you need to add below namespaces

image

And decode the byte array to writable bitmap as below,

clip_image002

And set the writeablebitmap to the source of image control ,

clip_image003

For your reference full source code is as below,


using System;
using System.Windows;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using System.Windows.Media.Imaging;
using Microsoft.Phone;

namespace PhoneApp6
{
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
}

private void btnSelectPhoto_Click(object sender, RoutedEventArgs e)
{
PhotoChoos erTask taskToChoosePhoto = new PhotoChooserTask();
taskToChoosePhoto.PixelHeight = 400;
taskToChoosePhoto.PixelWidth = 400;
taskToChoosePhoto.Show();
taskToChoosePhoto.Completed += new EventHandler<PhotoResult>(taskToChoosePhoto_Completed);
}

void taskToChoosePhoto_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
string fileName = e.OriginalFileName;
WriteableBitmap selectedPhoto = PictureDecoder.DecodeJpeg(e.ChosenPhoto);
imgSelected.Source = selectedPhoto;

}
}
}
}

&nbsp;

Design

I have just put one button and image control. On click event of the button user will select photo on device and after clicking on Ok button, selected photo will be set as source of the image control.


<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>

<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="select photo" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="Select Photo" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>

<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Grid.RowDefinitions>
<RowDefinition Height="100" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<Button x:Name="btnSelectPhoto" Content="Select Photo on Device" Height="100" Width="auto" Click="btnSelectPhoto_Click" />
<Image x:Name="imgSelected" Height="auto" Width="auto" Grid.Row="1" />
</Grid>
</Grid>

&nbsp;

In this way you can select a photo on Windows Phone 7. I hope this post is useful. Thanks for reading Smile

If you find my posts useful you may like to follow me on twitter http://twitter.com/debug_mode or may like Facebook page of my blog http://www.facebook.com/DebugMode.Net If you want to see post on a particular topic please do write on FB page or tweet me about that, I would love to help you.

Debugging WCF Service from Silverlight: The Breakpoint will not currently be hit at WCF Service error

Assumption

  1. Binding configuration at WCF has debugging enabled
  2. Cross domain problem has been take care of since WCF and Service is running on different servers.
  3. Visual studio is running in administrator credentials or credentials having enough permission to debug on server.

While working today I came across a requirement to debug WCF Service from Silverlight and it took me one hour to find the solution, I had above three assumptions met and I was not able to debug WCF Service from Silverlight.

I had architecture of solution as below,

clip_image001

There is nothing unusual in above architecture. I had Silverlight, Web Application and WCF as part of the same solution. Problems came when I tried to debug WCF service from Silverlight. I was getting below information at breakpoint in the service side and I was never able to hit the service while debugging.

clip_image002

I binged lot but did not get quiet good solution suggesting solving this. Eventually I found it very simple way to solve this.

Follow steps as below to hit the breakpoint in the service ,

Set web application as Startup project

clip_image003

Put breakpoint on Xaml.cs and run Silverlight application and once hitting breakpoint on XAML page stop for a while

clip_image005

While breakpoint on XAML is in hit select Tool from the menu and choose Attach to Process option

clip_image006

And then attach WCF host server.

clip_image008

Once after attaching WCF host server you should able to hit the breakpoint in WCF Service from Silverlight.

I hope this post was useful. Thanks for reading.

If you find my posts useful you may like to follow me on twitter http://twitter.com/debug_mode or may like Facebook page of my blog http://www.facebook.com/DebugMode.Net If you want to see post on a particular topic please do write on FB page or tweet me about that, I would love to help you.

 

Reading coordinate values using Accelerometer API in Windows Phone 7

In this post we will see how to read different axis values using Accelerometer API.

To work with Accelerometer API first you need to add reference of Microsoft.Devices.Sensors

clip_image001

Then below namespace,

clip_image001[6]

Now to capture X Axis, Y Axis and Z Axis value you need to follow below steps

Step 1

Create task object of Accelerometer chooser

clip_image001[8]

Step 2

Call the callback method when user will complete a task and Implement the completed event to capture user data and status when user complete as tasks.

clip_image001[10]

Step 3

Call the start method to start the accelerometer,

clip_image001[12]

Step 4

We can read value of XAxis, YAxis and ZAxis as below along with timestamp of capturing data as below in the completed event. We are changing double values to string.

clip_image001[14]

You can save these data for further use that when accelerometer value got changed.

Let us run the application in debug mode to see value got captured,

image

When you click on Capture Data button control will go to Accelerometer reading changed event. Then each time you will move the ball Accelerometer reading changed event will get called.

image

For your reference full code is as below,


using System;
using System.Windows;
using Microsoft.Phone.Controls;
using Microsoft.Devices.Sensors;

namespace EncryptingandDecryptinginMango
{
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
}
void accelerometerTaskObject_ReadingChanged(object sender, AccelerometerReadingEventArgs e)
{
string xCordinate = e.X.ToString("0.00");
string yCordinate = e.Y.ToString("0.00");
string zCordinate = e.Z.ToString("0.00");
DateTimeOffset timeCaptured = e.Timestamp;
}

private void btnCaptureData_Click(object sender, RoutedEventArgs e)
{

var accelerometerTaskObject = new Accelerometer();
accelerometerTaskObject.ReadingChanged +=
new EventHandler<AccelerometerReadingEventArgs>
(accelerometerTaskObject_ReadingChanged);

try
{
accelerometerTaskObject.Start();
}
catch
{
//Error in starting the Accelerometer
}

}
}
}

In this way you can work with Accelerometer API to capture different axis values. I hope this post was useful. Thanks for reading Smile

If you find my posts useful you may like to follow me on twitter http://twitter.com/debug_mode or may like Facebook page of my blog http://www.facebook.com/DebugMode.Net If you want to see post on a particular topic please do write on FB page or tweet me about that, I would love to help you.

Code to Send SMS in Windows Phone 7

Launchers are used to perform task provided by phone operating system. Sending SMS is feature of Windows phone operating system and can be used by Launcher API. SmsCompseTask launcher is used to send SMS. This task class is defined as below,

image

To work with SmsComposeTask, first you need to add namespace of Microsoft.Phone.Task

clip_image001

Then create instance of SmsComposeTask.

clip_image003

Next you need to set required or optional parameter for the launcher task. Required parameters for SmsComposeTask are

  1. To number
  2. Body of SMS

 

So first set the parameter as below,

clip_image001[6]

And SMS body as below,

clip_image002

Last step you need to launch SMS send application provided by operating system. You can show that as below,

clip_image003

If you put all code together and want to send SMS on click event of button then full code will be as below,


using System;
using System.Windows;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;

namespace test
{
public partial class MainPage : PhoneApplicationPage
{

public MainPage()
{
InitializeComponent();
}

private void btnSendSMS_Click(object sender, RoutedEventArgs e)
{

SmsComposeTask smsComposeTask = new SmsComposeTask();
smsComposeTask.To = "9158153792";
smsComposeTask.Body = "hey I am sending u SMS";
smsComposeTask.Show();

}
}
}

&nbsp;

When you run above code you will get application running as below,

image

When you click on Send SMS button, you will get drafted SMS with TO and body set as below,

image

Now you can either edit the message or set other recipients or send SMS as it is. I hope this post was useful. Thanks for reading Smile

If you find my posts useful you may like to follow me on twitter http://twitter.com/debug_mode or may like Facebook page of my blog http://www.facebook.com/DebugMode.Net If you want to see post on a particular topic please do write on FB page or tweet me about that, I would love to help you.

Protecting password or any data in Windows Phone 7 using Data Protection API

There may be many scenarios, when you need to save data in protected form for your application in isolated storage. Of course you have an option

  • To encrypt the data with some encryption algorithm
  • Save in the isolated storage

Problem with above approach is either you will have to store the key in isolated storage or read it from some external sources. In major scenarios key is residing on the phone itself and it brings the flaw.

Windows Phone Data Protection API helps us to protect data at the application level. On Windows Phone 7 every application has their own key. This key gets created when you first run the application.

Using DP API, all it takes one line of code to encrypt and decrypt the data.

To Encrypt

image

First line of code is converting string to byte array. To encrypt data only you need to pass byte array.

To Decrypt

image

Password is name of the file in which encrypted data is stored. All it takes only one line of code to decrypt.

If you closely look into ProtectedData class,

image

This is static class with two static methods. If required you can pass entropy as parameter also.

Design page

Now let us design a page like below, Design is very simple with one text box taking input to protect the data. There are two buttons, one to protect data and another to decrypt and retrieve protected data.

clip_image001

XAML of design is as below,


<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="PageTitle" Text="protected data" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle2Style}"/>
</StackPanel>
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Grid.RowDefinitions>
<RowDefinition Height="80" />
<RowDefinition Height="180" />
</Grid.RowDefinitions>
<TextBox x:Name="txtDataToProtect" HorizontalAlignment="Left" Width="450" Height="80" />
<StackPanel Orientation="Horizontal" Margin="0,0,0,0" Grid.Row="1">
<Button x:Name="btnReteriveData" Content="Reterive" Height="100"  Click="btnReteriveData_Click" Width="200" />
<Button x:Name="btnProtectData" Content="Protect" Height="100"  Click="btnProtectData_Click" Width="260" />
</StackPanel>
</Grid>
</Grid>

Protecting Data


private void btnProtectData_Click(object sender, RoutedEventArgs e)
{
byte[] passwordData = Encoding.UTF8.GetBytes(txtDataToProtect.Text);
byte[] EncryptedPasswordData = ProtectedData.Protect(passwordData, null);
SaveToFile(EncryptedPasswordData, "password");
}

In above code,

  1. We are converting text to byte array
  2. Passing byte array to protect
  3. We are applying no entropy.
  4. Saving protected data to a file called password. We will have to fetch encrypted data from file password while retrieving.
  5. To save protected data calling a function called SaveToFile

SaveToFile function


private void SaveToFile(byte[] EncryptedPasswordData,string FileName)
{
IsolatedStorageFile getApplicationFile = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream fileAsStream = new IsolatedStorageFileStream(FileName, System.IO.FileMode.Create, FileAccess.Write, getApplicationFile);
Stream writer = new StreamWriter(fileAsStream).BaseStream;
writer.Write(EncryptedPasswordData, 0, EncryptedPasswordData.Length);
writer.Close();
fileAsStream.Close();
}

In above code,

  1. We are passing byte array to save
  2. We are passing filename to say where to save the data in application
  3. Reading application file and opening it as file stream
  4. Writing byte array to file stream.

Decrypting data


private void btnReteriveData_Click(object sender, RoutedEventArgs e)
{
byte[] data = ReadFromFIle("password");
byte[] passwordByte = ProtectedData.Unprotect(data, null);
string password = Encoding.UTF8.GetString(passwordByte, 0, passwordByte.Length);
txtDataToProtect.Text = password;

}

In above code,

  1. We are first reading encrypted data from the isolated storage file. If you notice file name is same.
  2. Calling unprotect method and passing encrypted byte array to decrypt.
  3. Converting decrypted byte array to string and displaying in text box

ReadFromFile function


private byte[] ReadFromFIle(string FileName)
{

IsolatedStorageFile getApplicationFile = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream fileAsStream = new IsolatedStorageFileStream(FileName, System.IO.FileMode.Open, FileAccess.Read, getApplicationFile);
Stream reader = new StreamReader(fileAsStream).BaseStream;
byte[] password = new byte[reader.Length];
reader.Read(password, 0, password.Length);
reader.Close();
fileAsStream.Close();
return password;
}

In above code,

  1. Reading application file and opening it as file stream
  2. Reading byte array to file stream.

If you would have noticed we have not provided KEY either to ENCRYPT or DECRYPT the data.

In this way you can protect data in Windows Phone 7. I hope this post was useful. Thanks for reading Smile

If you find my posts useful you may like to follow me on twitter http://twitter.com/debug_mode or may like Facebook page of my blog http://www.facebook.com/DebugMode.Net If you want to see post on a particular topic please do write on FB page or tweet me about that, I would love to help you.

What is Channel in WCF?

In Service oriented architecture there are two parties involved, Server and Client .Server and client communicate to each other via Messages. Message travels over the network. For communication; server and client must be agreed upon

  • Format of message
  • Protocol
  • Transport
  • Encoding of the message etc.

Channels are the responsible component of WCF for

  • Creation of consistent message
  • Transport of consistent message.
  • Conversion of Message in wire format

image

The main goal of Channel is to transform message to format understandable by the communication wire and compatible to both server and client and then transport the message over the wire in between client and server.

image

Essentially Channel has to decide about

  • Protocol used in the message
  • Encoding in the message
  • Message is reliable or not
  • Security of the message
  • Transaction of the message

Mainly channel performs two different types of task and on basis of task channel is of two types,

image

Transport channels are responsible to send and receive the message using a transport protocol. WCF supports HTTP, TCP, and MSMQ, Peer to Peer and Named Piped transport protocols.

Protocol channels are responsible to implement and support security, transaction and reliable messaging. These channels transform and modify the message to make it compatible to wire level.

In a communication there may be one or more than one transport and protocol channels. Collections of different channels are called as Channel Stack.

Different channel performs different tasks. For example one protocol channel can secure the message whereas another protocol channel can make message reliable.

In WCF architecture, transport channel always resides at the bottom of channel stack. A usual channel stack can be depicted as below,

image

WCF provides us many readymade channel stacks to do communication between service and client. This WCF provided channel stacks are known as Binding. One channel layer is defined by one Binding Element.

image

You are very much allowed to create your own channel stack if all WCF provided bindings are not meeting your requirement.

Transport protocol of service and client communicates to each other.

image

Transport channel converts messages into bytes or binary to send over the network. Encoding of messages is also done by the transport channels.

I hope you are having basic idea about channel now. Thanks for reading Smile

If you find my posts useful you may like to follow me on twitter http://twitter.com/debug_mode or may like Facebook page of my blog http://www.facebook.com/DebugMode.Net If you want to see post on a particular topic please do write on FB page or tweet me about that, I would love to help you.

Calling One-Way WCF Service using IOutputChannel

Note: This post is inspired by one of the chapter from book Essential WCF. I tried to put the content in my way. Thanks to the original author. I strongly recommend you to read this post http://winterdom.com/2007/08/ioutputchanneloverirequestchannel

One way communication pattern allows client to send message to service and forget. Client doesn’t have to wait for the response.

One way WCF Service can be created as below,


using System.ServiceModel;

namespace WcfService1
{
[ServiceContract]
public interface IService1
{

[OperationContract(IsOneWay=true)]
void  InsertData();
}
}

What all you need to do is to pass parameter to Operation Contract as below,

clip_image001

Service is implemented as below,

namespace WcfService1
{

public class Service1 : IService1
{
public void InsertData()
{
//Insert here
}
}
}

&nbsp;

In one way communication two interfaces are very important,

clip_image002

These two interfaces are used to send messages between service and client in one way communication pattern.

clip_image003

At the service side we are using basicHttpBinding and this does not support one-way communication. It is made for Request-reply communication pattern. So you may consider using custom binding made on stack of basicHttpBinding.


static CustomBinding CreateOneWayBinding(Binding baseBinding)
{
List<BindingElement> elements  = new List<BindingElement>();
elements.Add(new OneWayBindingElement());
elements.AddRange(baseBinding.CreateBindingElements());
return new CustomBinding(elements);
}

&nbsp;

Above function can be used at WCF client side as well to create custom binding supporting one-way communication.

At client side we can call One-Way WCF Service using IOutputChannel. For that you need to add below references to the client project.

clip_image002[6]

You can create at client side one-way communication supporting custom binding as below,

image

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace Client
{
class Program
{
static void Main(string[] args)
{
BasicHttpBinding binding = new BasicHttpBinding();
CustomBinding bindingToprfoemOneWay = CreateOneWayBinding(binding);
BindingParameterCollection parameters = new BindingParameterCollection();
Message message = Message.CreateMessage(MessageVersion.Soap11,"urn:sendmessage");
IChannelFactory<IOutputChannel> factory = bindingToprfoemOneWay.BuildChannelFactory<IOutputChannel>(parameters);
factory.Open();
IOutputChannel channel = factory.CreateChannel(new EndpointAddress("<a href="http://localhost:10828/Service1.svc">http://localhost:10828/Service1.svc</a>"));
channel.Open();
channel.Send(message);
channel.Close();
factory.Close();
Console.ReadKey();
}
}
}

If you are not using custom binding at service side and relying on basicHttpBinding then very likely you will get below exception,

clip_image001[6]

In this way you can make one-way communication call to WCF Service from the client.

I hope this post was useful. Thanks for reading Smile

If you find my posts useful you may like to follow me on twitter http://twitter.com/debug_mode or may like Facebook page of my blog http://www.facebook.com/DebugMode.Net If you want to see post on a particular topic please do write on FB page or tweet me about that, I would love to help you