Thursday, March 26, 2015

WCF Service Demo with Channel Factory on Client Side

In this video series we are going to see how to make a WCF service with channel factory on client side:

This is two part series.

In part one we make service and host it using console host.



Please like and share the video with your friends.


In part two we make client with channelfactory class object and create channel on our own:



Following are steps to do the same:

In today's video we are going to see how to create, host and consume WCF service using channel factory.

The definition is as following:

We are going to create a service where user enters ID of a student and based on our records we will find out if the perticular student
is placed or not and return status of student (placed or not placed) to user.

To do this first we will create a WCF Service Library like we have seen in earlier practicals.

The structure of files in solution explorer is familiar to you. There is one ServiceContract which is an interface and under that there are serveral OperationContracts mentioned. This file's interface is implemented in another file "service1.cs".

Now lets first decide strategy on how to approch the problem.

First we will decide input and output so we can decide what OperationContract do we want to make.

We want to get details of placement status for perticular student.

So lets name our OperationContract as getPlacement().

But we want detail of perticular student only. So lets modify above OperationContract.

This is modified OperationContract: Public String getPlacement(String StudID)

This method we will declare in our interface and will be implemented where we implement our interface. That is inside our service class.

Since we need only one OperationContract and all we are going to use are built in types and no complex types so we won't be needing data contract as well so we will remove it as well.

Now we will write implementation of this method.

The code in service1.cs file is old and is not relevent anymore so we will remove whole code.

Lets name our interface and class properly so we know what they are for.

Now right click and select "Implement Interface" option to implement this interface automatically.

We have not written any logic because we don't know what we are suppose to do. We only know input and output so we have mentioned that.

Now we have to decide about flow of data.

Generally data about student placement would be stored in datbase but to reduce complexity we will store data locally.

To do this we will make a class "student" and members "studID" and "Status".

Now this class student can be declared anywhere in this solution. You can write it in Service1.cs file or Iservice1.cs or make separate file for the same.

We will write it in service1.cs file.

Now what we are going to do is we will store data of student at beginning of program by making objects of student class and storing data into them.

This process can be static or hardcoded or you can get input from user too.

In this program we are going to store data by writing hard code.

To do this we will create object of type student. But as you all know in order to create object of any class first constructor of class is called. so can we assign and store values in constructor only? If it can be done.

So lets first make constructor of student class.

Lets make constructor public as without that they are of no use.

Now we have to create an object of student so for that we will create a function "insertStudData" in service class.

Let's rename service class to more appropriate name "Placement".

Now to make this data accessible in other functions of class, we will create a list of students so that data about students can be grouped.

Now we will add both student members in list. This we will do using add() method of list.

We will call this insertStudData() function at the beginning of getData().

Now we have student data ready for user. So lets write logic for checking if student whose ID is entered is placed or not.

For this we will take each object of student and compare its studID with studID entered by user, when studID matches we will fetch status for that student and return that status.

Now the problem with this method is that both studID and status members of student class are private and can not be accessed outside of class.

So we will have to make functions that can return us data of student object which is calling it.

Now we have to call our InsertStudData() function inorder to insert the data of student and have it in list.

We have to decide where to call this function.

We know that inorder to call this getStatus() function we first have to make object of Placement class and using object we can call the getStatus() function.

When object gets created for class the constructor of class gets called. So now we want to enter details of student when the object of Placement gets created.

Now for the main logic of comparision we will run a foreach loop and fetch one object of student from list at a time and fetch its studID to compare it with our studID provided to us by user.

If it matches then we will fetch status and return status.

Now lets run our service and see if it is working as expected or not.

After that we will make host and client of our service.

It is not showing service to us in client as we are yet to change name of ServiceContract and service class in .Config file.

Now that our service is ready and working, it is time to make host which will host our service. Host is usually a consol application without any interface. Which once started should continue to execute unless specifically shut down.

To create host we will create a new project in existing solution.

You can note that this host has Main() function unlike WCF service which did not had Main(). So this host can be executed indipendently.

To host a WCF service we first need to create an object of ServiceHost class and initialize that object with our service class.

Then we can use Open() method of this class (ServiceHost) to start service.

