Create WCF Service Application
1. Open Microsoft Visual Studio
2. File->New Project->Type project name as 'ServiceLayer'
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhq7Dn318ywhG3itDJ-MlWwW_iJT3-hPqmVZa2-qvnsn20R0M0cCKcg-1gz-uIBdmd1rDvZkZ-v3_wEEjD3yEOR1J2fwPh_kFDnP7IeuyWzqnchZD2PWFOf_xPJFv4cXwFQbCn-x9BudbXp/s320/1.jpg)
3. Now you can see your solution explorer. It will look like bellow
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjEAvr9Lw9v3JE6EYBv7awk4nkYPIAKA4tMnGY4jYUKAUnuxTsBg6piN9Xlw3b5DdVfhCT63PlythDfUTEyQOQDId1s5B4tGPO3XQXtI_teb9GfWVbV7g-8swKouCbTsk-ROq9F18xE-M5N/s320/2.jpg)
4. Now delete IService1.cs and Service1.svc
5. Add a new item WCF Service with name 'HelloWorldService'
Now youe solution explorer will look like bellow
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYOOr4ks9l3G09Ags0nBYR9mGJ5HBoNetM7F9SPdh8qn8wX2WMvQQV3kBwijTgh1pZ0KZGdtgBfgbLzVyywZbAUamhE7Mm-hG1AjL4NuNXYgtBKj9Z9i6HyN3XaC6f-k7y72i9TeNevm62/s320/3.jpg)
6. Double click on and enter the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace ServiceLayer
{
[ServiceContract]
public interface IHelloWorldService
{
[OperationContract]
string SayHello(string name);
}
}
This interface defines exactly what type of work our service will be doing, and will quite literally form the definition of our service. We will refer directly to the IHelloWorld interface from our configuration file as specifying the contract of what our service will do. The only difference between this code and another .net interface is that parts of it are decorated with WCF attributes. The first attribute (“ServiceContract”) states that this interface will form the contract for a WCF service, while the second one (“OperationContract”) defines the method as an operation that the service will offer and perform. One ServiceContract can contain multiple OperationContracts and these will be translated by the framework into the appropriate wsdl tags, but all of this is entirely transparent to the developer.
Next comes our class – the implementation of our interface. This class appears below, and you should take note of two important aspects, which we will discuss below.
7. Go to the HelloWorldService.svc.cs and change the code like bellow
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace ServiceLayer
{
public class HelloWorldService : IHelloWorldService
{
public string SayHello(string name)
{
return "Hello, " + name;
}
}
}
Now your done and all set to run the service. Rebuild the solution.
Our next step is to to run our service and see how it appears in the browser. All being well, we should see something like the screenshot below:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhkwLNkZf3D22G8574ff-M62MCWH_f8LM6-yXl2zj_Es6RpXBFNq2mM_L6uwFcqsm8jDemjr2HAfvfE8-uEdFHLyb-7ToiWqwLa7AaGPRkti0ly0qFBKI_FYEF7wm5kG5A2I7Fbxo_jNb51/s320/4.jpg)
Create A Client Application
In order for our service to have any value (well, as valuable as a “Hello World” service can be!), it must have something that consumes it, so we’ll now go about creating our client. Add a new console application
8. Add a new project (Type Console Application) called TestClient
9. Right click on TestClient project and click Add Service Reference
10. Click on Discover
11. HelloWorldService.svc will display inServices list
12. Expand the node in the services list
13. Select the HellowWorldService and change the namespace 'MyService' and click on OK
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjeNxRZGa6Fe2ccWm3bMzKsrkaypA4UP4gevHSOss7r1CUTl9VZMAP9tJToKI9wp5fZuhBUi_SXVJIEoCFz6Yjd4Vu6KJtdSJtzXhF82NDO90iXUMlMj2CBn8ymD03TeXL_e_n7Wzilkutr/s320/5.jpg)
14. Double click on Program.cs and place the folowing code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestClient
{
class Program
{
static void Main(string[] args)
{
MyService.HelloWorldServiceClient client = new MyService.HelloWorldServiceClient();
// Use the 'client' variable to call operations on the service.
Console.WriteLine(client.SayHello("RUDRA"));
// Always close the client.
client.Close();
Console.ReadLine();
}
}
}
Now rebuild the solution and change the TestClient as start up project and press F5
You will see the bellow:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhJjh-eqfXpyMBbxye1F7NXClce3M-VQnCWF0NDiRtcf6QcZPMpFvtDFhmCBQ1pvfQyJIfLaEU_3-EHiyKA8B_xwRyo_043Ny2UQmv1VadDqS2qxedtsw18E3-vaSuXGsHjK2-_MXTDhnqg/s320/6.jpg)
Enjoy...
No comments:
Post a Comment