WCF IIS Hosting With MVC Web Application
16:23Introduction
In my previous atricles I explain hosting Wcf service on IIS with WFC Test Client. In this articales I want to show how to hosting Wcf service on IIS and using this service to MVC application.Create Project
We create a simple calculator service i.e. Add(), Subtract(), Multiply(), Divide()At first go to Visual studio > File > New > Project > Templates > WCF > WCF Service Application > Provide a name (Ex: "WcfIISHosting") of project > Ok Like below Images:
Now replace IService1.cs code by the below code.
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.Web; using System.Text; namespace WcfIISHosting { [ServiceContract] public interface IService1 { [OperationContract] double Add(double a, double b); [OperationContract] double Subtract(double a, double b); [OperationContract] double Multiply(double a, double b); [OperationContract] double Divide(double a, double b); } }
Also replace Service1.svc by the below code.
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.Web; using System.Text; namespace WcfIISHosting { public class Service1 : IService1 { public double Add(double a, double b) { return a + b; } public double Subtract(double a, double b) { return a - b; } public double Multiply(double a, double b) { return a * b; } public double Divide(double a, double b) { return a / b; } } }
Now create the service Endpoint like below:
<system.serviceModel> <services> <service behaviorConfiguration="IISHostingBehaviour" name="WcfIISHosting.Service1"> <endpoint address="" binding="basicHttpBinding" contract="WcfIISHosting.IService1"></endpoint> </service> </services> <behaviors> <serviceBehaviors> <behavior name="IISHostingBehaviour"> <!-- 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>Our service is ready to host on IIS.
Windows + R then type inetmgr
Click Ok and we can see the IIS server.
Now right click on the Sites and click Add Web Site... like below:
We can see a new window. Fill Site name (Ex: WcfIISHosting), Application pool, physical path, Port number and click Ok. Now right click on "WcfIISHosting" and browse. We look that new web site is create like below:
After click on browse the browser is open. Click Service1.svc and we see the service is open like below image:
Copy the address http://localhost:85/Service1.svc. Now the service is ready to test.
Now we add a new MVC project to this solution like below:
Select ASP.NET MVC 4 Web Application and provide project name (Ex: WcfTestService) and click Ok.
Select Basic and click Ok. New project is add successfully.
Create new controller by Right click on controller > Add > Controller... > Provide name of the controller (Ex: Test) > Add
The TestController.cs code is below:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace WcfTestService.Controllers { public class TestController : Controller { public ActionResult Index() { return View(); } } }After this, we add a service reference by Right click on References > Add Service References... > like below:
We see a new window Add Service Reference
Paste the service address to the box and click Go button. We see Service1 is show in the left site box. Click on the IService1 we see all the service in the right side i.e. Add, Divide, Multiply and Subtract. We can change the namespace (Ex: ServiceReference1). See below images:
Click Ok and we can see the ServiceReference1 is create like below:
After ServiceReference1 create we change the TestController.cs code below:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace WcfTestService.Controllers { public class TestController : Controller { public ActionResult Index() { return View(); } [HttpPost] public ActionResult Index(double a, double b, string type) { double resultAdd = 0; ServiceReference1.Service1Client obj = new ServiceReference1.Service1Client(); if (type == "Add") resultAdd = obj.Add(a, b); if (type == "Subtract") resultAdd = obj.Subtract(a, b); if (type == "Multiply") resultAdd = obj.Multiply(a, b); if (type == "Divide") resultAdd = obj.Divide(a, b); return Json(resultAdd, JsonRequestBehavior.AllowGet); ; } } }Index.cshtml code is below:
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> <script src="~/Scripts/jquery-1.7.1.min.js"></script> <script> function calc(val) { var a = $('#a').val(); var b = $('#b').val(); if (a != '' && b != '') { $.ajax({ "url": '/Test/Index', "data": { 'a': a, 'b': b, 'type': val }, "type": "POST", "success": function (result) { $('#lblResult').html(val + ' value is: ' + result); }, 'error': { } }); } else { $('#lblResult').html('Please enter the value!'); } } </script> <style> body{background-color:#eeeeef;} .head{ text-align:center; background-color:brown;color:#fff;font-size:20px;padding:10px;} .main{margin-left:45%;margin-top:10%;} table{border: 1px solid #000;padding:7px; width:300px;} table tbody tr {border-bottom:1px solid #000;} #lblResult{font-size:15px; padding:5px;color:#33a932;font-weight:bold;} </style> </head> <body> <div class="main"> <table> <tr> <td colspan="2" class="head"> WCF IIS Hosting Test </td> </tr> <tr> <td>A: </td> <td> <input type="text" name="a" id="a" /> </td> </tr> <tr> <td>B: </td> <td> <input type="text" name="b" id="b" /> </td> </tr> <tr><td> </td></tr> <tr> <td colspan="2"> <span id="lblResult"></span> </td> </tr> <tr><td> </td></tr> <tr> <td colspan="2"> <input id="btnSubmit1" type="submit" value="Add" onclick="calc('Add')" /> <input id="btnSubmit2" type="submit" value="Subtract" onclick="calc('Subtract')" /> <input id="btnSubmit3" type="submit" value="Multiply" onclick="calc('Multiply')" /> <input id="btnSubmit4" type="submit" value="Divide" onclick="calc('Divide')" /> </td> </tr> </table> </div> </body> </html>Now right click on WcfTestService project and click on Set as StartUp Project like below:
After this change the RouteConfig.cs like below:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Routing; namespace WcfTestService { public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Test", action = "Index", id = UrlParameter.Optional } ); } } }Run the application. Then put the value [A=35,B=20] and click Multiply. We can see the result comes up 700. See below image:
So our hosting service is working fine.
0 comments