IT tutorials
 
Technology
 

Sharepoint 2010 : Writing Custom WCF Services (part 4) - Consuming the Service in ASP.NET Ajax

8/28/2013 3:03:37 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

3. Consuming the Service in ASP.NET Ajax

ASP.NET Ajax cannot consume services that are exposed over basicHttpBinding. However, it can consume services that are exposed over webHttpBinding and use the enableWebScript behavior.

What this means to you is that with a little tweak in the web.config file of the service itself, you can easily consume this service in ASP.NET Ajax JavaScript–based code without any changes to the service. Now you can see where the client object model and ADO.NET Data Services get their good looks.

Go ahead and modify the web.config file under CustomWCFService\web.config, as shown inListing 8.

Example 8. Modified web.confg to Add Support for a JavaScript Proxy
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<bindings>
<basicHttpBinding>
<binding name="customBasicHttpBinding">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Ntlm"/>
</security>
</binding>
</basicHttpBinding>

<webHttpBinding>
<binding name="customWebHttpBinding">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Ntlm"/>
</security>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="webScriptBehavior">
<enableWebScript/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="MyServiceLibrary.ListsService">
<endpoint address="" binding="basicHttpBinding"
contract="MyServiceLibrary.IListsService"
bindingConfiguration="customBasicHttpBinding">
</endpoint>
<endpoint address="/javascript" binding="webHttpBinding"
behaviorConfiguration="webScriptBehavior"
contract="MyServiceLibrary.IListsService"
bindingConfiguration="customWebHttpBinding">
</endpoint>
</service>
</services>
</system.serviceModel>
</configuration>


Listing 8 may seem large, but don't be overwhelmed; it is actually quite simple. Let's break it down. First you added a new endpoint:

<endpoint address="/javascript" binding="webHttpBinding"
behaviorConfiguration="webScriptBehavior"
contract="MyServiceLibrary.IListsService"
bindingConfiguration="customWebHttpBinding">
</endpoint>

This endpoint uses webHttpBinding. It also uses the binding configuration of customWebHttpBinding, which is exactly the same as customBasicHttpBinding. It simply forces the browser to send the identity of the logged-in user to the server.

Also, it uses webScriptBehavior as its endpoint behavior. This behavior, as you can see from the web.config file, specifies the enableWebScript behavior.

Another thing to note is that the address of this service is different from the basicHttpBinding service. You cannot have two services with the same contracts at the same URL.

Now, in your browser, visit this site: http://sp2010/_vti_bin/CustomWCFService/listservice.svc/javascript/jsDebug.

SharePoint will prompt you to download a file, as shown in Figure 7.

Figure 7. A jsDebug file that the browser is prompting to download

This file contains the automatically generated JavaScript proxy that can now be used with ASP.NET Ajax. With the JavaScript proxy now set up, you can write very simple object-oriented Javascript code to fetch results from the WCF service from JavaScript. The code can be seen in Listing 9.

Example 9. ASP.NET Ajax Javascript Code to Query the WCF Service
<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
<Services>
<asp:ServiceReference Path="/_vti_bin/CustomWCFService/listservice.svc/javascript" />
</Services>
</asp:ScriptManagerProxy>

<script type="text/javascript">
function GetResults()
{
var resultsDiv = $get("results");

resultsDiv.innerHTML = "Fetching results .. ";

var proxy = new Winsmarts.IListsService();
proxy.GetLists(onSuccess, onFail) ;
}



function onSuccess(listsResult)
{
var result = "" ;
for (var list in listsResult)
{
var listDetails = listsResult[list] ;
result = result + listDetails.Name + " : " + listDetails.Author + "<br/>"
}
var resultsDiv = $get("results");
resultsDiv.innerHTML = result ;
}

function onFail(err){
var resultsDiv = $get("results");
resultsDiv.innerHTML = err._message;
}

_spBodyOnLoadFunctionNames.push("GetResults");
</script>
<div id="results"/>


I'm using the ScriptManagerProxy to register a reference to my WCF service. This will automatically translate into /jsDebug or /js, depending on whether you're in debug mode. Once the service reference is set up, it is a matter of executing an asynchronous call to the GetLists method and then passing in the onSuccess and onFail event handlers. The event handlers can then take care of displaying the results or the error message appropriately. This can be seen in Figure 8.

The ASPX with the code is available in the associated code download as WCFTestPage.aspx.

Figure 8. The same WCF service being queried from simple JavaScript
 
Others
 
- Sharepoint 2010 : Writing Custom WCF Services (part 3) - Consuming the Service in a Silverlight application
- Sharepoint 2010 : Writing Custom WCF Services (part 2) - Consuming the Service in a .NET Application
- Sharepoint 2010 : Writing Custom WCF Services (part 1)
- Active Directory 2008 : Configuring Password and Lockout Policies (part 2) - Fine-Grained Password and Lockout Policy
- Active Directory 2008 : Configuring Password and Lockout Policies (part 1) - Understanding Password Policies , Understanding Account Lockout Policies
- Active Directory 2008 : Implementing a Group Policy Infrastructure - Supporting Group Policy (part 2)
- Active Directory 2008 : Implementing a Group Policy Infrastructure - Supporting Group Policy (part 1)
- Microsoft Lync Server 2010 : Enterprise Voice - Call Admission Control (part 2) - Network Region Links, Network Region Routes
- Microsoft Lync Server 2010 : Enterprise Voice - Call Admission Control (part 1) - Bandwidth Policy Profiles , Associate Bandwidth Policy Profile
- Microsoft Lync Server 2010 : Enterprise Voice - Network Configuration - Network Regions, Network Sites, Network Subnets
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us