Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Wednesday, June 28, 2017

Step By Step : Excel-DNA, C#, Excel User Defined Functions

Hi,

Welcome to my blog of Excel DNA using Visual Studio and C#. In this post we will see how to
create Excel User Defined Functions ( UDFs ) using Visual Studio and C# by using Excel-DNA.

Introduction: 
As per https://excel-dna.net/, Excel-DNA is an independent project to provide high-performance user-defined functions ( UDFs ), create custom ribbon bar and more to your Excel sheet. It allows to develop native .xll add-ins for Excel using C#, VB.NET, F#. It helps to integrate .NET into Excel.

Prerequisites:
Since we are integrating .NET into Excel we need Visual Studio. We can get Visual Studio 2015 Community Edition for free at https://beta.visualstudio.com/downloads/.

Once you install Visual Studio you are ready to go. So let's dig in and see step-by-step guide of creating a user-defined function ( UDF ) and execute that in Excel.

Create Project:
Launch Visual Studio. You need to create a Class Library Project. You can create by going to
File -> New -> Project and select Visual C# and then select Class Library project and give it a name like MyExcelDNALibrary. Please see below
























This creates a new solution for you like below
























Next step you need to do is add Excel-DNA add-in to your class library project. This can be done using NuGet Package Manager ( this is included in most of the recent visual studio versions).

In your Visual Studio, go to View menu at the top and the Other Windows and select Package Manager Console. This will open Package Manager Console pane. Please see below


















Note : In Package Manager Console make sure you select the correct project under default project dropdown. In above image the red circled one.

Give the following command next to PM> in Package Manager Console and press Enter 
Install-Package Excel-DNA. 
You will get a bunch of text successful and complete messages and Excel-DNA add-in will
be added to your project.










Now your Solution Explorer must look like below




















This step is optional. Open Solution Explorer and right click on Class1.cs and then select Rename to rename class to UDFHelper.




























Now double click above class to open it and give using ExcelDna.Integration; at the top. Your class must look like below


























To make things simple, let's add a function that gets current date and time like below

    [ExcelFunction(Name = "CURRENTDATETIME")]
    public static DateTime CurrentDateTime()
    {
        return DateTime.Now;
    }

Lets also add a function that adds 2 numbers and returns result
       [ExcelFunction(Name = "ADDTWONUMBERS")]
        public static int Add(int a , int b)
        {
            return a + b;
        }

Now your class must look like below





























ExcelFunction tells that the function must be exposed in Excel as work sheet function. 
[ExcelFunction(Name = "CURRENTDATETIME")] tells that .NET C# CurrentDateTime function must be exposed in Excel as CURRENTDATETIME.
[ExcelFunction(Name = "ADDTWONUMBERS")] tells that .NET C# Add function must be exposed in Excel as ADDTWONUMBERS.

In Solution Explorer right-click on your Excel-DNA Class Library project and select Properties. This will open properties tab. Go to Debug and make sure you have Excel.exe location
for Start external program. In my case it was C:\Program Files (x86)\Microsoft Office\Office14\EXCEL.EXE

If you have 32-bit Excel then you can skip below steps and go directly to testing. If you have 64-bit Excel please follow bekow steps.

64-bit Excel:
In Solution Explorer right-click on your Excel-DNA Class Library project and select Properties. This will open properties tab. Go to Debug.













In Command line arguments : add 64 before .xll  for example "MyExcelDNALibrary-AddIn64.xll" like below. This will tell that 64-bit version XLL will be loaded in Excel when you debug the application else you will get error



















Test:
We are now ready to test. In Visual Studio, in command bar make sure you select Debug and then click on the green triangle to run the project or else press F5 on keyboard to run application.

















If you donot have any compile errors, your solution will be compiled and Excel-DNA creates few xll files. Excel should be launched and depending on your security settings it will ask whether
to enable the add-in. Just select Enable button. This will open your Excel session.




















Select a cell in excel and give =CURRENTDATETIME() like below. ( If everything goes fine Excel intellisense will show the method like below).

















Select method like below to see result.


















In order to format cell, select cell and right-click and follow below


































Now you must see sensible date like below





















To use Add follow below




















Conclusion :
In this article we saw how we can create user defined functions (UDFs) and use them in Excel using Excel-DNA add-in. We created functions using .NET and C#. In future post I will show you how to create custom ribbon bar in Excel and how we can add buttons to it and perform some functionality like open a web-browser and launch a sample page. Till then I hope you find this post useful.


I hope you enjoyed reading this post as much as I did while writing it. Please feel free to let me know your valuable and constructive comments and suggestions which can help me to make this article better and improve my technical and written skills. Last but not the least please excuse me for my grammar and typos.

Thanks and have a nice and wonderful day.


Saturday, February 25, 2017

Step by Step : Windows Phone 7 : Setting Up Development Environment

Hi,
Welcome to my blog of Windows Phone 7 Development. In this series of posts, I will try to explain various concepts in Windows Phone 7 Development. We will try to learn how to develop applications for Windows Phone 7.  So lets start.

In this post we will see how to set up Windows Phone 7 Development environment.

Introduction: 
Windows Phone 7 Development targets developing applications for Windows Phone 7 mobile operating system developed by Microsoft..NET framework is used to do Windows Phone 7 Development created by Microsoft. We can use any one of the several languages supported by the .NET framework, like C# to write applications which gets executed inside of a runtime environment called the Common Language Runtime. There are two distinct development approaches you can take when creating your application.

First Approach:
The first approach is to use Silverlight for Windows Phone. Silverlight is used by developers to create rich internet applications. A Silverlight application uses declarative markup (called XAML) to create user interface and code written in a .NET framework language to control an application’s behavior. For developing a data driven application for Windows Phone 7, we need to use Silverlight.

Second Approach:
The second approach is to use the XNA framework to develop your Windows Phone 7 app. It is Microsoft’s game development framework and has been used in recent years to create both Windows and Xbox 360 applications. If you’re creating a game for Windows Phone 7, you’ll likely use the XNA framework. The XNA framework is quite powerfu and need a great deal of learning curve and longer development cycles.

Installing SDK:
The Windows Phone 7 SDK is supported with one of the below operating systems except starter ( express ) versions
  • Windows Vista
  • Windows 7
Note : You cannot develop Windows Phone 8 applications using Windows Phone 7 SDK.
Hardware Requirements :
  • Minimum 4GB hard disk space
  • Minimum 3GB RamDirectX 10 or above capable graphics card with a WDDM 1.1 driver
Please go to below link and download and install below items
http://dev.windows.com/en-us/develop/download-phone-sdk
  • Windows Phone SDK 7.1
  • Windows Phone SDK 7.1.1 Update















This will install the following in your system
  • Microsoft Visual Studio 2010 Express For Windows Phone
  • Windows Phone Emulator
  • Windows Phone SDK 7.1 Assemblies
  • Silverlight 4 SDK and DRT
  • Windows Phone SDK 7.1 Extensions for XNA Game Studio 4.0
  • Micorosoft Expression Blend SDK for Windows Phone 7
  • Microsoft Expression Blend SDK for Windows Phone OS 7.1
  • WCF Data Services Client for Windows Phone
  • Microsoft Advertising SDK for Windows Phone
If your installation goes fine you will have all the necessary software requirements that are needed to develop Windows Phone 7 mobile applications.

Conclusion:
In this post we saw what are the hardward and software requirements to develop Windows Phone 7 applications and how we can install Windows Phone 7 SDK that will install the required components to start developing Windows Phone 7 applications.

In next post we will develop our first Windows Phone 7 application which is Hello World application.
I hope you enjoyed reading this post as much as I did while writing it. Please feel free to let me know your valuable and constructive comments and suggestions which can help me to make this article better and improve my technical and written skills. Last but not the least please excuse me for my grammar and typos.

Thanks and have a nice and wonderful day.





Friday, July 24, 2015

Step By Step : Create and Consume WCF Restful Service

Hi,

Welcome to my blog. In this post we will see how we can create a WCF RESTFUL service and how Windows Forms Application calls methods in that WCF RESTFUL service using WebRequest and WebClient classes.

Overview:
When I started to look into Restful web services, I was able to find many posts regarding how to implement a simple RESTful web service using WCF. But it was hard for me to find out how to calling RESTFUL service methods using HTTP verbs like POST, GET, DELETE and PUSH using C# code. Every post I came across was happpy to tell about call GET methods using Fiddler or browser or call methods using Javascript ot JQuery. I wanted some sample using C# code which calls all methods using HTTP verbs like POST, GET, DELETE and PUSH. So, I wrote this post so that it will be helpful for the needy.

In this post we will see step by step process of how to implement a simple RESTful web service using WCF and using a Windows Forms application and call WCF RESTFUL service methods using HTTP verbs like POST, GET, DELETE and PUSH.

This is just one of the way which I followed  I am not saying this is the best and it doesnot have cons. My main aim is to share my experience so that it will be useful for the needy. I tried to make this post as much interesting as possible. Please excuse me for my grammatical errors and typos. I welcome your constructive comments and suggestions which helps in improving this post and my technical skills.

Introduction:
REST stands for Representational State Transfer. RESTful web services use HTTP verbs. They expose either a collection resource (representational of a list) or an element resource (representational of a single item in the list).

HTTP verbs maps CRUD operations to HTTP methods as below

Create (POST) => create a new resource.
Read (GET) => retrieve one or many resources.
Update (PUT) => update an existing resource.
Delete (DELETE) => delete an existing resource.

Now let's start our work
For this article, I am using the following tools/technologies to develop a my sample application
Visual Studio 2013
C#
.NET Framework 4.5

Our sample application will launch a windows form UI with a datagrid view to show results and it has buttons to do the following
Get Books : Get all books. This uses WebRequest class to make service call.
Get Book : Get a book depending on ID. This uses WebClient class to make service call.
Add Book : Create a new book.This uses WebRequest class to make service call
Update Book: Update an existing book with new details. This uses WebClient class to make service call
Delete Book: Delete a particular book. This uses WebRequest class to make service call

The final UI will look something like this.























I always have the habit of creating empty solution first and then work on it. Here also I did same thing. I created an empty visual studio solution titled "RestfulTutorials" and added below 3 projects.
Data : This a standard C# class library. The data project will contain our entities, and all logic that provided data to our application. In this case, for simplicity, I added nothing.

WCFService: This is a WCF Service library and contains our WCF RESTful service definition, all associated configuration, and each of our CRUD methods

WindowsClient: This is a sample windows forms application that will call WCF service methods using HTTP verbs(POST,PUT...).

After adding your projects your solution must look like below.
























Data : Add new class and name it as Book.cs. Add below code to it. We need to decorated Book class with DataContract attribute so that it can be passed between client ( WindowsClient) and service (WCFService). Book class is serialized and pass between client and server. Each property that we want to be serialized is decorated with DataMember attribute

[DataContract]
    public class Book
    {
        [DataMember]
        public int Id { get; set; }
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public float Price { get; set; }
    }


Data project is simple. As I said it has nothing. Build your solution to make sure you donot have any errors.

WCFService: Make sure you have below references in your WCFService project and rename IService1.cs to IBookService.cs and Service1.cs to BookService.cs. Delete everything in IBookService.cs and BookService.cs. You project must look like below



















Build your solution to make sure you donot have any errors.
Open App.config and do the following

1) Make sure that the service name matches the full namespace for your service interface;
<system.serviceModel>
    <services>
      <service name="WCFService.BookService">
    
2) Add behavior like below
<endpointBehaviors>
        <behavior name="web">
          <webHttp automaticFormatSelectionEnabled="true" defaultOutgoingResponseFormat="Json"/>
        </behavior>
</endpointBehaviors>

3) Add endpoint with webHttpBinding like below and with behaviorConfiguration equals to above behavior name.
 <endpoint address="" binding="webHttpBinding" contract="WCFService.IBookService" behaviorConfiguration="web">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>

4) Update the baseAddress appropriately. Here I am updating the base address to tell WCF to use the port number 8085 and simplifed the address
<host>
          <baseAddresses>
            <add baseAddress="http://localhost:8085/BookService/" />
          </baseAddresses>
 </host>

Your App.config must looks like below






















Now add a reference to the Data project to WCFService project since we need to use Book object in service project.

Now in IBookService.cs add GetBooks() like below which returns list of Books. The WebGet attribute is a REST specific attribute indicating that the operation is accessible via the HTTP GET verb.  The WebInvoke attribute can be used for POST, PUT and DELETE verbs. To make the operations RESTful, we need to update WebGet attribute to indicate that we can execute this method using http://localhost:8085/BookService/Books url.
 [OperationContract]
 [WebGet(UriTemplate="/Books")]
 IList<Book> GetBooks();



