JG Vimalan's Blog

It is all about coding!

Consuming WCF service in Android application

In order to consume WCF service in an Android application, the WCF Service must send and receive data in JSON format. So, you need to set the RequestFormat and ResponseFormat to JSON, in the WebInvoke attribute as shown below,

using System.ServiceModel;
using System.ServiceModel.Web;

    [ServiceContract]
    public interface IService
    {      
        [OperationContract]
        [WebInvoke(Method = “POST”,
        UriTemplate = “/GetEmployee”,
        RequestFormat = WebMessageFormat.Json, 
        ResponseFormat = WebMessageFormat.Json)]
        Employee GetEmployee(EmployeeIdentity request);

I used wsHttpBinding in my WCF service.  

Now, to consume the WCF Service in your Android application, use httpClient to process the request and get the response. A sample code is given below,

HttpClient httpClient = null;
HttpUriRequest httpUriRequest = null
HttpResponse httpResponse = null;

try {
      
httpUriRequest = //build request
httpClient = new DefaultHttpClient();
httpResponse = httpClient.execute(httpUriRequest);

Advertisements

October 29, 2010 Posted by | Android, JSON, WCF | 1 Comment

Create a Addin for Visual Studio

This post describes the steps involved in creating an addin for Visual Studio. To create a Addin project, click File->New->Project choose ‘Extensibility’ under ‘Other Project Types’. Follow the instructions in the wizard to create the Addin project. Once you create the project, Connect.cs file in the project. The .cs file contains the OnConnection method. In this method, you can set the menu item under which your addin should appear as well as your addin name.

  public void OnConnection(object application, …
  {
   _applicationObject = (DTE2)application;
   _addInInstance = (AddIn)addInInst;
   if(connectMode == ext_ConnectMode.ext_cm_UISetup)
   {
    object []contextGUIDS = new object[] { };
    Commands2 commands = (Commands2)_applicationObject.Commands;
    string toolsMenuName = “View”;

Now, if you run the application, you will find the addin under the ‘View’ menu item in visual studio,



To add fuctionality to this addin, use the Exec method in the Connect.cs file. Here, I have added a piece of code to display a message. 

public void Exec
            (string commandName,
            vsCommandExecOption executeOption,
            ref object varIn,
            ref object varOut,
            ref bool handled)
  {
   handled = false;
   if(executeOption ==
                vsCommandExecOption.vsCommandExecOptionDoDefault)
   {
    if(commandName == “MyVSAddin.Connect.MyVSAddin”)
    {
     handled = true;

                    System.Windows.Forms.MessageBox.Show(
                        “Hello Universe!”,
                        “Custom Message”,
                        System.Windows.Forms.MessageBoxButtons.OK);
                   
     return;
    }
   }
  }

you can your own logic, launch a form, etc.,

So, on clicking the Addin, the Exec method will get fired and the message box will be displayed as shown below,

October 28, 2010 Posted by | VS 2010 | Leave a comment

Connect to tfs via API and get the build status of team projects

The following code snippet  explains the way to connect to tfs programmatically and get the build status of team projects.

This will be helpful when you create a custom application to display the build status of certain team projects in tfs.

 using Microsoft.TeamFoundation.Client;
 using Microsoft.TeamFoundation.Build.Client;
 …
 …

 private IQueuedBuildsView[] myTfsQueuedBuilds= null;
 private IQueuedBuildsView myTfsBuildQueue;
         /// <summary>
        ///  Create initial connection to the TFS 2008 Build Server.
        /// </summary>
        public void Connect()
        {
            TeamFoundationServer tfs = new TeamFoundationServer(TfsUrl);
            IBuildServer buildServer = (IBuildServer)
            tfs.GetService(typeof (IBuildServer));
            ArrayList queuedBuildsList = new ArrayList();

            //teamProject is the string array of team project name in tfs
            foreach (string project in teamProject)
            {
                myTfsBuildQueue = buildServer.CreateQueuedBuildsView (project);               
               
                myTfsBuildQueue.StatusChanged += new
                       StatusChangedEventHandler(myTfsBuildQueue_StatusChanged);

               //1000 ms and ‘myForm’ is the instance of the page
                myTfsBuildQueue.Connect(1000, myForm);

                queuedBuildsList.Add(myTfsBuildQueue);
            }
           
            myTfsQueuedBuilds= new IQueuedBuildsView[queuedBuildsList.Count];
            for(int i=0; i<queuedBuildsList.Count; i++)
            {
                queuedBuilds[i] = (IQueuedBuildsView)queuedBuildsList[i];
            }
        }

        private void buildQueue_StatusChanged(object sender, StatusChangedEventArgs e)
        {
            foreach(IQueuedBuildsView qbv in queuedBuilds)
            {
                if (e.Changed && qbv.QueuedBuilds.Length > 0)
                {
                    foreach (IQueuedBuild qb in qbv.QueuedBuilds)
                    {
                        //update status in UI
                    }
                }
            }           
        }

October 28, 2010 Posted by | TFS | Leave a comment

Android requires .class compatibility set to 5.0. Please fix project properties

I got this error message when I created my first android application. Right after creating the application, when I tried to run, the error message was displayed. I fixed this error message by selecting the android sdk version in the java build path. That is, select the project and goto its properties. Select Java Build Path, check the android sdk version in the ‘order and export’ tab. See the image below,

this will fix the error message. Now you will be able to run your android application.



October 27, 2010 Posted by | Android | 2 Comments

Setup Android development environment

In this post, I am going to explain the steps involved in setting up Android development environment.

Android Development Environment for Windows 

REF: http://developer.android.com/sdk/index.html

REF: http://developer.android.com/sdk/installing.html

Android SDK

–goto developer.android.com & SDK (Android SDK 2.2)

–download SDK

Eclipse

–goto www.eclipse.org/downloads

–download Eclipse IDE for Java Developers for Windows (Eclipse IDE for Java Developers Windows 32 bit)

JDK

–goto java.sun.com/javase/downloads

–download JDK 6

-Install JDK first

-Extract Android to D:\AndroidSDK. Follow the instructions given in the SDK Readme.txt file.

-Extract Eclipse package

-Open Eclipse

-Choose Workspace

Configure Android Development Tools Plug-in

—goto Help->Install New Software

—Click on Add button

—Type Google Android as the name in the popup

—TYpe https://dl-ssl.google.com/android/eclipse in the Location and click OK

–Check the checkbox & click next

–Accept the Terms & Conditions and click on Finish
 

–Restart Eclipse

–Goto Window -> Preferences.

–Select Android from left pane

–Select the Android SDK location and Apply

AVD
You must create an Android Virtual Device (AVD).
An AVD defines the system image and device settings used by the emulator.

-Create an AVD:

—In Eclipse, choose Window > Android SDK and AVD Manager.
—Select Virtual Devices in the left panel.
—Click New. The Create New AVD dialog appears.
—Keyin the name of the AVD, such as “demoavd”.
—Choose a target platform (say, android 2.2) you want to run on the emulator.
—Click Create AVD.

—————————–

Create a new project

—————————–

-File>New>Project

-Choose an Android project

-Specify Project Name

Thats all! Enjoy creating Android applications!

October 25, 2010 Posted by | Android | 4 Comments

Consuming WCF service in Silverlight application

In order to consume WCF service in your silverlight application, add the WCF Service as a service reference in your Silverlight application. Once you do that, you can see a ServiceReferences.ClientConfig file in your silverlight project. It will have the binding information and the endpoint as shown below,

<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name=”basicEndPoint” maxBufferSize=”2147483647″ maxReceivedMessageSize=”2147483647″>
                    <security mode=”None” />
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address=”http://mySystem/WCFService/Manager.svc” binding=”basicHttpBinding”
                bindingConfiguration=”basicEndPoint” contract=”SampleWCFService.IManager”
                name=”basicEndPoint” />
        </client>
    </system.serviceModel>
</configuration>

So, its time to invoke the methods exposed by the WCF service. In silverlight, it is slightly different when compared to asp.net web application. You will call the asynchronous method and handle its completed event, where you get the result.

private void button1_Click(object sender, RoutedEventArgs e)
        {
            SampleWCFService.ManagerClient client = new
                SampleWCFService.ManagerClient();

            client.GetEmployeesCompleted += new
                EventHandler<SampleWCFService.GetEmployeesCompletedEventArgs>(client_GetEmployeesCompleted);

            client.GetEmployeesAsync(); //invoke the GetEmployees method here
        }

        void client_GetEmployeesCompleted(object sender, SampleWCFService.GetEmployeesCompletedEventArgs e)
        {
            //you will get the result object here
            System.Collections.ObjectModel.ObservableCollection<SampleWCFService.Employee> employees =
                e.Result;

            if (employees.Count() > 0)
            {
                //put code here
            }
        }

October 21, 2010 Posted by | Silverlight, WCF | 1 Comment