Sunday, April 28, 2013

Difference between WCF and Ajax Enabled WCF service Templatea and how to convert from WCF service to Ajax enabled service




Sr No
Section
Regular WCF
Ajax Enabled WCF
1
Implementation
By default, two files are generated in App_Code : IXX.cs (interface)  and XX.cs (implementation)
By default, only implementation file (XX.cs) is generated.
No interface file generated.
2
Web.Config
Contract specifies interface name
Contract specifies class name
3
WCF decorations
WCF decorations are applied on interface.
WCF decorations are applied on class directly.
4
WCF decorations
Class/interface is NOT decorated with AspNetCompatibilityRequirements attribute.
Class is decorated with AspNetCompatibilityRequirements attribute.
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
5
WCF decorations
[ServiceContract] decoration in the Interface is plain, it does not specify either namespace=”” or anything else.
[ServiceCotract] decorations specifies Namespace=””
6
.svc file
No Difference
No difference
7
Default namespace inclusions in the source file
Source file has NO “using” statements for System.ServiceModel.Activation and System.ServiceModel.Web namespaces
Source file has “using” statements for :
System.ServiceModel.Activation
System.ServiceModel.Web
8
Default namespace inclusions in the source file
Source file has “using” statements for System.Collections.Generic and System.Text
Source file has NO “using” statements for System.Collections.Generic and System.Text
9
Web.Config
Contract specifies interface name
Contract specifies class name
10
Web.Config
There is no
serviceHostingEnvironment
tag in the
system.serviceModel
section
Specfies
<serviceHostingEnvironmentaspNetCompatibilityEnabled="true" />
In the
<system.serviceModel>
section
11
Web.config
In <behaviours>, only
<serviceBehaviors> is specified.
In <behaviours>, only
<endpointBehaviors> is specified.
12
Web.config
Service behaviour specifies
serviceMetadata and
serviceDebug attributes.
<serviceMetadata httpGetEnabled="true"/>
                    <serviceDebugincludeExceptionDetailInFaults="false"/>
Service behaviour is not specified.
13

Endpoint behaviour is not specified.
Endpoint behaviour specifies <enableWebScript>
<enableWebScript />
14
Web.Config
Endpoints
There are two endpoints added :
wsHttpBinding and
mexHttpBinding
Only one endpoint is added :
webHttpBinding
15
Web.config
Endpoints
There is an <identity> element inserted in the wsHttpBinding endpoint.
The endpoint does not contain identity element
16
Web.config
Endpoints
Endpoint is not given the behaviour ( obviously because it is not specified)
Endpoint is given the behaviour.


IMP : Namespace=”” is essential.
Decorating main class with ServiceContract and Aspnetcompatibility is essential. Hence replacing contract name from “IService” to “Service” in the endpoint declaration is essential.

Process:
1.       Combine the IXXXX and XXXX  interface and class files, to have only one file, i.e. the class file. This means removing interface file and decorating class file with appropriate attributes.
a.Decorate the class  declaration with [ServiceContract(Namespace=””)] and methods with [OperationCotract] attribute.
b.Remove the interface inheritance from class declaration.
c.Add ”using System.ServiceModel.Activation;” statement in the beginning of the file.
d. Decorate the class with
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

After this step, the class should look like :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;

[ServiceContract(Namespace="")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Reservation
{
    [OperationContract]
      public string GetRoom()
      {
        return "deluxe";
      }
}



Now remove the interface declaration file from project and build project.


2.       Next all changes are in web.config.
3.       Add new endpoint behaviour in the config :
                  <endpointBehaviors>
                        <behavior name="xyz">
                              <enableWebScript/>
                        </behavior>
                  </endpointBehaviors>
4.       Add New endpoint :
<endpoint address="" behaviorConfiguration="xyz" binding="webHttpBinding" contract="Reservation"/>

If there are any other endpoints present, ensure that each endpoint points to a different address. This is essential.

Also ensure that all endpoints specify contract as class name and not interface name. This is essential.

Thirdly, ensure that “webHttpBinding” endpoint points to null address (address=””). Consequently, other endpoints must be filled with some non-null address.

5.  Now access the service from script :
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">
        function aa() {
            Reservation.GetRoom(bb, cc, null);
        }
        function bb(result) {
            alert(result);
        }
        function cc(result) {
            alert(result);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="scm" runat="server">
    <Services>
    <asp:ServiceReference Path="~/Reservation.svc"  />
    </Services>
    </asp:ScriptManager>
    <asp:Button runat="server" ID="btn" OnClientClick="aa();" />
    </form>
1.  </body>
                       


No comments:

Post a Comment