It is also possible to pass parameters into URL in order to return specific book rather than all books. Please add GetBook method like below.
  [OperationContract]
  [WebGet(UriTemplate = "/Book/{id}")]
  Book GetBookById(string id);
       
A place-marker ({id}) is used to indicate that a parameter will be provided, and that marker matches the name of the parameter accepted by the operation.  Unfortunately, parameter type in URL as always strings. We have to manually cast the string to desired type before using it.

Other HTTP verbs are just as easy to implement.  Add the following operations to your service contract

 [OperationContract]
 [WebInvoke(Method="POST", UriTemplate="/Book")]
  IList<Book> CreateBook(Book newBook);

  [OperationContract]
  [WebInvoke(Method="PUT", UriTemplate="/Book")]
  IList<Book> UpdateBook(Book book);

   [OperationContract]
   [WebInvoke(Method="DELETE", UriTemplate="/Book/{id}")]
   IList<Book> DeleteBook(string id);

Instead of using the WebGet attribute, we use the WebInvoke attribute, which basically means most other verbs other than GET.  As we’re not using Uri templates with place-markers, we can pass in a complex object from the client and WCF will just figure out what to do with it.

For completeness, here are IBookService.cs and BookService.cs;

public interface IBookService
    {
        // TODO: Add your service operations here
        [OperationContract]
        [WebGet(UriTemplate="/Books")]
        IList<Book> GetBooks();

        [OperationContract]
        [WebGet(UriTemplate = "/Book/{id}")]
        Book GetBookById(string id);

        [OperationContract]
        [WebInvoke(Method="POST", UriTemplate="/Book")]
        IList<Book> CreateBook(Book newBook);

        [OperationContract]
        [WebInvoke(Method="PUT", UriTemplate="/Book")]
        IList<Book> UpdateBook(Book book);

        [OperationContract]
        [WebInvoke(Method="DELETE", UriTemplate="/Book/{id}")]
        IList<Book> DeleteBook(string id);
    }  

 public class BookService : IBookService
    {
        IList<Book> books = null;
        public BookService()
        {
            books = new List<Book>();

            books.Add(new Book() { Id = 1, Name = "Book 1", Price = 10.34f });
            books.Add(new Book() { Id = 2, Name = "Book 2", Price = 20.34f });
            books.Add(new Book() { Id = 3, Name = "Book 3", Price = 30.34f });
            books.Add(new Book() { Id = 4, Name = "Book 4", Price = 40.34f });
            books.Add(new Book() { Id = 5, Name = "Book 5", Price = 50.34f });
        }
        public IList<Book> GetBooks()
        {
            return books;
        }

        public Book GetBookById(string id)
        {
            int bookId = Convert.ToInt32(id);
            return books.Where(book => book.Id == bookId).FirstOrDefault();
        }

        public IList<Book> CreateBook(Book newBook)
        {
            int newBookId = books.Max(x => x.Id);
            books.Add(new Book() {Id = newBookId+1, Name=newBook.Name, Price = newBook.Price });
            return books;
        }

        public IList<Book> UpdateBook(Book book)
        {
            Book updateBook = books.Where(x => x.Id == book.Id).First();
            updateBook.Name = book.Name;
            updateBook.Price = book.Price;
            return books;
        }

        public IList<Book> DeleteBook(string id)
        {
            Book deleteBook = books.Where(x => x.Id == Convert.ToInt32(id)).First();
            books.Remove(deleteBook);
            return books;
        }
    }

Build your solution to make sure you donot have any errors.

WindowsClient : Now add a reference to the Data project to WindowsClient project since we need to use Book object in service project.. Create windows form UI with 5 buttons and a datagridview.Here we demonstrate how to work with both WebRequest and WebClient classes.

WebRequest : This is an abstract class and we cannot use it directly. We use Create method to create an instance of WebRequest and use GetResponseStream() to return a data stream. FileWebRequest and FtpWebRequest classes inherit from WebRequest. We use WebRequest to make a request and convert the return to either HttpWebRequest, FileWebRequest or FtpWebRequest, depending on our request. Using HttpWebRequest, we have to get the response of our request, instantiate StreamReader to read the response and finally, convert the result to whatever type we expect

WebClient: It provides common operations to sending and receiving data from a resource identified by a URI. It is a higher-level abstraction of HttpWebRequest. The operations like DownloadData and DownloadFile is what differentiate WebClient from HttpWebRequest and simplify code we would do with HttpWebRequest.

