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 channel factory caching? In simple terms "It enables caching of channel factories when you consume the service on a client. A new CacheSetting property has been introduced in WCF 4.5 which provides flexibility to set it up". Step 1: Create a new WCF 4.5 project. 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 ChannelFactoryCachingApp { // 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); [OperationContract] double Mul(double a, double b); [OperationContract] double Div(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; using WcfService1; namespace ChannelFactoryCachingApp { // 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; } public double Mul(double a, double b) { return a * b; } public double Div(double a, double b) { return a / b; } } } Step 4: The complete code of web.config file looks like this: <?xml version="1.0"?> <configuration> <appSettings> <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> </appSettings> <system.web> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5"/> </system.web> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior> <!-- To avoid disclosing metadata information, set the values below to false before deployment --> <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/> <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> </behaviors> <protocolMapping> <add binding="basicHttpsBinding" scheme="https" /> </protocolMapping> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> <!--To browse web app root directory during debugging, set the value below to true.Set to false before deployment to avoid disclosing web app folder information.--> <directoryBrowse enabled="true"/> </system.webServer> </configuration>Step 5: Create an "ASP.NET Web Forms Application" project and add the service reference of the new WCF project; see: Step 6: The complete code of WebForm1.aspx looks like this: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ChannelCachingApp.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 Channel Factory and Caching" 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="text-align: center"> <asp:Button ID="Button3" runat="server" Text="Multiplication" Font-Names="Verdana" Width="213px" BackColor="Orange" Font-Bold="True" OnClick="Button3_Click" /> </td> </tr> <tr> <td colspan="2" style="text-align: center"> <asp:Button ID="Button4" runat="server" Text="Division" Font-Names="Verdana" Width="213px" BackColor="Orange" Font-Bold="True" OnClick="Button4_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 ChannelCachingApp.ServiceReference1; using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace ChannelCachingApp { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e){} protected void Button1_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(TextBox1.Text) || string.IsNullOrEmpty(TextBox2.Text)) { Label5.Text = "Please Enter Some Values"; Label5.ForeColor = System.Drawing.Color.Red; } else { System.ServiceModel.ClientBase<IService1>.CacheSetting = CacheSetting.Default; Label5.Text = "The Addition of " + TextBox1.Text + " and " + TextBox2.Text + " is " + objClient.Add(double.Parse(TextBox1.Text), double.Parse(TextBox2.Text)); Label5.ForeColor = System.Drawing.Color.Green; TextBox1.Text = string.Empty; TextBox2.Text = string.Empty; } } protected void Button2_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(TextBox1.Text) || string.IsNullOrEmpty(TextBox2.Text)) { Label5.Text = "Please Enter Some Values"; Label5.ForeColor = System.Drawing.Color.Red; } else { System.ServiceModel.ClientBase<IService1>.CacheSetting = CacheSetting.Default; Label5.Text = "The Substraction of " + TextBox1.Text + " and " + TextBox2.Text + " is " + objClient.Sub(double.Parse(TextBox1.Text), double.Parse(TextBox2.Text)); Label5.ForeColor = System.Drawing.Color.Green; TextBox1.Text = string.Empty; TextBox2.Text = string.Empty; } } protected void Button3_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(TextBox1.Text) || string.IsNullOrEmpty(TextBox2.Text)) { Label5.Text = "Please Enter Some Values"; Label5.ForeColor = System.Drawing.Color.Red; } else { System.ServiceModel.ClientBase<IService1>.CacheSetting = CacheSetting.AlwaysOff; Label5.Text = "The Multiplication of " + TextBox1.Text + " and " + TextBox2.Text + " is " + objClient.Mul(double.Parse(TextBox1.Text), double.Parse(TextBox2.Text)); Label5.ForeColor = System.Drawing.Color.Green; TextBox1.Text = string.Empty; TextBox2.Text = string.Empty; } } protected void Button4_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(TextBox1.Text) || string.IsNullOrEmpty(TextBox2.Text)) { Label5.Text = "Please Enter Some Values"; Label5.ForeColor = System.Drawing.Color.Red; } else { System.ServiceModel.ClientBase<IService1>.CacheSetting = CacheSetting.AlwaysOn; Label5.Text = "The Division of " + TextBox1.Text + " and " + TextBox2.Text + " is " + objClient.Div(double.Parse(TextBox1.Text), double.Parse(TextBox2.Text)); Label5.ForeColor = System.Drawing.Color.Green; TextBox1.Text = string.Empty;TextBox2.Text = string.Empty; } } #region Instance VariablesService1Client objClient = new Service1Client(); #endregion } } Step 8: The output of application looks like this: Step 9: The addition operation output of the application looks like this: I hope this article is useful for you. I look forward for your comments and feedback. Thanks Vijay Prativadi
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: