Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I consume a WCF Service from javascript in an ASP.NET WebForm?
    text
    copied!<p>I've followed the instructions on MSDN : Exposing WCF Services to Client Script (http://msdn.microsoft.com/en-us/library/bb514961.aspx), however the example does not show how to actually consume the service in javascript, which is where I am getting stuck.</p> <p>I've created a very simple WCF service :</p> <p>using System.ServiceModel; using System.Text; using System.ServiceModel.Activation;</p> <p>namespace MyNamespace {</p> <pre><code>[ServiceContract(Namespace = "MyDomain.com")] public interface IMyService { [OperationContract] void DoWork(); } // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "McCormickProdService" in code, svc and config file together. [ServiceBehavior(IncludeExceptionDetailInFaults = true)] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class MyService : IMyService { public void DoWork() { } } </code></pre> <p>}</p> <p>I've also updated by web.config :</p> <pre><code> &lt;system.serviceModel&gt; &lt;bindings&gt; &lt;webHttpBinding&gt; &lt;binding name="default"/&gt; &lt;/webHttpBinding&gt; &lt;/bindings&gt; &lt;behaviors&gt; &lt;endpointBehaviors&gt; &lt;behavior name="webScriptEnablingBehavior"&gt; &lt;enableWebScript/&gt; &lt;/behavior&gt; &lt;/endpointBehaviors&gt; &lt;serviceBehaviors&gt; &lt;behavior name=""&gt; &lt;serviceMetadata httpGetEnabled="true" /&gt; &lt;serviceDebug includeExceptionDetailInFaults="false" /&gt; &lt;/behavior&gt; &lt;/serviceBehaviors&gt; &lt;/behaviors&gt; &lt;serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /&gt; &lt;services&gt; &lt;service name="MyNamespace.MyService" behaviorConfiguration=""&gt; &lt;endpoint address="" binding="webHttpBinding" bindingConfiguration="default" contract="MyNamespace.IMyService" behaviorConfiguration="webScriptEnablingBehavior"/&gt; &lt;/service&gt; &lt;/services&gt; &lt;/system.serviceModel&gt; </code></pre> <p>And here is the code that I have cobbled together in ASP.NET / javascript :</p> <p></p> <pre><code>&lt;asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="false" EnableCdn="true" AjaxFrameworkMode="Explicit"&gt; &lt;Scripts&gt; &lt;asp:ScriptReference Path="http://ajax.aspnetcdn.com/ajax/act/40412/start.js" /&gt; &lt;/Scripts&gt; &lt;Services&gt; &lt;asp:ServiceReference Path="~/Services/MyService.svc"/&gt; &lt;/Services&gt; &lt;/asp:ScriptManager&gt; &lt;asp:ContentPlaceHolder ID="endbody" runat="server" /&gt; &lt;script type="text/javascript"&gt; Sys.loader.defineScripts(null, [{ name: "jQueryUI", releaseUrl: "http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/jquery-ui.min.js", dependencies: ["jQuery"], isLoaded: !!(window.jQuery &amp;&amp; jQuery.ui)}]); Sys.loader.defineScripts(null, [{ name: "jQueryUIdatepickerfr", releaseUrl: "http://jquery-ui.googlecode.com/svn/trunk/ui/i18n/jquery.ui.datepicker-fr.js", dependencies: ["jQueryUI"], isLoaded: !!(window.jQuery &amp;&amp; jQuery.ui)}]); Sys.require([ Sys.scripts.ApplicationServices, Sys.scripts.Templates, Sys.scripts.DataContext, Sys.scripts.WebServices, Sys.scripts.jQuery, Sys.scripts.jQueryUI, Sys.scripts.jQueryUIdatepickerfr], function () { MyDomain.com.IMyService.DoWork(function () { alert('success') }, function () { alert('failure') }, null); }); &lt;/script&gt; </code></pre> <p></p> <p>When I load my page, I am receiving the following errors :</p> <p>Error: 'Sys.Net.WebServiceProxy' is null or not an object Error: Object doesn't support this property or method</p> <p>Remarks :</p> <ul> <li>I'm loading a couple of other scripts, such as jQuery and jQuery UI (including the French localization, which I could only find on Google for the time being).</li> <li>I can't figure out if I absolutely have to use the Sys.require method, or if I could just reference the needed scripts directly using like we used to do in the olden days.</li> <li>I placed the script manager and script elements at the bottom of my page, outside the form element but inside the body element, is this the right place, or should it all be in the head?</li> <li>I set the EnableCDN parameter to true, thus I'm using the Microsoft content distribution network, although I'd really rather host these scripts myself. This is an internal project, which may have a long lifespan with little intervention, so I'd prefer not to have the rug pulled out from under my feet when Microsoft decides to upgrade their scripts and break compatibility. However, I can't actually figure out how to download these scripts, or if I even need to (perhaps they are bundled inside some resource file somewhere on my disk). Although I can find some of the scripts here: <a href="http://www.asp.net/ajaxlibrary/CDNAjax4.ashx" rel="nofollow">http://www.asp.net/ajaxlibrary/CDNAjax4.ashx</a>, there are certain scripts that I can't find, such as MicrosoftAjaxTemplates.js (I'm not using Templates just yet, but I'd like to give it a bash later).</li> <li>Do I need to reference the start.js script directly? I understand that this is the script that allows the Sys.require method to work, which then brings in the remainder of the scripts.</li> <li>I have referenced System.scripts.WebServices directly; do I need to do this, or is this referenced automagically when I have a service reference?</li> <li>Perhaps I'm chasing my tail for nothing and I don't need to use Sys.require at all ?!</li> </ul> <p>Hope someone brilliant out there can give a dog a bone, because I'm lost!</p> <p>Thanks,</p> <p>Patrick</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload