Apr 22 2009
WCF client and WCF service
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”
I was very excited to see this work. It must say, though, that bouncing to the localhost and back is slow!
Thanks
Its very useful for create sample application…
Thanks I have problem with my #C WCF Client if I want to send datacontracts like parameters.
Any idea.
Thanks
Thanks sir it helps me.
ı want to create clint -wcf but client .net 2.0-wcf.net3.5
how can ı ?
Thanks;
I was confused how to create a WCF application , This example gives me a clear guidelines to create a simple WCF.thanks again.
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.
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.
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.
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
nusja,
Try this; works for me!
ServiceReference1.IService1 client = new ServiceReference1.Service1Client();
String strResponse = client.GetData(20025115);
Console.WriteLine(strResponse);
Console.ReadLine();
Hi nusja,
try to resolve ServiceHost (right-click then resolve)
or add this line below (using System.ServiceModel)
.
.
.
using WcfClient1.ServiceReference1;
Regards
simple, clear and perfect!
thx!
is it possible to get list data from javascript?
Its helps me alot….
Thank you
great to learn WCF.
keep posting
Very easiest and clear way, Very beneficial to first user….I appriciate your great work.
It is very useful this tutorial
Can u plz explain sample project on wcf?
Hi I’m looking for an example in VB.NET, can you help?
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
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!
my application can’t find service
http://localhost:52390/Service1.svc
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.
thank you so much this process helped me alot……..thank you 1′s again
I learn First step from your article. Thank You.