The simplest one is calling GET methods. Please see below for GetBooks and GetBook methods calling.

GetBooks: Using WebRequest
private void btnGetBooks_Click(object sender, EventArgs e)
        {
            try
            {
                //This will match GetBooks in IBookService.cs
                WebRequest request = WebRequest.Create(@"http://localhost:8085/BookService/Books");
                request.Method = "GET"; //HTTP : GET method

                using (WebResponse response = request.GetResponse()) // calling REST method
                {
                    //below code to access the result.
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(IList<Book>));
                        IList<Book> books = (IList<Book>)serializer.ReadObject(responseStream);

                        bindingSource1.DataSource = books;
                        dvBooks.DataSource = bindingSource1;
                    }
                }
            }
            catch (Exception ex)
            {

            }

        }


GetBook : Using WebClient
   private void btnGetBook_Click(object sender, EventArgs e)
        {
            try
            {
               using(WebClient client = new WebClient())
               {
                   Uri uri = new Uri(@"http://localhost:8085/BookService/Book/2");
                   client.DownloadDataCompleted += OnGetBookCompleted;
                   client.DownloadDataAsync(uri);
               }

            }
            catch (Exception ex)
            {

            }
        }

        void OnGetBookCompleted(object sender, DownloadDataCompletedEventArgs e)
        {
            byte[] result = e.Result as byte[];
            MemoryStream responseStream = new MemoryStream(result);
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Book));
            Book book = (Book)serializer.ReadObject(responseStream);

            bindingSource1.DataSource = book;
            dvBooks.DataSource = bindingSource1;

        }


DeleteBook : This is also simple. Using WebRequest
 private void btnDeleteBook_Click(object sender, EventArgs e)
        {
            try
            {
               //This will match DeleteBook in IBookService.cs. I am hard coding id here
                WebRequest request = WebRequest.Create(@"http://localhost:8085/BookService/Book/2");
                request.Method = "DELETE"; // HTTP Delete Method

                //invoke REST method
                using (WebResponse response = request.GetResponse())
                {
                    //below code to access the result.
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(IList<Book>));
                        IList<Book> books = (IList<Book>)serializer.ReadObject(responseStream);

                        bindingSource1.DataSource = books;
                        dvBooks.DataSource = bindingSource1;
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }

AddBook: This is little bit tricky since we need to provide new book that must be added to collection. Please see below code. Using WebRequest
 private void btnAddBook_Click(object sender, EventArgs e)
        {
            try
            {
                Book newBook = new Book() { Id = 0, Name = "New Book", Price = 123.45f };

                //create new request
                //This will match AddBook in IBookService.cs
                WebRequest request = WebRequest.Create(@"http://localhost:8085/BookService/Book");
                request.Method = "POST";
                request.ContentType = "application/json; charset=utf-8";

                //add new update book info to request
                Stream stream = request.GetRequestStream();
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Book));
                ser.WriteObject(stream, newBook);

                //invoke REST method
                using (WebResponse response = request.GetResponse())
                {
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(IList<Book>));
                        IList<Book> books = (IList<Book>)serializer.ReadObject(responseStream); // since we get collection back

                        bindingSource1.DataSource = books;
                        dvBooks.DataSource = bindingSource1;
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }

UpdateBook: This is little bit tricky since we need to provide new book that must be updated and added to collection. Please see below code. Using WebClient
 private void btnUpdateBook_Click(object sender, EventArgs e)
        {
            try
            {
                using(WebClient client = new WebClient())
                {
                    client.Headers[HttpRequestHeader.ContentType] = "text/json";                  
                    Uri uri = new Uri(@"http://localhost:8085/BookService/Book");

                    Book updateBook = new Book() { Id = 3, Name = "UpdateBook Name 3", Price = 77.77f };

                    MemoryStream requestStream = new MemoryStream();
                    DataContractJsonSerializer requestSerializer = new DataContractJsonSerializer(typeof(Book));
                    requestSerializer.WriteObject(requestStream, updateBook);

                    client.UploadDataCompleted += OnUpdateBookCompleted;
                    client.UploadDataAsync(uri, "PUT",requestStream.ToArray());
                }

            }
            catch (Exception ex)
            {

            }
        }

        void OnUpdateBookCompleted(object sender, UploadDataCompletedEventArgs e)
        {
            byte[] result = e.Result as byte[];
            MemoryStream responseStream = new MemoryStream(result);
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(IList<Book>));
            IList<Book> books = (IList<Book>)serializer.ReadObject(responseStream);

            bindingSource1.DataSource = books;
            dvBooks.DataSource = bindingSource1;

        }


