WCF Example in C#

In this article we will learn about WCF services with an example.

In this article, we will learn about WCF services with an example.

  • First create a class library and name it TestService. (Execute VisualStudio using "Run as Administrator".)
  • Next add one WCF Service class to the class library.
  • It will create TestService.cs and one interface class ITestService.cs.

Here I am writing a string that we want in the output of the service.

  1. [ServiceContract]  
  2. public interface ITestService  
  3. {  
  4.     [OperationContract]  
  5.     string GetMessage(string Name,string Email,string Address);  
  6. }  
Then we need to change the TestService.cs (because Testservice.cs inherits ITestService.cs).
  1. public class TestService : ITestService  
  2. {  
  3.     public string GetMessage(string Name,string Email,string Address)  
  4.     {  
  5.         return "Hello" + Name + "Email" + Email + "Address" + Address;  
  6.     }  
  7. } 
  • Next we need to add a Console Application to the solution, name it TestServicehost.
  • Next add a reference for System.Servicemodel to the project.
  • Next add the TestService project as a reference.

Next modify the Main method as in the following:

  1. static void Main()  
  2. {  
  3.      using (ServiceHost host = new ServiceHost(typeof(TestService.TestService)))  
  4.      {  
  5.          host.Open();  
  6.          Console.WriteLine("Host strated @" + DateTime.Now.ToString());  
  7.          Console.ReadLine();  
  8.     }  
  9. }  
Next add an Application Configuration file to the project. Provide the following code for it:
  1. <system.serviceModel>  
  2.     <services>  
  3.       <service  name="TestService.TestService" behaviorConfiguration="mexBehavior">  
  4.         <endpoint address="HelloService" binding="basicHttpBinding" contract="TestService.ITestService">  
  5.         </endpoint>  
  6.         <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange">  
  7.         </endpoint>  
  8.         <host>  
  9.           <baseAddresses>  
  10.             <add baseAddress ="http://localhost:8080/"/>  
  11.           </baseAddresses>  
  12.         </host>  
  13.       </service>  
  14.     </services>  
  15.     <behaviors>  
  16.       <serviceBehaviors>  
  17.         <behavior name="mexBehavior">  
  18.           <serviceMetadata httpGetEnabled="true"/>  
  19.         </behavior>  
  20.       </serviceBehaviors>  
  21.     </behaviors>  
  22. </system.serviceModel>  
Next, set TestServiceHost as the start up project. Then a command prompt window will open.
command prompt window 
  • Next open Visual Studio and again use "Run as administrator".
  • Create New->Project->Web->ASP.NET Empty Web application.
  • Add System.Servicemodel as a Refernce to project.
  • Add a service refernce to the project, it will open a window.

service Refernce to project 

In the address bar, enter the URL (as you gave in the app.config). Click on go, it will show the running webservice.

Click on the web service and provide a name and then click OK.
  • Next add a Webform to the project and name it Student.aspx.
  • Enter the following code.

  1. <form id="form1" runat="server">  
  2.     <table>  
  3.         <tr>  
  4.             <td>  
  5.                 <asp:Label ID="lblName" runat="server" Text="Name"></asp:Label>  
  6.             </td>  
  7.             <td>  
  8.                 <asp:TextBox ID="txtName" runat="server"></asp:TextBox>  
  9.             </td>  
  10.         </tr>  
  11.         <tr>  
  12.             <td>  
  13.                 <asp:Label ID="lblMail" runat="server" Text="Email"></asp:Label>  
  14.             </td>  
  15.             <td>  
  16.                 <asp:TextBox ID="txtMail" runat="server"></asp:TextBox>  
  17.             </td>  
  18.         </tr>  
  19.         <tr>  
  20.             <td>  
  21.                 <asp:Label ID="lblAddress" runat="server" Text="Address"></asp:Label>  
  22.             </td>  
  23.             <td>  
  24.                 <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>  
  25.             </td>  
  26.         </tr>  
  27.         <tr>  
  28.             <td colspan="2">  
  29.                 <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />  
  30.             </td>  
  31.         </tr>  
  32.         <tr>  
  33.             <td>  
  34.                 <asp:Label ID="lblMsg" runat="server"></asp:Label>  
  35.             </td>  
  36.         </tr>  
  37.     </table>  
  38. </form>  

In the code behind file, in the click event enter the following code.

  1. protected void btnSubmit_Click(object sender, EventArgs e)  
  2. {  
  3.      TestService.TestServiceClient client = new TestService.TestServiceClient("BasicHttpBinding_ITestService");  
  4.      string Name=txtName.Text;  
  5.      string Email=txtMail.Text;  
  6.      string Address=txtAddress.Text;  
  7.      lblMsg.Text = client.GetMessage(Name, Email, Address);  
  8. }  
Next, run the project; we will get the result.