WCF Example in C#

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.

[ServiceContract]

    public interface ITestService

    {

        [OperationContract]

        string GetMessage(string Name,string Email,string Address);

    }

Then we need to change the TestService.cs (because Testservice.cs inherits ITestService.cs).

public class TestService : ITestService

{

    public string GetMessage(string Name,string Email,string Address)

    {

        return "Hello" + Name + "Email" + Email + "Address" + Address;

    }

}

  • 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 refernce.

Next modify the Main method as in the following:

static void Main()

{

     using (ServiceHost host = new ServiceHost(typeof(TestService.TestService)))

     {

            host.Open();

            Console.WriteLine("Host strated @" + DateTime.Now.ToString());

           Console.ReadLine();

    }

 }

Next add an Application Configuration file to the project. Provide the following code for it:

<system.serviceModel>

    <services>

      <service  name="TestService.TestService" behaviorConfiguration="mexBehavior">

        <endpoint address="HelloService" binding="basicHttpBinding" contract="TestService.ITestService">

        </endpoint>

        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange">

        </endpoint>

        <host>

          <baseAddresses>

            <add baseAddress ="http://localhost:8080/"/>

          </baseAddresses>

        </host>

      </service>

    </services>

    <behaviors>

      <serviceBehaviors>

        <behavior name="mexBehavior">

          <serviceMetadata httpGetEnabled="true"/>

        </behavior>

      </serviceBehaviors>

    </behaviors>

  </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.

<form id="form1" runat="server">

    <table>

        <tr>

            <td>

                <asp:Label ID="lblName" runat="server" Text="Name"></asp:Label>

            </td>

            <td>

                <asp:TextBox ID="txtName" runat="server"></asp:TextBox>

            </td>

        </tr>

        <tr>

            <td>

                <asp:Label ID="lblMail" runat="server" Text="Email"></asp:Label>

            </td>

            <td>

                <asp:TextBox ID="txtMail" runat="server"></asp:TextBox>

            </td>

        </tr>

        <tr>

            <td>

                <asp:Label ID="lblAddress" runat="server" Text="Address"></asp:Label>

            </td>

            <td>

                <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>

            </td>

        </tr>

        <tr>

            <td colspan="2">

                <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />

            </td>

        </tr>

        <tr>

            <td>

                <asp:Label ID="lblMsg" runat="server"></asp:Label>

            </td>

        </tr>

    </table>

</form>

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

protected void btnSubmit_Click(object sender, EventArgs e)

 {

     TestService.TestServiceClient client = new TestService.TestServiceClient("BasicHttpBinding_ITestService");

     string Name=txtName.Text;

     string Email=txtMail.Text;

     string Address=txtAddress.Text;

     lblMsg.Text = client.GetMessage(Name, Email, Address);

}

Next, run the project; we will get the result.

Up Next
    Ebook Download
    View all
    Learn
    View all