{"id":124,"date":"2013-09-06T12:08:50","date_gmt":"2013-09-06T12:08:50","guid":{"rendered":"https:\/\/praveenkatiyar.wordpress.com\/?p=124"},"modified":"2013-09-06T12:08:50","modified_gmt":"2013-09-06T12:08:50","slug":"creating-simple-wcf-service-with-iis-hosting","status":"publish","type":"post","link":"https:\/\/praveenkatiyar.in\/blog\/index.php\/2013\/09\/06\/creating-simple-wcf-service-with-iis-hosting\/","title":{"rendered":"Creating Simple WCF Service with IIS Hosting"},"content":{"rendered":"<p><font color=\"#000080\">In this post I am going to explain how to create a simple WCF Service, with IIS Hosting and consume it, there will be two parts of the project. <\/font><\/p>\n<ul>\n<li><font color=\"#000080\"><strong>A WCF Service (MyMathService.svc): <\/strong>will be hosted by IIS.<\/font> <\/li>\n<li><font color=\"#000080\"><strong>Client Application (<strong>MyMathClient<\/strong>.exe):<\/strong> Client Application which will use this service.<\/font> <\/li>\n<\/ul>\n<p><font color=\"#000080\">let\u2019s start with creating the WCF Service.<\/font><\/p>\n<h3><font style=\"font-weight:bold;\">Creating the WCF Service<\/font><\/h3>\n<p><font color=\"#000080\">Start the Visual Studio 2010 in administrator Mode (<strong>Run as Administrator<\/strong>), and click <strong>File-&gt;New-&gt;Web Site<\/strong>. Select the project template &#8216;WCF Service&#8217; and Location as http. This will directly host the service in IIS and click OK. select the template \u201c<strong>WCF Service\u201d.<\/strong>&#160; <\/font><\/p>\n<p><a href=\"http:\/\/praveenkatiyar.wordpress.com\/wp-content\/uploads\/2013\/09\/image.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"background-image:none;float:none;padding-top:0;padding-left:0;margin-left:auto;display:block;padding-right:0;margin-right:auto;border-width:0;\" border=\"0\" alt=\"image\" src=\"http:\/\/praveenkatiyar.wordpress.com\/wp-content\/uploads\/2013\/09\/image_thumb.png\" width=\"561\" height=\"337\" \/><\/a><\/p>\n<p><font color=\"#000080\">Now let&#8217;s analyse the project created by wizard for us, and do little house keeping, before we write any code.<\/font><\/p>\n<ul>\n<li><font color=\"#000080\">IService.cs (the interface definition of the service)<\/font> <\/li>\n<li><font color=\"#000080\">Service.cs (the implementation of the service).<\/font> <\/li>\n<li><font color=\"#000080\">web.config (Service configuration file).<\/font> <\/li>\n<li><font color=\"#000080\">Service.svc<\/font> <\/li>\n<\/ul>\n<h4>Assumption<\/h4>\n<p><font color=\"#000080\">We are going to create a Service as per following assumptions.<\/font><\/p>\n<ul>\n<li><font color=\"#000080\">Service will be named as &quot;<strong>MyMathService<\/strong>&quot; (<strong>MyMathService.svc<\/strong>)<\/font> <\/li>\n<li><font color=\"#000080\">Interface will named as IMyMathService (<strong>IMyMathService.cs<\/strong>)<\/font> <\/li>\n<li><font color=\"#000080\">Service will be implemented as MyMathService (<strong>MyMathService.cs<\/strong>)<\/font> <\/li>\n<li><font color=\"#000080\">Service will implement <strong>Http endpoint at Port 8001<\/strong>.<\/font> <\/li>\n<li><font color=\"#000080\">Service will also expose <strong>IMetadataExchange<\/strong> endpoint.&#160; <strong>IMetadataExchange <\/strong>interface Exposes methods used to return metadata about a service.<\/font> <\/li>\n<\/ul>\n<h4>Little house keeping. (just to keep things organized)<\/h4>\n<ul>\n<li><font color=\"#000080\">Rename <strong>IService.cs as IMyMathService.cs<\/strong>.<\/font> <\/li>\n<li><font color=\"#000080\">Rename <strong>Service.cs as MyMathService.cs<\/strong>.<\/font> <\/li>\n<li><font color=\"#000080\">Rename <strong>Service.svc as MyMathService.svc<\/strong>, and change its content as <\/font><\/li>\n<\/ul>\n<p><font color=\"#000080\"><strong>Previous Content<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Arial Narrow\">&lt;%@ ServiceHost Language=&quot;C#&quot; Debug=&quot;true&quot; Service=&quot;<strong><font color=\"#ff0000\">Service<\/font><\/strong>&quot; CodeBehind=&quot;~\/App_Code\/<strong><font color=\"#ff0000\">Service.cs<\/font><\/strong>&quot; %&gt;<\/font><\/p>\n<p><font color=\"#000080\"><strong>New Content<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Arial Narrow\">&lt;%@ ServiceHost Language=&quot;C#&quot; Debug=&quot;true&quot; Service=&quot;<strong><font color=\"#ff0000\">MyMathService<\/font><\/strong>&quot; CodeBehind=&quot;~\/App_Code\/<strong><font color=\"#ff0000\">MyMathService.cs<\/font><\/strong>&quot; %<strong>&gt;<\/strong><\/font><\/p>\n<h4>Defining Configuration File<\/h4>\n<h5>Changing web.config file.<\/h5>\n<p><font color=\"#000080\">Below is the listing of the configuration file, generated by visual studio wizard, <font color=\"#000080\">as we know, that Services are defined in <font color=\"#ff0000\"><strong>&lt;System.servicemodel&gt;<\/strong><\/font> section. Below is the listing of the configuration file, generated by visual studio wizard.&#160; let&#8217;s define a <\/font>service and endpoint, see the highlighted part. <\/font><\/p>\n<blockquote>\n<p>&lt;?xml version=&quot;1.0&quot;?&gt;      <br \/><font color=\"#333333\"><strong>&lt;configuration&gt;<\/strong><\/font><\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;configSections&gt;      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; . . . . . . . . .       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; . . . . . . . . .       <br \/>&#160;&#160;&#160;&#160;&#160; &lt;\/configSections&gt;<\/p>\n<p>&#160;&#160;&#160; &lt;system.web&gt;      <br \/>&#160;&#160;&#160;&#160; . . . . . . . . .       <br \/>&#160;&#160;&#160;&#160; . . . . . . . . .       <br \/>&#160;&#160;&#160; &lt;\/system.web&gt;<\/p>\n<p><font color=\"#ff0000\"><strong>&#160; &lt;system.serviceModel&gt;&#160; <br \/><\/strong><\/font><font style=\"background-color:#ffff00;\">&lt;services&gt;        <br \/>&#160;&#160;&#160;&#160; &lt;service name=&quot;MyMathService&quot; behaviorConfiguration=&quot;<strong>svcBehavior<\/strong>&quot;&gt;&#160;&#160; <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><font style=\"background-color:#ffff00;\">&lt;endpoint address=<\/font><a href=\"http:\/\/localhost:8001\/MyMathService\/\"><font style=\"background-color:#ffff00;\"><\/font><\/a><a href=\"http:\/\/localhost:8001\/MyMathService\/\">http:\/\/localhost:8001\/MyMathService\/<\/a><\/a><font style=\"background-color:#ffff00;\">MyMathService.svc<\/font>&quot; <font style=\"background-color:#ffff00;\">binding=&quot;wsHttpBinding&quot; contract=&quot;IMyMathService&quot;&gt; <\/font><\/p>\n<p><font style=\"background-color:#ffff00;\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;identity&gt; &lt;dns value=&quot;localhost&quot;\/&gt; &lt;\/identity&gt;        <br \/>&lt;\/endpoint&gt;<\/font><\/p>\n<p><font style=\"background-color:#ffff00;\">&#160;&#160;&#160;&#160;&#160; &lt;endpoint address =&quot;MEX&quot; binding =&quot;mexHttpBinding&quot; contract=&quot;IMetadataExchange&quot; \/&gt;&#160; <br \/>&#160;&#160; &lt;\/service&gt;&#160; <br \/>&lt;\/services&gt;         <br \/><\/font><\/p>\n<p>&#160;&#160; <font color=\"#9b00d3\"><\/font><font color=\"#9b00d3\">&lt;behaviors&gt;        <br \/>&#160;&#160;&#160;&#160;&#160; &lt;serviceBehaviors&gt;         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;behavior&#160; <font style=\"background-color:#ffff00;\">name=&quot;svcBehavior&quot;&gt;          <br \/><\/font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;!&#8211; To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment &#8211;&gt;         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <strong>&lt;serviceMetadata httpGetEnabled=&quot;true&quot;\/&gt;          <br \/><\/strong>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;!&#8211; To receive exception details in faults for debugging purposes, set the value below to true.&#160; Set to false before deployment to avoid disclosing exception information &#8211;&gt;         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;serviceDebug includeExceptionDetailInFaults=&quot;true&quot;\/&gt;         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;\/behavior&gt;         <br \/>&#160;&#160;&#160;&#160;&#160; &lt;\/serviceBehaviors&gt;         <br \/>&#160;&#160;&#160; &lt;\/behaviors&gt;         <br \/>&#160;&#160;&#160; <br \/><\/font>&#160;&#160;&#160; &lt;serviceHostingEnvironment multipleSiteBindingsEnabled=&quot;true&quot; \/&gt;       <br \/>&#160;<font color=\"#ff0000\"><strong> &lt;\/system.serviceModel&gt;&#160; <br \/><\/strong><\/font><\/p>\n<p>&lt;system.codedom&gt;      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; . . . . . . . . .       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; . . . . . . . . .       <br \/>&#160;&#160;&#160;&#160;&#160; &lt;\/system.codedom&gt;<\/p>\n<p><font color=\"#c0504d\"><strong><\/strong><\/font><\/p>\n<p>&#160;&#160; &lt;system.webServer&gt;      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; . . . . . . . . .       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; . . . . . . . . .       <br \/>&#160;&#160; &lt;\/system.webServer&gt;&#160;&#160;&#160;&#160; <\/p>\n<p>&#160;&#160; &lt;runtime&gt;      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; . . . . . . . . .       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; . . . . . . . . .       <br \/>&#160;&#160; &lt;\/runtime&gt;<\/p>\n<p><strong><font color=\"#008000\"><\/font><\/strong>&#160; <br \/><font color=\"#333333\"><strong>&lt;\/configuration&gt;<\/strong><\/font><\/p>\n<\/blockquote>\n<h5>Explanation<\/h5>\n<p><font color=\"#000080\">See the highlighted text, as you can see service defines two endpoints, one is first end point defines an address(<strong>http:\/\/localhost:8001\/MyMathService<\/strong>), binding(<strong>HTTP<\/strong>) and contract (<strong>IMyMathService<\/strong>), the other is MEX endpoint, <strong>MEX<\/strong> endpoint are defined to import Metadata (to be used by client applications) from a Service using utility <strong>SVCUtil.exe<\/strong><\/font><\/p>\n<p><font style=\"background-color:#ffff00;\">&lt;endpoint <strong>address<\/strong> =&quot;<\/font><font style=\"background-color:#ffff00;\">http:\/\/localhost:8001\/MyMathService&quot;<\/font><font style=\"background-color:#ffff00;\"> <strong>binding<\/strong> =&quot;basicHttpBinding&quot; <\/font><font style=\"background-color:#ffff00;\"><strong>contract<\/strong>=&quot;IMyMathService&quot; \/&gt;       <br \/><\/font><\/p>\n<p><font style=\"background-color:#ffff00;\">&lt;endpoint <strong>address<\/strong> =&quot;MEX&quot; <strong>binding<\/strong> =&quot;mexHttpBinding&quot; <strong>contract<\/strong>=&quot;IMetadataExchange&quot; \/&gt;<\/font><\/p>\n<p><font color=\"#000080\">In the <strong>serviceBehaviors<\/strong>, a behavior named &quot;<strong>svcBehavior<\/strong>&quot; defines following attributes about the service.<\/font><\/p>\n<p><strong><font style=\"background-color:#ffff00;\">&lt;serviceMetadata httpGetEnabled=&quot;true&quot;\/&gt; <\/font><\/strong><\/p>\n<p><font color=\"#000080\">metadata information about the service can disclosed through a get request (if this is <strong>false<\/strong>, <strong>SVCUtil.exe<\/strong> would not be able to obtain metadata information about the service).<\/font><\/p>\n<p><strong><font style=\"background-color:#ffff00;\">&lt;serviceDebug includeExceptionDetailsInFaults=&quot;true&quot;\/&gt; <\/font><\/strong><\/p>\n<p><font color=\"#000080\">can be set true, to receive exception details in faults for debugging purposes, should be set to false, before deployment, to avoid disclosing exception information.<\/font><\/p>\n<h4>Defining Service Interface (IMyMathService.cs)<\/h4>\n<p><font color=\"#0000ff\">using System;      <br \/>using System.ServiceModel;       <br \/>using System.Text;       <br \/><\/font><\/p>\n<p><font color=\"#0000ff\"><strong>[ServiceContract] <\/strong>      <br \/>public interface IMyMathService       <br \/>{       <br \/>&#160;&#160;&#160; <strong>[OperationContract]        <br \/><\/strong>&#160;&#160;&#160; double Add(double dblNum1, double dblNum2);<\/font><\/p>\n<p><font color=\"#0000ff\"><strong>&#160;&#160;&#160; [OperationContract] <\/strong>      <br \/>&#160;&#160;&#160; double Subtract(double dblNum1, double dblNum2);<\/font><\/p>\n<p><font color=\"#0000ff\"><strong>&#160;&#160;&#160; [OperationContract] <\/strong>      <br \/>&#160;&#160;&#160; double Multiply(double dblNum1, double dblNum2);<\/font><\/p>\n<p><font color=\"#0000ff\"><strong>&#160;&#160;&#160; [OperationContract]        <br \/><\/strong>&#160;&#160;&#160; double Divide(double dblNum1, double dblNum2);       <br \/>}<\/font><\/p>\n<h5>explanation<\/h5>\n<p><font color=\"#000080\">as one can see, there is one service contract (IMyMathService), and 4 operation contract to perform 4 basic math operations.<\/font><\/p>\n<h4>Implementing Service (MyMathService.cs)<\/h4>\n<p><font color=\"#0000ff\">using System;      <br \/>using System.ServiceModel;       <br \/>using System.Text;<\/font><\/p>\n<p><font color=\"#0000ff\">public class MyMathService : IMyMathService      <br \/>{       <br \/>&#160;&#160;&#160; public double Add(double dblNum1, double dblNum2)       <br \/>&#160;&#160;&#160; {       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; return (dblNum1 + dblNum2);       <br \/>&#160;&#160;&#160; }<\/font><\/p>\n<p><font color=\"#0000ff\">&#160;&#160;&#160; public double Subtract(double dblNum1, double dblNum2)      <br \/>&#160;&#160;&#160; {       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; return (dblNum1 &#8211; dblNum2);       <br \/>&#160;&#160;&#160; }<\/font><\/p>\n<p><font color=\"#0000ff\">&#160;&#160;&#160; public double Multiply(double dblNum1, double dblNum2)      <br \/>&#160;&#160;&#160; {       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; return (dblNum1 * dblNum2);       <br \/>&#160;&#160;&#160; }<\/font><\/p>\n<p><font color=\"#0000ff\">&#160;&#160;&#160; public double Divide(double dblNum1, double dblNum2)      <br \/>&#160;&#160;&#160; {       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; return ((dblNum2 == 0) ? 0 : (dblNum1 \/ dblNum2));       <br \/>&#160;&#160;&#160; }       <br \/>}<\/font><\/p>\n<h5>explanation<\/h5>\n<p><font color=\"#000080\">here simply those 4 methods has been implemented. <\/font><\/p>\n<h3><font style=\"font-weight:bold;\">Creating Client for the Service <\/font><\/h3>\n<p><font color=\"#000080\">Add a new project to the workspace, say a Console based application, name it <strong>MyMathClient<\/strong>, for instance.<\/font><\/p>\n<p><a href=\"http:\/\/praveenkatiyar.wordpress.com\/wp-content\/uploads\/2013\/09\/image1.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"background-image:none;padding-top:0;padding-left:0;display:inline;padding-right:0;border-width:0;\" border=\"0\" alt=\"image\" src=\"http:\/\/praveenkatiyar.wordpress.com\/wp-content\/uploads\/2013\/09\/image_thumb1.png\" width=\"563\" height=\"345\" \/><\/a><\/p>\n<p><font color=\"#000080\">newly created project will be added to the work space.<\/font><\/p>\n<h4>Generating proxy and config file.<font color=\"#000080\"><\/font><font color=\"#000080\"><\/font>     <\/p>\n<p><font color=\"#000080\"><font color=\"#000080\">right click on the newly created (client) project, and click on <\/font><font color=\"#000080\"><strong>&quot;Add Service Reference<\/strong>&quot;, as shown below.<\/font><\/font><\/p>\n<\/h4>\n<p><font color=\"#000080\"><\/font><\/p>\n<p><a href=\"http:\/\/praveenkatiyar.wordpress.com\/wp-content\/uploads\/2013\/09\/image2.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"background-image:none;float:none;padding-top:0;padding-left:0;margin-left:auto;display:block;padding-right:0;margin-right:auto;border-width:0;\" border=\"0\" alt=\"image\" src=\"http:\/\/praveenkatiyar.wordpress.com\/wp-content\/uploads\/2013\/09\/image_thumb2.png\" width=\"380\" height=\"185\" \/><\/a><\/p>\n<p><font color=\"#000080\"><\/font><\/p>\n<p><font color=\"#000080\">a dialog box will be displayed as shown below. in the address bar type the address of the service, <\/font><\/p>\n<p><a href=\"http:\/\/praveenkatiyar.wordpress.com\/wp-content\/uploads\/2013\/09\/image3.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"background-image:none;padding-top:0;padding-left:0;display:inline;padding-right:0;border-width:0;\" border=\"0\" alt=\"image\" src=\"http:\/\/praveenkatiyar.wordpress.com\/wp-content\/uploads\/2013\/09\/image_thumb3.png\" width=\"568\" height=\"381\" \/><\/a><\/p>\n<p><font color=\"#000080\"><font color=\"#000080\"><font color=\"#000080\"><\/font>        <\/p>\n<p><font color=\"#000080\">Click OK, <\/font><\/p>\n<ul>\n<li>A app.config (client side configuration) will be added to the project. <\/li>\n<li>a Service reference named MyMathServiceProxy will be added in <strong>Service References node<\/strong> of the client project. <\/li>\n<\/ul>\n<h3><font style=\"font-weight:bold;\">Calling the Service<\/font><\/h3>\n<p>             <\/font><font color=\"#000080\"><\/font><\/font>    <\/p>\n<p><font color=\"#000080\">Now write the code to use the proxy and call the service, here is the listing of the <strong>Program.cs<\/strong>.<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Arial\">using System;        <br \/>using System.Collections.Generic;         <br \/>using System.Linq;         <br \/>using System.Text;<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Arial\">namespace MyMathClient        <br \/>{         <br \/>&#160;&#160;&#160; class Program         <br \/>&#160;&#160;&#160; {         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; static void Main(string[] args)         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; {         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; double dblX = 2000.0 ;         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; double dblY = 100.0 ;         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; double dblResult = 0 ;         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; MyMathServiceProxy.MyMathServiceClient mathClient = new MyMathServiceProxy.MyMathServiceClient();<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Arial\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; dblResult = mathClient.Add (dblX, dblY );        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;Calling Add &gt;&gt;&#160; X : {0:F2}&#160; Y : {1:F2}&#160; Result : {2:F2}&quot;, dblX, dblY, dblResult);         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; dblResult = mathClient.Subtract (dblX, dblY);         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;Calling Sub &gt;&gt;&#160; X : {0:F2}&#160; Y : {1:F2}&#160; Result : {2:F2}&quot;, dblX, dblY, dblResult);<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Arial\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; dblResult = mathClient.Multiply(dblX, dblY);        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;Calling Mul &gt;&gt;&#160; X : {0:F2}&#160; Y : {1:F2}&#160; Result : {2:F2}&quot;, dblX, dblY, dblResult);<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Arial\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; dblResult = mathClient.Divide(dblX, dblY);        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;Calling Sub &gt;&gt;&#160; X : {0:F2}&#160; Y : {1:F2}&#160; Result : {2:F2}&quot;, dblX, dblY, dblResult);         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }         <br \/>&#160;&#160;&#160; }         <br \/>}<\/font><\/p>\n<p>and here is the output.<\/p>\n<p><a href=\"http:\/\/praveenkatiyar.wordpress.com\/wp-content\/uploads\/2013\/09\/image4.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"background-image:none;padding-top:0;padding-left:0;display:inline;padding-right:0;border-width:0;\" border=\"0\" alt=\"image\" src=\"http:\/\/praveenkatiyar.wordpress.com\/wp-content\/uploads\/2013\/09\/image_thumb4.png\" width=\"695\" height=\"74\" \/><\/a><\/p>\n<h3><font style=\"font-weight:bold;\">Creating a web Client for the Service <\/font><\/h3>\n<p><font color=\"#000080\">Add a new web site to the workspace, name it <strong>MyWebMathClient<\/strong>, for instance as shown below.<\/font><\/p>\n<p><a href=\"http:\/\/praveenkatiyar.wordpress.com\/wp-content\/uploads\/2013\/09\/image5.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-top:0;border-right:0;background-image:none;border-bottom:0;padding-top:0;padding-left:0;border-left:0;display:inline;padding-right:0;\" border=\"0\" alt=\"image\" src=\"http:\/\/praveenkatiyar.wordpress.com\/wp-content\/uploads\/2013\/09\/image_thumb5.png\" width=\"587\" height=\"376\" \/><\/a><\/p>\n<p>Add Service reference of the Math Service to the newly created website, as shown below<\/p>\n<p><a href=\"http:\/\/praveenkatiyar.wordpress.com\/wp-content\/uploads\/2013\/09\/image6.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-top:0;border-right:0;background-image:none;border-bottom:0;padding-top:0;padding-left:0;border-left:0;display:inline;padding-right:0;\" border=\"0\" alt=\"image\" src=\"http:\/\/praveenkatiyar.wordpress.com\/wp-content\/uploads\/2013\/09\/image_thumb6.png\" width=\"573\" height=\"405\" \/><\/a><\/p>\n<p>Adding this Service Reference, made a change in the web.config file of the Client Web site, if you open the web.config, you will find the following changes, at the bottom of the file.<\/p>\n<p><font color=\"#0000ff\">&lt;system.serviceModel&gt;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;bindings&gt;        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;wsHttpBinding&gt;        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;binding name=&quot;WSHttpBinding_IMyMathService&quot; \/&gt;        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;\/wsHttpBinding&gt;        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;\/bindings&gt;        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;client&gt;        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;endpoint address=&quot;<\/font><font color=\"#0000ff\"><strong>http:\/\/localhost\/MyMathService\/MyMathService.svc<\/strong>&quot;<\/font>      <br \/><font color=\"#0000ff\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; binding=&quot;wsHttpBinding&quot; bindingConfiguration=&quot;WSHttpBinding_IMyMathService&quot;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; contract=&quot;MyMathServiceReference.IMyMathService&quot; name=&quot;WSHttpBinding_IMyMathService&quot;&gt;        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;identity&gt;<\/font><\/p>\n<p><font color=\"#0000ff\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color=\"#ff0000\"><strong> &lt;!\u2014this name can be different on your machine. \u2013!&gt;           <br \/><\/strong><\/font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;servicePrincipalName value=&quot;<strong>host\/PRAVEEN-WIN7<\/strong>&quot; \/&gt;        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;\/identity&gt;        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;\/endpoint&gt;        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;\/client&gt;        <br \/>&#160;&#160;&#160; &lt;\/system.serviceModel&gt;<\/font><\/p>\n<p>&#160;<\/p>\n<p>Open the Page_load Handler of the Default.aspx in the newly created (Client Web Site) and put the code as shown below.<\/p>\n<p><font color=\"#0000ff\" size=\"1\" face=\"Verdana\"><strong>protected void Page_Load(object sender, EventArgs e)         <br \/>&#160;&#160; {          <br \/>&#160;&#160;&#160;&#160;&#160;&#160; double dblX = 10000.0;          <br \/>&#160;&#160;&#160;&#160;&#160;&#160; double dblY = 2000.0;          <br \/>&#160;&#160;&#160;&#160;&#160;&#160; double dblResult = 0;          <br \/>&#160;&#160;&#160;&#160;&#160;&#160; Response.Write(&quot;&lt;p&gt;Value 1 : &quot; + dblX.ToString(&quot;F2&quot;));          <br \/>&#160;&#160;&#160;&#160;&#160;&#160; Response.Write(&quot;&lt;br&gt;Value 2 : &quot; + dblY.ToString(&quot;F2&quot;));<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"1\" face=\"Verdana\"><strong>&#160;&#160;&#160;&#160;&#160;&#160; try         <br \/>&#160;&#160;&#160;&#160;&#160;&#160; {          <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Response.Write(&quot;&lt;p&gt;Using WS HTTP Binding&quot;);<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"1\" face=\"Verdana\"><strong>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; MyMathServiceReference.MyMathServiceClient mathProxy2 = new MyMathServiceReference.MyMathServiceClient(&quot;WSHttpBinding_IMyMathService&quot;);<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"1\" face=\"Verdana\"><strong>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; dblResult = mathProxy2.Add(dblX, dblY);         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Response.Write(&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\" size=\"1\" face=\"Verdana\"><strong>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; dblResult = mathProxy2.Subtract(dblX, dblY);         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Response.Write(&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\" size=\"1\" face=\"Verdana\"><strong>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; dblResult = mathProxy2.Multiply(dblX, dblY);         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Response.Write(&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><strong><font color=\"#0000ff\" size=\"1\" face=\"Verdana\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; dblResult = mathProxy2.Divide(dblX, dblY);         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Response.Write(&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;));          <br \/>&#160;&#160;&#160;&#160;&#160;&#160; }          <br \/>&#160;&#160;&#160;&#160;&#160;&#160; catch (Exception eX)          <br \/>&#160;&#160;&#160;&#160;&#160;&#160; {          <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Response.Write(&quot;There was an error while calling Service &lt;p&gt; &lt;b&gt;[&quot; + eX.Message + &quot;] &lt;\/b&gt;&quot;);          <br \/>&#160;&#160;&#160;&#160;&#160;&#160; }          <br \/>&#160;&#160; }<\/font>        <br \/><\/strong><\/p>\n<\/p>\n<p><font color=\"#000080\">and when you run this web site, you will get following output.<\/font><\/p>\n<p><a href=\"http:\/\/praveenkatiyar.wordpress.com\/wp-content\/uploads\/2013\/09\/image7.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-top:0;border-right:0;background-image:none;border-bottom:0;padding-top:0;padding-left:0;border-left:0;display:inline;padding-right:0;\" border=\"0\" alt=\"image\" src=\"http:\/\/praveenkatiyar.wordpress.com\/wp-content\/uploads\/2013\/09\/image_thumb7.png\" width=\"380\" height=\"216\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post I am going to explain how to create a simple WCF Service, with IIS Hosting and consume it, there will be two parts of the project. A WCF Service (MyMathService.svc): will be hosted by IIS. Client Application (MyMathClient.exe): Client Application which will use this service. let\u2019s start with creating the WCF Service.&hellip; <a class=\"more-link\" href=\"https:\/\/praveenkatiyar.in\/blog\/index.php\/2013\/09\/06\/creating-simple-wcf-service-with-iis-hosting\/\">Continue reading <span class=\"screen-reader-text\">Creating Simple WCF Service with IIS Hosting<\/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":[3,12],"tags":[17,33],"class_list":["post-124","post","type-post","status-publish","format-standard","hentry","category-codeproject","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\/124","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=124"}],"version-history":[{"count":0,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/posts\/124\/revisions"}],"wp:attachment":[{"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}