For completeness, here is full code for Form1.cs in WindowsClient project


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Net;
using System.IO;
using System.Xml.XPath;
using System.Xml;
using Data;
using System.Runtime.Serialization.Json;

namespace WindowsClient
{
    public partial class Form1 : Form
    {
        private BindingSource bindingSource1 = new BindingSource();

        public Form1()
        {
            InitializeComponent();
        }

        //GetBooks : GET
        private void btnGetBooks_Click(object sender, EventArgs e)
        {
            try
            {
                WebRequest request = WebRequest.Create(@"http://localhost:8085/BookService/Books");
                request.Method = "GET";

                using (WebResponse response = request.GetResponse())
                {
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(IList<Book>));
                        IList<Book> books = (IList<Book>)serializer.ReadObject(responseStream);

                        bindingSource1.DataSource = books;
                        dvBooks.DataSource = bindingSource1;
                    }
                }
            }
            catch (Exception ex)
            {

            }

        }

        //GetBook : GET
        private void btnGetBook_Click(object sender, EventArgs e)
        {
            try
            {
               using(WebClient client = new WebClient())
               {
                   Uri uri = new Uri(@"http://localhost:8085/BookService/Book/2");
                   client.DownloadDataCompleted += OnGetBookCompleted;
                   client.DownloadDataAsync(uri);
               }
            }
            catch (Exception ex)
            {

            }
        }

        void OnGetBookCompleted(object sender, DownloadDataCompletedEventArgs e)
        {
            byte[] result = e.Result as byte[];
            MemoryStream responseStream = new MemoryStream(result);
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Book));
            Book book = (Book)serializer.ReadObject(responseStream);

            bindingSource1.DataSource = book;
            dvBooks.DataSource = bindingSource1;
        }

        private void btnUpdateBook_Click(object sender, EventArgs e)
        {
            try
            {
                using(WebClient client = new WebClient())
                {
                    client.Headers[HttpRequestHeader.ContentType] = "text/json";                  
                    Uri uri = new Uri(@"http://localhost:8085/BookService/Book");

                    Book updateBook = new Book() { Id = 3, Name = "UpdateBook Name 3", Price = 77.77f };

                    MemoryStream requestStream = new MemoryStream();
                    DataContractJsonSerializer requestSerializer = new DataContractJsonSerializer(typeof(Book));
                    requestSerializer.WriteObject(requestStream, updateBook);

                    client.UploadDataCompleted += OnUpdateBookCompleted;
                    client.UploadDataAsync(uri, "PUT",requestStream.ToArray());
                }
            }
            catch (Exception ex)
            {

            }
        }

        void OnUpdateBookCompleted(object sender, UploadDataCompletedEventArgs e)
        {
            byte[] result = e.Result as byte[];
            MemoryStream responseStream = new MemoryStream(result);
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(IList<Book>));
            IList<Book> books = (IList<Book>)serializer.ReadObject(responseStream);

            bindingSource1.DataSource = books;
            dvBooks.DataSource = bindingSource1;
        }
      
        private void btnAddBook_Click(object sender, EventArgs e)
        {
            try
            {
                Book newBook = new Book() { Id = 0, Name = "New Book", Price = 123.45f };

                //create new request
                WebRequest request = WebRequest.Create(@"http://localhost:8085/BookService/Book");
                request.Method = "POST";
                request.ContentType = "application/json; charset=utf-8";

                //add new update book info to request
                Stream stream = request.GetRequestStream();
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Book));
                ser.WriteObject(stream, newBook);


                //invoke REST method
                using (WebResponse response = request.GetResponse())
                {
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(IList<Book>));
                        IList<Book> books = (IList<Book>)serializer.ReadObject(responseStream);

                        bindingSource1.DataSource = books;
                        dvBooks.DataSource = bindingSource1;
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }

        private void btnDeleteBook_Click(object sender, EventArgs e)
        {
            try
            {
                WebRequest request = WebRequest.Create(@"http://localhost:8085/BookService/Book/2");
                request.Method = "DELETE";

                //invoke REST method
                using (WebResponse response = request.GetResponse())
                {
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(IList<Book>));
                        IList<Book> books = (IList<Book>)serializer.ReadObject(responseStream);

                        bindingSource1.DataSource = books;
                        dvBooks.DataSource = bindingSource1;
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }
    }

}