The namespace in which ServiceHost class resides is not available by default in this application. So we will have to add it using "Add Reference Option". The namespace in which ServiceHost class resides is "System.ServiceModel".

Now that System.serviceModel class reference is added we can create object of the class once we specify namespace.

It is not able to find our service class (Placement) as it is not residing in same namespace as our host. In this case we need to give reference to the library file (.DLL) that has been generated of our service.

You may have noted that error in "Placement" keyword is not there anymore as it has now found reference.

All that remains to be done is open the host and its ready to be hosted.

Lets set this project as startup project.

And then execute and see if service is successfully hosted or not.

As you can see from highlighted text, there is no configuration file found for this project which is causing this error.

So we now have to create the configuration file.

To create configuration file, you can go to Tools-->WCF Configuration File Editor option.

Select new file from file  menu.

Select "Create a New Service" option from right hand panel.

Now that wizard has opened up we have to first give reference to DLL file which is created by our service.

Here it has taken the contract automatically.

Select HTTP communication mode.

Select Basic Interoperability mode.

In address for endpoint, you can give any address with localhost as computer and userdefined port with service path.

As with all files, save the file on location of your project.

Keep the filename as App.Config.

Click on show all files option in solution explorer to show files which are not visible by deafult.

Right click on App.config file and click on "Include in Project".

This error is regarding access rights so you have to run this .exe file as administrator.

For this go to location of file, and right click and select "Run as Administrator" option.

As you can see now our service is hosted and is ready to be consumed.

When you create client remember that for client to consume service, the service must be ready and hosted. If its not hosted it can not be accessed by client and consumed.

Next step is to create client that will consume this service.


Thursday, February 26, 2015

Working with ASP.NET Table Control

Dear All,

Today we are going to see a video tutorial on working with ASP.NET Table control. This control unlike HTML tag is server side control and provides modification in table structure at runtime.


Working with TextBox control of ASP.NET

Dear All,

Today we are going to learn about TextBox control in ASP.NET. Textbox control is built in control and is used for getting input from user. It can be single line of input or multiple lines of input. It can be simple text or password text. Textbox control does all of it..

Working with ASP.NET TreeView Control

Dear All,

In this video we are going to learn how to work with TreeView Control built in ASP.NET. Tree view control is navigational control and a good alternative to menu control.


Working with ASP.NET Validation Controls

Dear All,

Today we will see how to work with validation controls in ASP.NET. These controls are provided built in by .NET framework and are good alternative to JavaScript based validation.



Please like and share with your friends.

Tuesday, January 13, 2015

Working with ASP.NET label and literal control

Today we show tutorial on how to work with ASP.NET's Label and Literal controls.



Label and Literal controls of ASP.NET are used to display text on web page.


  • Literal Control:

The literal control can only be used to display text that doesn't require any special formatting. Literal control does not support any of formatting that Label supports. This is just for very simple use.

Literal Properties:

  • Mode: Mode Property has 3 Values. These values have an effect on how text is displayed. 
    • Transform: If you add HTML tags in text field, it will transform text based on HTML tag.
    • PassThrough:It will pass all the tags and formatting through. 
    • Encode: This will ignore all HTML tags, and display them as text and not format text as per HTML tags.


Label Control:

Label control allows text to be formatted.  Label control renders its control in span tag. Hence, you can use Label tag as HTML tag, so you can place the content inside its tag rather then use text property.

You can use property window to change some properties of controls such as BackColor, ToolTip, BorderColor, Fonts, etc..

Label Properties: ID, Text



Please like, subscribe and share.

Working with Listbox control of ASP.NET

Today we see how to work with ASP.NET's Listbox Control in following video tutorial.




Listbox is collection of ListItem type of objects.

Items can be added to Listbox in HTML source or in some case ListBox can be bound to a database table or an XML file.

Several useful properties of ListBox control:
1.Rows
2.SelectionMode : Single or Multiple selection
 3. Selected: True or False
 4. DataTextField - Used when bounded with data
 5. DataValueField - Used when bounded with data

Please share, subscribe and like.
IndiBlogger - The Indian Blogger Community