Apr 22 2009

WCF client and WCF service

Published by at 8:06 pm under WCF & Java Interop

This article is a part of WCF & Java Interop

Download source code

 

Introduction

To begin with, we are going to create a very simple WCF service and WCF client in C#/VB.NET first. After this article, we will have a working WCF application. Then in the next article, we will create a Java client to connect to the WCF service created in this article.

 

Creating a WCF service

Using Visual Studio 2008, we can easily create Windows Communication Foundation (WCF) service and client. This topic describes how to create a basic WCF service and a client that can call the service.

To create a basic WCF service, please follow the steps below:

1. Open Visual Studio 2008.

2. Create a new WCF Service Application project. In Visual Studio 2008, click menu File -> New -> Project. In the New Project dialog, expand Visual Basic or Visual C# and choose the WCF Service Application template to create a project named “WcfService1″. Click OK.

 

Now, we have created a basic WCF service application. In Solution Explorer, we can see four files in the “WcfService1″ project.

 

File

Description

IService1.cs

IService1 interface, a WCF service contract

Service1.svc

A service file, accessed by the client as a part of URL

Service1.svc.cs

A class, implements the IService1interface

Web.config

Service configuration file

 

In the “Service1.svc.cs” file, we can find a class named “Service” which includes two functions, “GetData” and “GetDataUsingDataContract”.

You can create a new service or add more functions to the existing service. I will talk about these in the future. Now we can run the server in the Debug mode. The ASP.NET Deployment Server will start and the service will be deployed to the server:

 

Creating a WCF client

To create a WCF client that calls the service, please follow the steps below:

1. Open Visual Studio 2008.

2. Create a new Console Application project. In Visual Studio 2008, click menu File -> New -> Project. In the New Project dialog, expand Visual Basic or Visual C# and choose the Console Application template to create a project named “WcfClient1″. Click OK.

 

3. Add a service reference to the project. Go to Solution Explorer, right-click the “References” node in the “WcfClient1″ project, and click Add Service Reference.

 

4. In the Add Service Reference dialog, input the server URL above in Address. Click Go and then click OK.

 

5. We can find a node named “ServiceReference1″ under Service References.

 

6. Add a using statement (Imports in Visual Basic) for the System.ServiceModel namespace in the generated “Program.cs” file.

using System.ServiceModel;

7. Add the following code into the Main function in the “Program.cs” file:

Service1Client client = new Service1Client();
String strResponse = client.GetData(100);
Console.WriteLine(strResponse);
Console.ReadLine();
client.Close();

8. Then we can run the client application in the Debug mode. If we see the following result, our WCF application is working properly.

 

Links:
Previous article >>>>: WCF and Java Interop Series introduction
Next article >>>>: Java client and WCF server
WCF & Java Interop series home page: WCF & Java Interop


25 Responses to “WCF client and WCF service”

  1. Anaristoson 02 May 2009 at 9:09 pm

    I was very excited to see this work. It must say, though, that bouncing to the localhost and back is slow!

  2. Jignesh Patelon 02 Dec 2009 at 1:36 am

    Thanks

    Its very useful for create sample application…

  3. Javieron 04 Feb 2010 at 1:56 pm

    Thanks I have problem with my #C WCF Client if I want to send datacontracts like parameters.

    Any idea.

    Thanks

  4. Maheshon 10 Aug 2010 at 4:39 am

    Thanks sir it helps me.

  5. emirhanon 11 Aug 2010 at 4:12 am

    ı want to create clint -wcf but client .net 2.0-wcf.net3.5
    how can ı ?

  6. Tharakaon 16 Sep 2010 at 1:24 am

    Thanks;

    I was confused how to create a WCF application , This example gives me a clear guidelines to create a simple WCF.thanks again.

  7. Alyonaon 01 Nov 2010 at 3:24 am

    Thank you a lot. I couldn’t find a way to do this kind of work. I didn’t think that it can be as easy.

  8. Arvindon 02 Dec 2010 at 10:26 pm

    At the Beginning i was confused of creatng WCF service and client application.But now looking at this site i made it with an ease .Thanks a lot for this stuff for beginners.

  9. Nupur Bakshion 21 Dec 2010 at 12:02 am

    Hello Sir,

    I am developing a project in WCF in which I am using WCF Message Based Security with UserNamePassword Client credentials for authenticating the Client.I have also created X509 certificates and provide access to Claims using Claims Based Authorization.Everything is working fine as When I initially call the method of the service it asks for the authentication of the client and after authenticating the Client it provides access to the method which is accurately done but when I call another method from the same service it again asks for authentication which is creating a problem as I don’t want to authenticate the Client again and again on every method call of the same service.Is there any way to retain the Client Credentials so that I would not have to re-authenticate the Client on every method call.Please help me in this.This is my first project in WCF so please provide me the step by step guidance on how to do it.I will be thankful to you.

  10. nusjaon 17 Mar 2011 at 1:56 am

    article was very good but i have one error in my code.
    ServiceHost client = new ServiceHost();
    this ServiceHost is not available .
    i did as you told( using System.ServiceModel)
    can help me

  11. Sjiht77on 17 Mar 2011 at 3:55 am

    nusja,

    Try this; works for me!

    ServiceReference1.IService1 client = new ServiceReference1.Service1Client();
    String strResponse = client.GetData(20025115);
    Console.WriteLine(strResponse);
    Console.ReadLine();

  12. kshukomon 21 Mar 2011 at 10:15 pm

    Hi nusja,

    try to resolve ServiceHost (right-click then resolve)

    or add this line below (using System.ServiceModel)
    .
    .
    .
    using WcfClient1.ServiceReference1;

    Regards

  13. myroon 06 May 2011 at 7:24 am

    simple, clear and perfect!
    thx!

  14. UniQueon 20 Jul 2011 at 12:38 am

    is it possible to get list data from javascript?

  15. leelaon 20 Sep 2011 at 4:04 am

    Its helps me alot….

    Thank you

  16. chanchalon 19 Oct 2011 at 7:18 am

    great to learn WCF.

    keep posting

  17. Webinfosyson 16 Jan 2012 at 2:35 pm

    Very easiest and clear way, Very beneficial to first user….I appriciate your great work.

  18. Thirupathion 26 Jan 2012 at 11:57 pm

    It is very useful this tutorial
    Can u plz explain sample project on wcf?

  19. Dev Guyon 29 Jan 2012 at 5:45 am

    Hi I’m looking for an example in VB.NET, can you help?

  20. Jose Luison 10 Feb 2012 at 3:52 pm

    Dev Guy: Here is a VB client sample:

    Imports System.ServiceModel
    Imports WCFClient1.ServiceReference1

    Module Module1

    Sub Main()

    Dim client As Service1Client = New Service1Client()
    Dim strResponse As String = client.GetData(100)
    Console.WriteLine(strResponse)
    Console.ReadLine()
    client.Close()

    End Sub

    End Module

  21. Ravi Sharmaon 29 Feb 2012 at 7:31 am

    Hi,

    I was reading your article and I would like to appreciate you for making it very simple and understandable. This article gives me a basic idea of First WCF Service and it helped me lot. I have found another posts over the internet which also explained very well. you may check that post…
    http://www.mindstick.com/Articles/a0e46f74-f76e-4804-a3ed-ccd0738c0d36/?Creating%20%E2%80%9CHello%20World%E2%80%9D%20WCF%20Application
    and

    http://www.displacedguy.com/tech/creating-a-wcf-service-using-powerbuilder-12-5/

    Thank you very much!

  22. hungbkhon 10 Mar 2012 at 1:09 am

    my application can’t find service
    http://localhost:52390/Service1.svc

  23. hungbkhon 10 Mar 2012 at 5:53 am

    when i add service:
    http://localhost:2871/Service1.svc
    error:
    There was an error downloading ‘http://localhost:2871/Service1.svc'.
    Unable to connect to the remote server
    No connection could be made because the target machine actively refused it 127.0.0.1:2871
    Metadata contains a reference that cannot be resolved: ‘http://localhost:2871/Service1.svc'.
    There was no endpoint listening at http://localhost:2871/Service1.svc that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
    Unable to connect to the remote server
    No connection could be made because the target machine actively refused it 127.0.0.1:2871
    If the service is defined in the current solution, try building the solution and adding the service reference again.

  24. salmaon 03 Apr 2012 at 2:55 am

    thank you so much this process helped me alot……..thank you 1′s again

  25. Abhijit Baruaon 11 Apr 2012 at 11:55 pm

    I learn First step from your article. Thank You.