That's it. Build your application so that we donot have any errors.
The easiest way to run your application is to do following. Right click on your Solution and go to Properties and select Multiple startup projects and Start WCFSErvice and WindowsClient projects as show below and click Ok.























Now run your application by simply pressing F5 which starts your WCFService and WindowsClient projects and if everything goes fine all the buttons must work like below.

GetBooks:


GetBook:


DeleteBook:























AddBook:























UpdateBook:
























Conclusion:
In this article we learned how to implement a simple RESTful web service using WCF and how we can call all the Restful HTTP methods using HTTP verbs like PUT, POST, GET, DELETE by using a simple windows form client application.

Please feel free to let me know your valuable and constructive comments and suggestions which can help me to make this article better and improve my technical and written skills. Last but not the least please excuse me for my grammar and typos.

Thanks and have a nice and wonderful day.

Wednesday, July 22, 2015

Step By Step : Custom Configuration Section in Configuration File

Hi,

Welcome to my blog. In this post we will see how to add custom configuration section to your config files and read those values. I added a custom configuration section called CountriesConfigSection in my App.config and will read values from it and access them in my Windows form application. In my App.config, the CountriesConfigSection will have entries for states belonging to a particular country. As shown below, I have 4 states for USA country and I will display names of USA states in a combobox in my Windows Form application.

App.Config

















This is just one of the way which I followed  I am not saying this is the best and it doesnot have cons. My main aim is to share my experience so that it will be useful for the needy. I tried to make this post as much interesting as possible. Please excuse me for my grammatical errors and typos. I welcome your constructive comments and suggestions which helps in improving this post and my technical skills.

Overview: 
In this example we will see how we can create custom configuration section in our app.config and how we can access the values in our code. We will create the following projects

1) CustomConfigSections : This is Windows Forms project.

Now let's start our work
For this article, I am using the following tools/technologies to develop a my sample application
Visual Studio 2013
C#
.NET Framework 4.5

I always have the habit of creating empty solution first and then work on it. Here also I did same thing. I created an empty visual studio solution titled "CustomConfigSections"
 I added a new Windows Forms project called "CustomConfigSections".
Solution






















































































Steps:
1) Open your App.config and add do below steps. Please see below screen shot
a) Inside <conifguration> add this
    <configSections>
    <section name="CountriesConfigSection" type="CustomConfigSections.CountriesConfigSection, CustomConfigSections"></section>
  </configSections>

 This step is important. It tells that we are adding our custom configuration section with name and of particular type.
  b) After above step add this
   <CountriesConfigSection>
    <USA>
      <State name="Alabama" capital="Montgomery" symbol ="AL" population="100"/>
      <State name="Alaska" capital="Juneau" symbol ="AS" population="50"/>
      <State name="Arizona" capital="Phoeniz" symbol ="AZ" population="300"/>
      <State name="California" capital="Sacremento" symbol ="CA" population="600"/>
    </USA>
  </CountriesConfigSection>

This is our actual custom config section. Please make sure that it matches the name in step 1 ( above )
App.config must be like below

















Close your App.config and save it and build your solution to make sure nothing broke.

2) Add System.Configuration to your Windows Forms project. 




















3) Create a new class and name it as CountriesConfigSection.cs and add using System.Configuration at top

4) If you examine our custom config section (<CountriesConfigSection>) in App.config you will notice that we have hierarchical structure. We have collection of countries section (CountriesConfigSection) and in that for each country ( USA ) we have State. So lets start from lowest level ( State ) first. Open CountriesConfigSection.cs and create State class which is a Configuration Element like below which creates properties and matches to respective attribute in <State> Configuration Element in App.config inside our custom config section (<CountriesConfigSection>)

public class State : ConfigurationElement
    {
        [ConfigurationProperty("name", IsRequired = true)]
        public string Name
        {
            get { return (string)this["name"]; }
            set { this["name"] = value; }
        }

        [ConfigurationProperty("capital", DefaultValue = "", IsRequired = true)]
        public string Capital
        {
            get { return (string)this["capital"]; }
            set { this["capital"] = value; }
        }

        [ConfigurationProperty("symbol", DefaultValue = "", IsRequired = true)]
        public string Symbol
        {
            get { return (string)this["Symbol"]; }
            set { this["Symbol"] = value; }
        }

        [ConfigurationProperty("population", DefaultValue = "", IsRequired = true)]
        public string Population
        {
            get { return (string)this["population"]; }
            set { this["population"] = value; }
        }       
    }

After State configuration Element we need to create ConfigurationElementCollection that stores all Configuration Elements. Lets create USAStatesCollection class like below

public class USAStatesCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new State();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((State)element).Name;
        }
    }


Now at last we need to create a class to hold our Custom Configuration Section itself. So lets create CountriesConfigSection like below.
This will have a method (GetConfig) that will read App.config and get our custom config section. This also has a property to hold our ConfigurationElementCollection of type <USA>.

public class CountriesConfigSection : ConfigurationSection
    {
        public static CountriesConfigSection GetConfig()
        {
            return (CountriesConfigSection)System.Configuration.ConfigurationManager.GetSection("CountriesConfigSection") ?? new CountriesConfigSection();
        }

        [System.Configuration.ConfigurationProperty("USA")]
        [ConfigurationCollection(typeof(USAStatesCollection), AddItemName = "State")]
        public USAStatesCollection USAStates
        {
            get
            {
                object o = this["USA"];
                return o as USAStatesCollection;
            }
        }
    }

   
At last our CountriesConfigSection.cs must look like below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace CustomConfigSections
{
    public class CountriesConfigSection : ConfigurationSection
    {
        public static CountriesConfigSection GetConfig()
        {
            return (CountriesConfigSection)System.Configuration.ConfigurationManager.GetSection("CountriesConfigSection") ?? new CountriesConfigSection();
        }

        [System.Configuration.ConfigurationProperty("USA")]
        [ConfigurationCollection(typeof(USAStatesCollection), AddItemName = "State")]
        public USAStatesCollection USAStates
        {
            get
            {
                object o = this["USA"];
                return o as USAStatesCollection;
            }
        }
    }


    public class State : ConfigurationElement
    {
        [ConfigurationProperty("name", IsRequired = true)]
        public string Name
        {
            get { return (string)this["name"]; }
            set { this["name"] = value; }
        }

        [ConfigurationProperty("capital", DefaultValue = "", IsRequired = true)]
        public string Capital
        {
            get { return (string)this["capital"]; }
            set { this["capital"] = value; }
        }

        [ConfigurationProperty("symbol", DefaultValue = "", IsRequired = true)]
        public string Symbol
        {
            get { return (string)this["Symbol"]; }
            set { this["Symbol"] = value; }
        }

        [ConfigurationProperty("population", DefaultValue = "", IsRequired = true)]
        public string Population
        {
            get { return (string)this["population"]; }
            set { this["population"] = value; }
        }       
    }

    public class USAStatesCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new State();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((State)element).Name;
        }
    }
}


Now save all items and build your solutions to make sure you donot have any errors.

5) Thats all we are done setting up to read custom configuration section in App.config. The final step is to access them and use them in our Winforms application. Please see below code which self explanatory. I am trying to get USA States name and display them in Combobox as shown in below screen shot.

 private void Form1_Load(object sender, EventArgs e)
        {
            List<string> usaStatesNames = null;
            CountriesConfigSection config = CountriesConfigSection.GetConfig();
            if (config != null && config.USAStates != null && config.USAStates.Count > 0)
            {
                usaStatesNames = new List<string>();
                foreach(State usaState in config.USAStates)
                {
                   
                    usaStatesNames.Add(usaState.Name);
                }
            }

            cmbStates.DataSource = usaStatesNames;
            cmbStates.SelectedIndex = 0;
        }




















That's it. Build your code and run the app and if everything goes fine you will get something like shown in above image.

Conclusion:
In this article we learned how we can add custom configuration section in our App.config and how to read them and access them in our Windows Forms application.

Please feel free to let me know your valuable and constructive comments and suggestions which can help me to make this article better and improve my technical and written skills. Last but not the least please excuse me for my grammar and typos.

Thanks and have a nice and wonderful day.