{"id":285,"date":"2013-10-01T07:08:50","date_gmt":"2013-10-01T07:08:50","guid":{"rendered":"https:\/\/praveenkatiyar.wordpress.com\/?p=285"},"modified":"2013-10-01T07:08:50","modified_gmt":"2013-10-01T07:08:50","slug":"wcf-hosting-with-windows-service","status":"publish","type":"post","link":"https:\/\/praveenkatiyar.in\/blog\/index.php\/2013\/10\/01\/wcf-hosting-with-windows-service\/","title":{"rendered":"WCF Hosting with Windows Service"},"content":{"rendered":"<p>There are several ways to host a WCF service library (IIS, Windows Service, Self Hosting), Windows Service is one of them. Windows service hosting option is suitable for a long-running WCF service hosted outside of IIS in a secure environment that is not message activated. The lifetime of the service is controlled instead by the operating system. In this article, I am going to explain how to host a WCF library in a Windows service.<\/p>\n<p>his article has been divided into 4 modules: <\/p>\n<ul>\n<li><strong>WCF Service Library (<em>WCFCalcLib.dll<\/em>)<\/strong>: Actual Service logic, which defines a Service Contract Interface, <code>OperationContract<\/code>, and implements them, and exposes few functions to the world to use them <\/li>\n<li><strong>Windows Service To Host the WCF Service Library <\/strong><strong>WinSvcHostedCalcService.exe<\/strong>): Host the WCF library <\/li>\n<li><strong>Console Client Application (<em>CalcClient.exe<\/em>):<\/strong> Client Application which will use this service <\/li>\n<li><strong>Web based Client Application (<em>CalcWebClient.exe<\/em>):<\/strong> Web Application which will use this service <\/li>\n<\/ul>\n<h3><font style=\"font-weight:bold;\">First Module: WCF Service Library (WCFCalcLib.dll) <\/font><\/h3>\n<p>To create this project, you can simply take a &quot;<strong>Class Library&quot; <\/strong>project, while choosing from project wizard option. let&#8217;s name it &quot;<code>WCFCalcLib<\/code>&quot;, it is the actual service which implements the business logic. The project already contains a file <em>Class1.cs<\/em>, rename that file as <em>CalcService.cs<\/em>, add one more file (<em>ICalcService.cs<\/em>) to define the interface, although you can define interface in this file also. <\/p>\n<h4>Definition of Service Contract <\/h4>\n<pre>    <font color=\"#0000ff\"><strong>[ServiceContract]\n    public interface ICalcService\n    {\n        [OperationContract]\n        double Add(double dblNum1, double dblNum2);\n        [OperationContract]\n        double Subtract(double dblNum1, double dblNum2);\n        [OperationContract]\n        double Multiply(double dblNum1, double dblNum2);\n        [OperationContract]\n        double Divide(double dblNum1, double dblNum2);\n    } <\/strong><\/font><\/pre>\n<h5>Explanation <\/h5>\n<p>Interface simply defines 4 operations supported by this service. <\/p>\n<h4>Implementation of Service Contract <\/h4>\n<pre><font color=\"#0000ff\"><strong>public class CalcService : ICalcService\n    {\n        public double Add(double dblNum1, double dblNum2)\n        {\n            return (dblNum1 + dblNum2);\n        }\n \n        public double Subtract(double dblNum1, double dblNum2)\n        {\n            return (dblNum1 - dblNum2);\n        }\n \n        public double Multiply(double dblNum1, double dblNum2)\n        {\n            return (dblNum1 * dblNum2);\n        }\n \n        public double Divide(double dblNum1, double dblNum2)\n        {\n            return ((dblNum2 == 0) ? 0 : (dblNum1 \/ dblNum2));\n        }\n    } <\/strong><\/font><\/pre>\n<h5><font color=\"#0000ff\">ServiceContract<\/font> Attribute <\/h5>\n<p>Service Contract describes which related operations can be tied together as a single functional unit that the client can perform on the service.<\/p>\n<h5><font color=\"#0000ff\">OperationContract<\/font> Attribute <\/h5>\n<p>An Operation contract specifies that the said operation is exposed by the service, service defines the parameters and return type of an operation. <\/p>\n<p>As one can see, there is nothing special here, just a Service Contract definition and its implementation. <\/p>\n<h3><font style=\"font-weight:bold;\">Second Module: WCF Service Library (WinSvcHostedCalcService.exe)<\/font><\/h3>\n<p>To create this project, you can simply take a &quot;<strong>Windows Service&quot;<\/strong>, while choosing from project wizard option. let&#8217;s name it &quot;<code><strong>WinSVCHostedCalcService<\/strong><\/code>&quot;. This module is a Windows service, which is going to host our WCF Library (business logic), providing endpoints to the outside world to use WCF library. In this module at the start of the service, an infrastructure will be created to expose the service, and when the service is stopped, all objects are disposed. <\/p>\n<h4>Little housekeeping <\/h4>\n<p>Rename <em>Service1.cs<\/em> as <font color=\"#0000ff\"><em><strong>MyCalcWinService.cs<\/strong><\/em>.<\/font><\/p>\n<p>Add a member variable <code>ServiceHost <\/code>type. As the name implies, <code>ServiceHost <\/code>provides a host for services. Name it, for instance <code><strong>m_svcHost<\/strong><\/code>. <\/p>\n<pre><font color=\"#0000ff\">private ServiceHost m_svcHost = null ; <\/font><\/pre>\n<pre>Implement the <code>OnStart () <\/code>and <code>OnStop () <\/code>handler for the service. <\/pre>\n<h5>Creating end points when Service is started <\/h5>\n<pre><font color=\"#0000ff\"><strong>protected override void OnStart(string[] args)\n        {\n            if (m_svcHost != null) m_svcHost.Close();\n                \n            string strAdrHTTP = &quot;http:\/\/localhost:9001\/CalcService&quot;;\n            string strAdrTCP = &quot;net.tcp:\/\/localhost:9002\/CalcService&quot;;\n \n            Uri[] adrbase = { new Uri(strAdrHTTP), new Uri(strAdrTCP) };\n            m_svcHost = new ServiceHost(typeof(WCFCalcLib.CalcService), adrbase);\n \n            ServiceMetadataBehavior mBehave = new ServiceMetadataBehavior();\n            m_svcHost.Description.Behaviors.Add(mBehave);\n \n            BasicHttpBinding httpb = new BasicHttpBinding();\n            m_svcHost.AddServiceEndpoint(typeof(WCFCalcLib.ICalcService), httpb, strAdrHTTP);\n            m_svcHost.AddServiceEndpoint(typeof(IMetadataExchange), \n            MetadataExchangeBindings.CreateMexHttpBinding(), &quot;mex&quot;);\n \n            NetTcpBinding tcpb = new NetTcpBinding();\n            m_svcHost.AddServiceEndpoint(typeof(WCFCalcLib.ICalcService), tcpb, strAdrTCP);\n            m_svcHost.AddServiceEndpoint(typeof(IMetadataExchange), \n            MetadataExchangeBindings.CreateMexTcpBinding(), &quot;mex&quot;);\n \n            m_svcHost.Open();\n        }  <\/strong><\/font><\/pre>\n<h6>Explanation <\/h6>\n<p>The <code>OnStart() <\/code>handler is called, when the said service is started (through service control panel, or command line or otherwise), it does the following things. <\/p>\n<p>&#160;<\/p>\n<p>Defines two end points. <\/p>\n<pre><strong><font color=\"#0000ff\">string strAdrHTTP = &quot;http:\/\/localhost:9001\/CalcService&quot;;\nstring strAdrTCP = &quot;net.tcp:\/\/localhost:9002\/CalcService&quot;; <\/font><\/strong><\/pre>\n<p>Creates and initializes a new instance of <code>ServiceHost <\/code>class, with the instance of the service (<code>WCFCalcLib.CalcService<\/code>) and its base addresses (<code>adrbase<\/code>) specified. <\/p>\n<pre><font color=\"#0000ff\"><strong>Uri[] adrbase = { new Uri(strAdrHTTP), new Uri(strAdrTCP) };\nm_svcHost = new ServiceHost(typeof(WCFCalcLib.CalcService), adrbase)<\/strong><\/font>  <\/pre>\n<h6>Adding Service Behavior <\/h6>\n<p><code>ServiceMetadataBehavior <\/code>class controls the publication of service metadata and associated information, although. <\/p>\n<pre><font color=\"#0000ff\"><strong>ServiceMetadataBehavior mBehave = new ServiceMetadataBehavior();\nm_svcHost.Description.Behaviors.Add(mBehave); <\/strong><\/font><\/pre>\n<h6>Adding service endpoints to the hosted service<\/h6>\n<h6>Add Basic TCP end point <\/h6>\n<p><code>ServiceMetadataBehavior <\/code>class controls the publication of service metadata and associated information, although. <\/p>\n<pre><font color=\"#0000ff\"><strong>NetTcpBinding tcpb = new NetTcpBinding();\nm_svcHost.AddServiceEndpoint(typeof(WCFCalcLib.ICalcService), tcpb, strAdrTCP);<\/strong><\/font>  <\/pre>\n<h6>Add Basic HTTP end point <\/h6>\n<p><font color=\"#0000ff\"><strong>BasicHttpBinding httpb = new BasicHttpBinding();<br \/>\n      <br \/>m_svcHost.AddServiceEndpoint(typeof(WCFCalcLib.ICalcService), httpb, strAdrHTTP);<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\"><strong>Adding just behavior to the service is not sufficient for publication of metadata, you must add an <code>IMetadataExchange <\/code>endpoint to your service for each supported binding type. <\/strong><\/font><\/p>\n<p><font color=\"#0000ff\"><strong>m_svcHost.AddServiceEndpoint(typeof(IMetadataExchange),<br \/>\n      <br \/>MetadataExchangeBindings.CreateMexHttpBinding(), &quot;mex&quot;);<\/p>\n<p>m_svcHost.AddServiceEndpoint(typeof(IMetadataExchange), <\/p>\n<p>MetadataExchangeBindings.CreateMexTcpBinding(), &quot;mex&quot;);<\/strong><\/font>&#160; <\/p>\n<p>Finally open the host, and we are ready to go. <\/p>\n<p><font color=\"#0000ff\"><strong>m_svcHost.Open();<\/strong><\/font> <\/p>\n<h6>Disposing, when Service is Stopped <\/h6>\n<p><font color=\"#0000ff\"><strong>protected override void OnStop()<br \/>\n      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; {<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (m_svcHost != null)<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; m_svcHost.Close();<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; m_svcHost = null;<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<\/strong><\/font>&#160; <\/p>\n<p>When the service is stopped either by service console\/command prompt, or by any other means, Service host is closed, and object is assigned <code>null<\/code>. <\/p>\n<h6>Installing the Service <\/h6>\n<p>Windows service needs to be installed, and should be running, you can install the service using <em>InstallUtil.exe<\/em> utility. This utility is part of the SDK, the path is set, when you run this utility through <strong>Visual Studio command prompt<\/strong>. <\/p>\n<p>Open Visual Studio Command Prompt through:<\/p>\n<p><strong><font color=\"#0000ff\">Start -&gt; All Programs -&gt; Microsoft Visual Studio 2010 -&gt; Visual Studio Tools -&gt; Visual Studio Command Prompt<\/font> <\/strong><\/p>\n<p>Switch to the folder, where <em>WinSvcHostedCalcService.exe<\/em> has been built, or preferably where you want to deploy it. <\/p>\n<p>Issue the following command to Install the service:<\/p>\n<pre><font color=\"#0000ff\"><strong>Command Prompt &gt; InstallUtil  WinSvcHostedCalcService.exe &lt;return&gt;<\/strong><\/font><\/pre>\n<pre><img loading=\"lazy\" decoding=\"async\" border=\"0\" hspace=\"0\" src=\"http:\/\/www.codeproject.com\/KB\/WCF\/653493\/InstallService.jpg\" width=\"726\" height=\"264\" \/><\/pre>\n<h6>Starting the Service<\/h6>\n<p>You can start the service using command prompt using the following command: <\/p>\n<pre><font color=\"#0000ff\"><strong>Command Prompt &gt; sc start WinSvcHostedCalcService &lt;return&gt;<\/strong><\/font> <\/pre>\n<pre><img loading=\"lazy\" decoding=\"async\" border=\"0\" hspace=\"0\" src=\"http:\/\/www.codeproject.com\/KB\/WCF\/653493\/startService.jpg\" width=\"640\" height=\"121\" \/><\/pre>\n<p>Another way to start the service is to: <\/p>\n<p><strong>Computer -&gt; Manage -&gt; Services and Applications -&gt; Services<\/strong><\/p>\n<p><strong>Locate<\/strong> the Service named as <code>WinSvcHostedCalcService<\/code>, <strong>right click<\/strong> and select <strong>Start<\/strong>. <\/p>\n<h3><strong>Third Module: Console Client Application (<\/strong><strong>CalcClient.exe)<\/strong><\/h3>\n<p>Once the service has been built, and deployed, it is time to create the client. I have chosen the console based application, to keep things simple. Select the project template &quot;Console Application&quot;, name it <code>CalcClient<\/code>. <\/p>\n<h6>Generating the Service Proxy <\/h6>\n<p>To use the service, we have to generate the proxy. <strong>Switch to the folder where client project has been created<\/strong>. Open a command prompt and issue a command to generate the proxy. <\/p>\n<p><strong><font color=\"#0000ff\">Command Prompt &gt;&#160; SvcUtil http:\/\/localhost:9001\/CalcService \/out:CalcServiceProxy.cs \/config:App.config &lt;return&gt;<\/font><\/strong> <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" border=\"0\" hspace=\"0\" src=\"http:\/\/www.codeproject.com\/KB\/WCF\/653493\/WinClientGenerateProxy.JPG\" width=\"696\" height=\"130\" \/><\/p>\n<p>Two files &#8211; a Service Proxy file and a client configuration file is generated. <\/p>\n<ul>\n<li><em><strong>App.config<\/strong><\/em><\/li>\n<li><em><strong>CalcServiceProxy.cs<\/strong><\/em><\/li>\n<\/ul>\n<p><strong>Add these to files to the newly generated client project.<\/strong> When we closely examine the configuration file, we find the following sections: <\/p>\n<pre><strong><font color=\"#0000ff\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\n&lt;configuration&gt;\n    &lt;system.serviceModel&gt;\n        &lt;bindings&gt;\n\t\t. . . .\n        &lt;\/bindings&gt;\n        &lt;client&gt;\n\t\t. . . .\n        &lt;\/client&gt;\n    &lt;\/system.serviceModel&gt;\n&lt;\/configuration&gt;<\/font><\/strong><\/pre>\n<p>The binding section holds the collection of standard and custom bindings. Each entry is a binding element that can be identified by its unique name. <\/p>\n<pre> <font color=\"#0000ff\"><strong>&lt;bindings&gt;\n      &lt;basicHttpBinding&gt;\n           &lt;binding name=&quot;BasicHttpBinding_ICalcService&quot; \/&gt;\n      &lt;\/basicHttpBinding&gt;\n      &lt;netTcpBinding&gt;\n           &lt;binding name=&quot;NetTcpBinding_ICalcService&quot; \/&gt;\n      &lt;\/netTcpBinding&gt;\n&lt;\/bindings&gt;<\/strong><\/font><\/pre>\n<p>The client section contains a list of endpoints a client uses to connect to a service. <\/p>\n<pre>&lt;client&gt;\n<strong>     &lt;endpoint<\/strong> address=&quot;http:\/\/localhost:9001\/CalcService&quot; binding=&quot;basicHttpBinding&quot;\n                bindingConfiguration=&quot;BasicHttpBinding_ICalcService&quot; \n                contract=&quot;ICalcService&quot;  name=&quot;<strong>BasicHttpBinding_ICalcService<\/strong>&quot; \/&gt;\n     <strong>&lt;endpoint <\/strong>address=&quot;net.tcp:\/\/localhost:9002\/CalcService&quot; \n            binding=&quot;netTcpBinding&quot; bindingConfiguration=&quot;NetTcpBinding_ICalcService&quot; \n                contract=&quot;ICalcService&quot; name=&quot;<strong>NetTcpBinding_ICalcService<\/strong>&quot;&gt;\n           &lt;identity&gt;\n                    &lt;servicePrincipalName value=&quot;<strike>host\/PRAVEEN-WIN7<\/strike>&quot; \/&gt;\n           &lt;\/identity&gt;\n      &lt;\/endpoint&gt;\n&lt;\/client&gt;  <\/pre>\n<p>The client section contains a list of endpoints a client uses to connect to a service. <\/p>\n<p><strong>Note<\/strong>: Identity might be different on your machine. <\/p>\n<p>Calling the service, using the Proxy<\/p>\n<p>Open <em>programs.cs<\/em> file of the client project and write code to use the service, using the generated proxy class, as shown below:<\/p>\n<blockquote>\n<p>\/\/Listing of Client Program (<em>Program.cs<\/em>): <\/p>\n<\/blockquote>\n<pre><font color=\"#0000ff\"><strong>using System;\nusing System.Text;\nnamespace CalcClient\n{\n    class Program\n    {\n        static void Main(string[] args)\n        {\n            double dblX = 2000.0;\n            double dblY = 100.0;\n            double dblResult = 0;\n            try\n            {\n                Console.WriteLine(&quot;Using TCP Binding&quot;, dblX, dblY, dblResult);\n                CalcServiceClient objCalcClient1 = new CalcServiceClient\n                (&quot;NetTcpBinding_ICalcService&quot;);\n                dblResult = objCalcClient1.Add(dblX, dblY);\n                Console.WriteLine(&quot;Calling Add &gt;&gt;  X : {0:F2}  Y : {1:F2}  \n                Result : {2:F2}&quot;, dblX, dblY, dblResult);\n                dblResult = objCalcClient1.Subtract(dblX, dblY);\n                Console.WriteLine(&quot;Calling Sub &gt;&gt;  X : {0:F2}  Y : {1:F2}  \n                Result : {2:F2}&quot;, dblX, dblY, dblResult);\n                dblResult = objCalcClient1.Multiply(dblX, dblY);\n                Console.WriteLine(&quot;Calling Mul &gt;&gt;  X : {0:F2}  Y : {1:F2}  \n                Result : {2:F2}&quot;, dblX, dblY, dblResult);\n                dblResult = objCalcClient1.Divide(dblX, dblY);\n                Console.WriteLine(&quot;Calling Sub &gt;&gt;  X : {0:F2}  Y : {1:F2}  \n                Result : {2:F2}&quot;, dblX, dblY, dblResult);\n                Console.WriteLine(&quot;Using Basic HTTP Binding&quot;, dblX, dblY, dblResult);\n                CalcServiceClient objCalcClient2 = new CalcServiceClient\n                (&quot;BasicHttpBinding_ICalcService&quot;);\n                dblResult = objCalcClient2.Add(dblX, dblY);\n                Console.WriteLine(&quot;Calling Add &gt;&gt;  X : {0:F2}  Y : {1:F2}  \n                Result : {2:F2}&quot;, dblX, dblY, dblResult);\n                dblResult = objCalcClient2.Subtract(dblX, dblY);\n                Console.WriteLine(&quot;Calling Sub &gt;&gt;  X : {0:F2}  Y : {1:F2}  \n                Result : {2:F2}&quot;, dblX, dblY, dblResult);\n                dblResult = objCalcClient2.Multiply(dblX, dblY);\n                Console.WriteLine(&quot;Calling Mul &gt;&gt;  X : {0:F2}  Y : {1:F2}  \n                Result : {2:F2}&quot;, dblX, dblY, dblResult);\n                dblResult = objCalcClient2.Divide(dblX, dblY);\n                Console.WriteLine(&quot;Calling Sub &gt;&gt;  X : {0:F2}  Y : {1:F2}  \n                Result : {2:F2}&quot;, dblX, dblY, dblResult);\n            }\n            catch (Exception eX)\n            {\n                Console.WriteLine(&quot;There was an error while calling Service \n                [&quot; + eX.Message + &quot;]&quot;);\n            }\n        }\n    }\n} <\/strong><\/font><\/pre>\n<h6>Explanation <\/h6>\n<p>It simply does two things:<\/p>\n<h6>Creates the proxy object for the TCP binding<\/h6>\n<p><font color=\"#0000ff\"><strong>CalcServiceClient objCalcClient1 = new CalcServiceClient(&quot;NetTcpBinding_ICalcService&quot;); <\/strong><\/font><\/p>\n<p>Call the service, and print the result:<\/p>\n<p><font color=\"#0000ff\"><strong>dblResult = objCalcClient1.Add(dblX, dblY);<br \/>\n      <br \/>Console.WriteLine(&quot;Calling Add &gt;&gt;&#160; X : {0:F2}&#160; Y : {1:F2}&#160; Result : {2:F2}&quot;, dblX, dblY, dblResult);<\/p>\n<p>&#160; <br \/>dblResult = objCalcClient1.Subtract(dblX, dblY);<\/p>\n<p>Console.WriteLine(&quot;Calling Sub &gt;&gt;&#160; X : {0:F2}&#160; Y : {1:F2}&#160; Result : {2:F2}&quot;, dblX, dblY, dblResult);<\/p>\n<p>&#160; <br \/>dblResult = objCalcClient1.Multiply(dblX, dblY);<\/p>\n<p>Console.WriteLine(&quot;Calling Mul &gt;&gt;&#160; X : {0:F2}&#160; Y : {1:F2}&#160; Result : {2:F2}&quot;, dblX, dblY, dblResult);<\/p>\n<p>&#160; <br \/>dblResult = objCalcClient1.Divide(dblX, dblY);<\/p>\n<p>Console.WriteLine(&quot;Calling Sub &gt;&gt;&#160; X : {0:F2}&#160; Y : {1:F2}&#160; Result : {2:F2}&quot;, dblX, dblY, dblResult);<\/p>\n<p><\/strong><\/font><\/p>\n<p>Creates the proxy object for the HTTP binding <\/p>\n<p><font color=\"#0000ff\"><strong>CalcServiceClient objCalcClient2 = new CalcServiceClient(&quot;BasicHttpBinding_ICalcService&quot;); <\/strong><\/font><\/p>\n<p>Call the service, and print the result: <\/p>\n<p><font color=\"#0000ff\"><strong> dblResult = objCalcClient2.Add(dblX, dblY);<br \/>\n      <br \/>Console.WriteLine(&quot;Calling Add &gt;&gt;&#160; X : {0:F2}&#160; Y : {1:F2}&#160; Result : {2:F2}&quot;, dblX, dblY, dblResult);<\/p>\n<p>&#160; <br \/>dblResult = objCalcClient2.Subtract(dblX, dblY);<\/p>\n<p>Console.WriteLine(&quot;Calling Sub &gt;&gt;&#160; X : {0:F2}&#160; Y : {1:F2}&#160; Result : {2:F2}&quot;, dblX, dblY, dblResult);<\/p>\n<p>&#160; <br \/>dblResult = objCalcClient2.Multiply(dblX, dblY);<\/p>\n<p>Console.WriteLine(&quot;Calling Mul &gt;&gt;&#160; X : {0:F2}&#160; Y : {1:F2}&#160; Result : {2:F2}&quot;, dblX, dblY, dblResult);<\/p>\n<p>&#160; <br \/>dblResult = objCalcClient2.Divide(dblX, dblY);<\/p>\n<p>Console.WriteLine(&quot;Calling Sub &gt;&gt;&#160; X : {0:F2}&#160; Y : {1:F2}&#160; Result : {2:F2}&quot;, dblX, dblY, dblResult);<\/strong><\/font> <\/p>\n<p>Build and execute and here is the output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" border=\"0\" hspace=\"0\" src=\"http:\/\/www.codeproject.com\/KB\/WCF\/653493\/ClientOutput.JPG\" width=\"640\" height=\"145\" \/><\/p>\n<h5>Fourth Module: Client Web page, which can use this service <\/h5>\n<p>Create a new Web Site, open the default page (<em>default.aspx<\/em>), put some controls, and set the properties as described. <\/p>\n<ul>\n<li><font color=\"#0000ff\"><strong>asp:Label ID=&quot;Label1&quot; Text =&quot;Value 1 :&quot;, runat=&quot;server&quot; <\/strong><\/font><\/li>\n<li><font color=\"#0000ff\"><strong>asp:Label ID=&quot;Label2&quot; Text =&quot;Value 2 :&quot;, runat=&quot;server&quot; <\/strong><\/font><\/li>\n<li><font color=\"#0000ff\"><strong>asp:TextBox ID=&quot;txtVal1&quot; runat=&quot;server&quot; <\/strong><\/font><\/li>\n<li><font color=\"#0000ff\"><strong>asp:TextBox ID=&quot;txtVal2&quot; runat=&quot;server&quot; <\/strong><\/font><\/li>\n<li><font color=\"#0000ff\"><strong>asp:Button = ID=&quot;btnCalc&quot;, onclick=&quot;btnCalc_Click&quot;, Text=&quot;Call Service&quot; , runat=&quot;server&quot; <\/strong><\/font><\/li>\n<\/ul>\n<p>Below is the listing of <em>default.aspx<\/em>: <\/p>\n<p><font color=\"#0000ff\"><strong>&lt;%@ Page Language=&quot;C#&quot; AutoEventWireup=&quot;true&quot;&#160; <br \/>CodeFile=&quot;Default.aspx.cs&quot; Inherits=&quot;_Default&quot; %&gt;&lt;span style=&quot;<\/p>\n<p>font-size: 9pt;&quot;&gt;&lt;!DOCTYPE html PUBLIC &quot;-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN&quot; <\/p>\n<p>&quot;http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd&quot;&gt;&lt;\/span&gt;&lt;pre&gt;&lt;html <\/p>\n<p>xmlns=&quot;http:\/\/www.w3.org\/1999\/xhtml&quot;&gt;<\/p>\n<p>&lt;head runat=&quot;server&quot;&gt;<\/p>\n<p>&#160;&#160;&#160; &lt;title&gt;&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>&#160;&#160;&#160; &lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt;<\/p>\n<p>&#160;&#160;&#160; &lt;div&gt;<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;asp:Label ID=&quot;Label1&quot; runat=&quot;server&quot; <\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; Text=&quot;Value 1 : &quot;&gt;&lt;\/asp:Label&gt;<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;asp:TextBox ID=&quot;txtVal1&quot; runat=&quot;server&quot;&gt;&lt;\/asp:TextBox&gt;<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;br \/&gt;<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;asp:Label ID=&quot;Label2&quot; runat=&quot;server&quot; <\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; Text=&quot;Value 2 : &quot;&gt;&lt;\/asp:Label&gt;<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;asp:TextBox ID=&quot;txtVal2&quot; runat=&quot;server&quot;&gt;&lt;\/asp:TextBox&gt;<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;br \/&gt;<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;asp:Button ID=&quot;btnCalc&quot; runat=&quot;server&quot; <\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; onclick=&quot;btnCalc_Click&quot; Text=&quot;Button&quot; <\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Width=&quot;91px&quot; \/&gt;<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;br \/&gt;<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;asp:Label ID=&quot;lblOutput&quot; runat=&quot;server&quot; <\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; BorderStyle=&quot;None&quot; Height=&quot;152px&quot; <\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; Width=&quot;606px&quot;&gt;&lt;\/asp:Label&gt;<\/p>\n<p>&#160;&#160;&#160; &lt;\/div&gt;<\/p>\n<p>&#160;&#160;&#160; &lt;\/form&gt;<\/p>\n<p>&lt;\/body&gt; <\/p>\n<p>&lt;\/html&gt; <\/strong><\/font><\/p>\n<h6>Generating the Service Proxy <\/h6>\n<p>Right click on the website project node, select the option Add service reference. type any <strong>mex<\/strong> end point address of the service in the <strong>Address :<\/strong> field: <\/p>\n<p><strong><font color=\"#0000ff\">http:\/\/localhost:9001\/CalcService\/mex<\/font><\/strong><\/p>\n<p>or: <\/p>\n<p><strong><font color=\"#0000ff\">net.tcp:\/\/localhost:9002\/CalcService\/mex<\/font> <\/strong><\/p>\n<p><strong><\/strong><\/p>\n<p>In the name give any name of your choice, it can be the name of the client&#8217;s namespace, let&#8217;s say for instance name it <code><strong>CalcServiceReference<\/strong><\/code>. <\/p>\n<p>Click OK as shown below:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" border=\"0\" hspace=\"0\" src=\"http:\/\/www.codeproject.com\/KB\/WCF\/653493\/WebClientGenerateProxy.JPG\" width=\"632\" height=\"510\" \/><\/p>\n<p>It will add a new <em><strong><font color=\"#0000ff\">App_WebReferences<\/font><\/strong><\/em> folder to your web site and will modify your <em>web.config<\/em> file, let&#8217;s analyze the changes, it made to the <em>web.config<\/em>. It made a new node in the last of the existing <em><font color=\"#0000ff\"><strong>web.config<\/strong><\/font><\/em> named <code><strong><font color=\"#0000ff\">&lt;system.ServiceModel&gt;<\/font><\/strong> <\/code>as shown below:<\/p>\n<p><font color=\"#0000ff\">&lt;system.serviceModel&gt;<br \/>\n    <br \/>&#160;&#160;&#160; &lt;bindings&gt;<\/p>\n<p>&#160;&#160;&#160;&#160;&#160; &lt;basicHttpBinding&gt;<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <strong>&lt;binding name=&quot;BasicHttpBinding_ICalcService&quot;\/&gt;<\/strong><\/p>\n<p>&#160;&#160;&#160;&#160;&#160; &lt;\/basicHttpBinding&gt;<\/p>\n<p>&#160;&#160;&#160;&#160;&#160; &lt;netTcpBinding&gt;<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160; <strong> &lt;binding name=&quot;NetTcpBinding_ICalcService&quot;\/&gt;<\/strong><\/p>\n<p>&#160;&#160;&#160;&#160;&#160; &lt;\/netTcpBinding&gt;<\/p>\n<p>&#160;&#160;&#160; &lt;\/bindings&gt;<\/p>\n<p>&#160;&#160;&#160; &lt;client&gt;<\/p>\n<p>&#160;&#160;&#160;&#160;&#160; &lt;endpoint <strong>address<\/strong><strong>=&quot;http:\/\/localhost:9001\/CalcService<\/strong>&quot; <\/p>\n<p>&#160;&#160;&#160;&#160;&#160; binding=&quot;<strong>basicHttpBinding<\/strong>&quot; <\/p>\n<p>&#160;&#160;&#160;&#160;&#160; bindingConfiguration=&quot;<strong>BasicHttpBinding_ICalcService<\/strong>&quot; <\/p>\n<p>&#160;&#160;&#160;&#160;&#160; contract=&quot;CalcServiceReference.ICalcService&quot; <\/p>\n<p>&#160;&#160;&#160;&#160;&#160; name=&quot;<strong>BasicHttpBinding_ICalcService<\/strong>&quot;\/&gt;<\/p>\n<p>&#160;&#160;&#160;&#160;&#160; &lt;endpoint<strong><\/strong><strong> address=&quot;net.tcp:\/\/localhost:9002\/CalcService&quot;<\/strong> <\/p>\n<p>&#160;&#160;&#160;&#160;&#160; binding=&quot;<strong>netTcpBinding<\/strong>&quot; <\/p>\n<p>&#160;&#160;&#160;&#160;&#160; bindingConfiguration=&quot;<strong>NetTcpBinding_ICalcService<\/strong>&quot; <\/p>\n<p>&#160;&#160;&#160;&#160;&#160; contract=&quot;CalcServiceReference.ICalcService&quot; <\/p>\n<p>&#160;&#160;&#160;&#160;&#160; name=&quot;<strong>NetTcpBinding_ICalcService<\/strong>&quot;&gt;<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;identity&gt;<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;servicePrincipalName value=&quot;<strike>host\/PRAVEEN-WIN7<\/strike>&quot;\/&gt;<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;\/identity&gt;<\/p>\n<p>&#160;&#160;&#160;&#160;&#160; &lt;\/endpoint&gt;<\/p>\n<p>&#160;&#160;&#160; &lt;\/client&gt;<\/p>\n<p><\/font><font color=\"#0000ff\">&lt;\/system.serviceModel&gt;<\/font><\/p>\n<p>If you analyze it carefully, you will see two sections <code>bindings <\/code>and <code>client<\/code>, <code>binding <\/code>sections simply defines the default binding names supported by this service, and <code>client <\/code>section defines the endpoints available for this service. Each endpoint has been named, so that we know, which endpoint we are using to call the service. <\/p>\n<h6>Calling the Service <\/h6>\n<p>Call the service, I have called the service on the <strong>click event<\/strong> of <strong>Call Service<\/strong> button. Code for the event handler is shown below:<\/p>\n<p><font color=\"#0000ff\"><strong>protected void btnCalc_Click(object sender, EventArgs e)<br \/>\n      <br \/>&#160;&#160;&#160; {<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; double dblX = 0, dblY = 0 ;<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; bool b1 = double.TryParse(txtVal1.Text, out dblX);<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; bool b2 = double.TryParse(txtVal2.Text, out dblY);<\/p>\n<p>&#160; <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; if ((b1) &amp;&amp; (b2))<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; {<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; StringBuilder sbTmp = new StringBuilder( &quot;&lt;font size=3 color=#000080&gt;&quot;);<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\"><strong>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; sbTmp.Append(&quot;&lt;p&gt;Value 1 : &quot; + dblX.ToString(&quot;F2&quot;));<br \/>\n      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; sbTmp.Append(&quot;&lt;br&gt;Value 2 : &quot; + dblY.ToString(&quot;F2&quot;));<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; sbTmp.Append (&quot;&lt;p&gt;Using TCP Binding&quot;);<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\"><strong>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CalcServiceReference.CalcServiceClient calcProxy1 = new CalcServiceReference.CalcServiceClient(&quot;NetTcpBinding_ICalcService&quot;);<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\"><strong>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; double dblResult = calcProxy1.Add(dblX, dblY);<br \/>\n      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; sbTmp.Append(&quot;&lt;br&gt;Calling Add &gt;&gt;&#160; X : &quot; + dblX.ToString(&quot;F2&quot;) + &quot;&#160; Y : &quot; + dblY.ToString(&quot;F2&quot;) + &quot; Result : &quot; + dblResult.ToString(&quot;F2&quot;));<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\"><strong>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; dblResult = calcProxy1.Subtract(dblX, dblY);<br \/>\n      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; sbTmp.Append(&quot;&lt;br&gt;Calling Add &gt;&gt;&#160; X : &quot; + dblX.ToString(&quot;F2&quot;) + &quot;&#160; Y : &quot; + dblY.ToString(&quot;F2&quot;) + &quot; Result : &quot; + dblResult.ToString(&quot;F2&quot;));<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\"><strong>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; dblResult = calcProxy1.Multiply(dblX, dblY);<br \/>\n      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; sbTmp.Append(&quot;&lt;br&gt;Calling Add &gt;&gt;&#160; X : &quot; + dblX.ToString(&quot;F2&quot;) + &quot;&#160; Y : &quot; + dblY.ToString(&quot;F2&quot;) + &quot; Result : &quot; + dblResult.ToString(&quot;F2&quot;));<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\"><strong>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; dblResult = calcProxy1.Divide(dblX, dblY);<br \/>\n      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; sbTmp.Append(&quot;&lt;br&gt;Calling Add &gt;&gt;&#160; X : &quot; + dblX.ToString(&quot;F2&quot;) + &quot;&#160; Y : &quot; + dblY.ToString(&quot;F2&quot;) + &quot; Result : &quot; + dblResult.ToString(&quot;F2&quot;));<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\"><strong>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; sbTmp.Append(&quot;&lt;p&gt;Using Basic HTTP Binding&quot;);<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\"><strong>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CalcServiceReference.CalcServiceClient calcProxy2 = new CalcServiceReference.CalcServiceClient(&quot;BasicHttpBinding_ICalcService&quot;);<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\"><strong>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; dblResult = calcProxy2.Add(dblX, dblY);<br \/>\n      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; sbTmp.Append(&quot;&lt;br&gt;Calling Add &gt;&gt;&#160; X : &quot; + dblX.ToString(&quot;F2&quot;) + &quot;&#160; Y : &quot; + dblY.ToString(&quot;F2&quot;) + &quot; Result : &quot; + dblResult.ToString(&quot;F2&quot;));<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\"><strong>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; dblResult = calcProxy2.Subtract(dblX, dblY);<br \/>\n      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; sbTmp.Append(&quot;&lt;br&gt;Calling Add &gt;&gt;&#160; X : &quot; + dblX.ToString(&quot;F2&quot;) + &quot;&#160; Y : &quot; + dblY.ToString(&quot;F2&quot;) + &quot; Result : &quot; + dblResult.ToString(&quot;F2&quot;));<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\"><strong>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; dblResult = calcProxy2.Multiply(dblX, dblY);<br \/>\n      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; sbTmp.Append(&quot;&lt;br&gt;Calling Add &gt;&gt;&#160; X : &quot; + dblX.ToString(&quot;F2&quot;) + &quot;&#160; Y : &quot; + dblY.ToString(&quot;F2&quot;) + &quot; Result : &quot; + dblResult.ToString(&quot;F2&quot;));<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\"><strong>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; dblResult = calcProxy2.Divide(dblX, dblY);<br \/>\n      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; sbTmp.Append(&quot;&lt;br&gt;Calling Add &gt;&gt;&#160; X : &quot; + dblX.ToString(&quot;F2&quot;) + &quot;&#160; Y : &quot; + dblY.ToString(&quot;F2&quot;) + &quot; Result : &quot; + dblResult.ToString(&quot;F2&quot;));<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\"><strong>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; sbTmp.Append ( &quot;&lt;font&gt;&quot;);<br \/>\n      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; lblOutput.Text = sbTmp.ToString(); <\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; else<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; {<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; lblOutput.Text = &quot;&lt;font size=4 COLOR=#ff0000&gt; Invalid Input &lt;\/font&gt;&quot;;<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<\/p>\n<p>&#160;&#160;&#160; }<\/strong><\/font><\/p>\n<h6>Output <\/h6>\n<p>And here is the output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" border=\"0\" hspace=\"0\" align=\"bottom\" src=\"http:\/\/www.codeproject.com\/KB\/WCF\/653493\/weboutput.jpg\" width=\"411\" height=\"380\" \/><\/p>\n<h4>Points of Interest <\/h4>\n<ul>\n<li>There was no configuration file on the Hosting side (neither with Windows service, nor with WCF library). at the hosting side (in Windows service). End points are generated on the fly, this way, the port value can be made configurable. <\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>There are several ways to host a WCF service library (IIS, Windows Service, Self Hosting), Windows Service is one of them. Windows service hosting option is suitable for a long-running WCF service hosted outside of IIS in a secure environment that is not message activated. The lifetime of the service is controlled instead by the&hellip; <a class=\"more-link\" href=\"https:\/\/praveenkatiyar.in\/blog\/index.php\/2013\/10\/01\/wcf-hosting-with-windows-service\/\">Continue reading <span class=\"screen-reader-text\">WCF Hosting with Windows Service<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[17,33],"class_list":["post-285","post","type-post","status-publish","format-standard","hentry","category-soa","tag-net","tag-wcf","entry"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/posts\/285","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=285"}],"version-history":[{"count":0,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/posts\/285\/revisions"}],"wp:attachment":[{"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}