Simple WCF Walkthrough

First open Microsoft Visual Studio 2012 with Administrative rights. The go to "File" -> "New" -> "Project".

WCF1.jpg

Select the "WCF Template" and then click on "WCF Service Library".

Set the name field to "WcfServiceConfiguration" and framework to ".NET 4.5" and save it your preferred location.

WCF2.jpg

Rename the "IService.cs" to "IA.cs" and "Service.cs" to "A.cs".

Replace the existing code of IA.cs with the code shown in the following image.

WCF3.jpg

Now replace the existing code of A.cs with the code shown in the image below

WCF4.jpg

Now build this service to check that it does not contain any errors.

In the example I will declare the baseAddress, endpoints etcetera programmatically so you can delete the tags from the App.Config file.

Now I will create a Host Application to host this service.

Right-click the existing solution and select "Add" -> "New Project" then select Console Application, name this project "Host".

Add a reference to the System.ServiceModel dll and WcfServiceConfiguration dll that we have just built.

Now replace the existing code with the following code shown in the image below:

WCF5.jpg

Set this Host Project as your startup project (right-click the host project and set as setup project) and press the F5 Key to run the host. If everything is fine you will see this command prompt:

WCF6.jpg

Now we can consume this service in two ways.

First

Using ChannelFactory

Add a new project to this existing solution and name it "ConsumerWithChannelFactory".

Add a reference to the System.ServiceModel dll and WcfServiceConfiguration dll, replace the code of the existing Program.cs class with the following code shown in the image below.

WCF7.jpg

Run the Host Project and then run the ConsumerWithChannelFactory project to see the output like in the following image.

WCF8.jpg

The second way to consume this service is by using the ServiceModel Metadata Utility (svcutil.exe) to generate a proxy and a configuration file.

Add a new project to the WcfServiceConfiguration solution and name it ConsumerWithSvcuUtil.

Run the Visual Studio developer command prompt with administrative rights, run the host process first and then type the following command on the prompt to generate a proxy and a configuration file.

After the generation of the files add them to the ConsumerWithSvcuUtil Project by clicking the project and select "Add" -> "Existing Item".

WCF9.jpg

Now type in the following code in the Program.cs file of the ConsumerWithSvcuUtil project as shown in the following image:

WCF10.jpg

Now run the Host; if its not already running then run the ConsumerWithSvcuUtil, if everything is "Ok" then it will look as in the following:

WCF11.jpg

Feel free to tell if I have missed something or many things and I hope this tutorial works fine for you.
 


Similar Articles