Simplified Config File in WCF 4.5

Introduction

Today, in this article let's play around with one of the interesting and most useful concepts in WCF 4.5.

Question: What is simplified configuration?

In simple terms "It enables the config file to reduce the complexity and maximizes the usage power. Adding a service reference only generates the necessary config".

Step 1: Create a new WCF 4.5 project.

 

Output.jpg
 

Step 2: The complete code of IService1.cs looks like this:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.ServiceModel.Web;

using System.Text;

namespace WcfService1

{

    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.

    [ServiceContract]

    public interface IService1

    {

        [OperationContract]double Add(double a, double b);

        [OperationContract]double Sub(double a, double b);

    }

}

 

Step 3: The complete code of Service1.svc.cs looks like this:
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.ServiceModel.Web;

using System.Text;

namespace WcfService1

{

    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.

    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.

    public class Service1 : IService1{public double Add(double a, double b)

    {

        return a + b;

    }

        public double Sub(double a, double b)

        {

            return a - b;

        }

    }

}

 

Step 4: The complete code of web.config file looks like this (Simplified Configuration):

 

<?xml version="1.0"?>

<configuration>

  <system.serviceModel>

    <bindings>

      <basicHttpBinding>

        <binding name="BasicHttpBinding_IService1"/>

      </basicHttpBinding>

    </bindings>

    <client>

      <endpoint address="http://localhost:7395/Service1.svc" binding="basicHttpBinding" contract="WCFApp.IService1" bindingConfiguration="BasicHttpBinding_IService1" name="BasicHttpBinding_IService1"/>

    </client>

  </system.serviceModel>

  <system.web>

    <compilation debug="true"/>

  </system.web>

</configuration>

 

Step 5: Create a new webforms project and add the service reference of the new WCF project; see:

 

Output2.jpg
 

Step 6: The complete code of WebForm1.aspx looks like this:

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="SimplifiedConfigurationApp.WebForm1" %>

 

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

    <title></title>

</head>

<body>

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

    <center>

        <div>

            <table>

                <tr>

                    <td colspan="2">

                        <asp:Label ID="Label1" runat="server" Text="Arthmetic Operations - WCF 4.5 Simplified Configuration"

                            Font-Bold="true" Font-Size="Large" Font-Names="Verdana" ForeColor="Maroon"></asp:Label>

                    </td>

                </tr>

                <tr>

                    <td>

                        <asp:Label ID="Label3" runat="server" Text="Please Enter First Number" Font-Size="Large"

                            Font-Names="Verdana" Font-Italic="true"></asp:Label>

                    </td>

                    <td>

                        <asp:TextBox ID="TextBox2" runat="server" Width="120px"></asp:TextBox>

                    </td>

                </tr>

                <tr>

                    <td>

                        <asp:Label ID="Label2" runat="server" Text="Please Enter Second Number" Font-Size="Large"

                            Font-Names="Verdana" Font-Italic="true"></asp:Label>

                    </td>

                    <td>

                        <asp:TextBox ID="TextBox1" runat="server" Width="120px"></asp:TextBox>

                    </td>

                </tr>

                <tr>

                    <td colspan="2" style="text-align: center">

                        <asp:Button ID="Button1" runat="server" Text="Addition" Font-Names="Verdana" Width="213px"

                            BackColor="Orange" Font-Bold="True" OnClick="Button1_Click" />

                    </td>

                </tr>

                <tr>

                    <td colspan="2" style="text-align: center">

                        <asp:Button ID="Button2" runat="server" Text="Substraction" Font-Names="Verdana"

                            Width="213px" BackColor="Orange" Font-Bold="True" OnClick="Button2_Click" />

                    </td>

                </tr>

                <tr>

                    <td colspan="2" style="top: 50px; text-align: center">

                        <asp:Label ID="Label5" runat="server" Font-Bold="true" Font-Names="Verdana" ForeColor="Maroon"></asp:Label>

                    </td>

                </tr>

            </table>

        </div>

    </center>

    </form>

</body>

</html>

 

Step 7: The complete code of WebForm1.aspx.cs looks like this:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using SimplifiedConfigurationApp.ServiceReference1;namespace SimplifiedConfigurationApp

{

    public partial class WebForm1 : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            TextBox2.Focus();

        }

        protected void Button1_Click(object sender, EventArgs e)

        {

            double val1 = double.Parse(TextBox2.Text);

            double val2 = double.Parse(TextBox1.Text);

            if (string.IsNullOrEmpty(TextBox1.Text) || string.IsNullOrEmpty(TextBox2.Text))

            {

                Label5.Text = "Please Enter Some Values";

                Label5.ForeColor = System.Drawing.Color.Red;

            }

            else

            {

                Label5.Text = "The Addition of " + TextBox1.Text + " and " + TextBox2.Text + " is " + objClient.Add(val1, val2);

                Label5.ForeColor = System.Drawing.Color.Green;TextBox1.Text = string.Empty;

                TextBox2.Text = string.Empty;

            }

        }

        protected void Button2_Click(object sender, EventArgs e)

        {

            double val1 = double.Parse(TextBox2.Text);

            double val2 = double.Parse(TextBox1.Text);

            if (string.IsNullOrEmpty(TextBox1.Text) || string.IsNullOrEmpty(TextBox2.Text))

            {

                Label5.Text = "Please Enter Some Values";

                Label5.ForeColor = System.Drawing.Color.Red;

            }

            else

            {

                Label5.Text = "The Substraction of " + TextBox1.Text + " and " + TextBox2.Text + " is " + objClient.Sub(val1, val2);

                Label5.ForeColor = System.Drawing.Color.Green;

                TextBox1.Text = string.Empty;

                TextBox2.Text = string.Empty;

            }

        }

        #regionService1Client objClient = new Service1Client();

        #endregion}

}
 

Step 8: The output of application looks like this:

 

Output3.png
 

 

Step 9: The addition operation output of the application looks like this:

 

Output4.png

I hope this article is useful for you.


Similar Articles
MVC Corporation
MVC Corporation is consulting and IT services based company.