Navigation

Monday 11 April 2016

Web.Config File setting on AJAX Enabled WCF Service.



Web Config.

 <system.web>
    <compilation debug="true" targetFramework="4.0">      
    </compilation>
    <customErrors mode="Off"></customErrors>
  </system.web>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <validation validateIntegratedModeConfiguration="false" />

    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Content-Type"/>
        <add name="Access-Control-Allow-Methods" value="GET,POST,OPTIONS" />
      </customHeaders>
    </httpProtocol>


  </system.webServer>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="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="true" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="EndpBehavior">
          <webHttp helpEnabled="True"/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="webHttpBinding" scheme="http" />
    </protocolMapping>
    <bindings>
      <webHttpBinding>
        <binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="false" maxReceivedMessageSize="106489750" />
      </webHttpBinding>
    </bindings>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <standardEndpoints>
      <webScriptEndpoint>
        <standardEndpoint name="" crossDomainScriptAccessEnabled="true" />
      </webScriptEndpoint>
    </standardEndpoints>
    <services>
      <service name="Service">
        <endpoint address="" binding="webHttpBinding" bindingConfiguration="webHttpBindingWithJsonP" contract="IService" behaviorConfiguration="EndpBehavior" />
      </service>
    </services>
  </system.serviceModel>



Service.cs file code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using System.Web.Script.Serialization;

[ServiceContract(Namespace = "")]

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service
{
// To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)
// To create an operation that returns XML,
//     add [WebGet(ResponseFormat=WebMessageFormat.Xml)],
//     and include the following line in the operation body:
//         WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
[OperationContract]
    [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public string GetEmployees()
{
        List<Employee> empList = new List<Employee>();
        empList.Add(new Employee() { EmpId = 1, Name = "Vijay", Gender = "Male", Salary = 10000 });
        empList.Add(new Employee() { EmpId = 2, Name = "Vijay1", Gender = "Male", Salary = 10000 });
        empList.Add(new Employee() { EmpId = 3, Name = "Vijay2", Gender = "Male", Salary = 10000 });
        empList.Add(new Employee() { EmpId = 4, Name = "Vijay4", Gender = "Male", Salary = 10000 });
        empList.Add(new Employee() { EmpId = 5, Name = "Vijay5", Gender = "Male", Salary = 10000 });
        empList.Add(new Employee() { EmpId = 6, Name = "Vijay6", Gender = "Male", Salary = 10000 });
        var javaScriptSerializer = new JavaScriptSerializer();
        string jsonString = javaScriptSerializer.Serialize(empList);
        return jsonString;
       
}

// Add more operations here and mark them with [OperationContract]

}

------------------------------------------------------------------------------

Script.js file Code

/// <reference path="angular.min.js" />
var app = angular.module("myApp", []);
app.controller("myCtrl", function ($scope, $http, $log) {
    debugger;
    $http.get("Service.svc/GetEmployees")
          .then(function (responce) {
              $scope.Employees = responce.data;
          })
});


HTML page code 


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="Script/angular.min.js"></script>
    <script src="Script/Script.js"></script>
</head>
<body  ng-app="myApp" >
    <div ng-controller="myCtrl">
         {{Employees}}
    </div>  
</body>
</html>

No comments:

Post a Comment