1

I add in my project WCF service (ProductService.svc in forder Services):

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

namespace Application.Services {
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode
= AspNetCompatibilityRequirementsMode.Allowed)]
    public class ProductService
    {
        [WebMethod]
        [OperationContract]
        public static string GetMainGridProductHtml()
        {
            return "Helo World :)";
        }
    } 
 }

And try to call service from javascript file:

<script type="text/javascript">
Application.Services.ProductService.GetMainGridProductHtml(ResultLoadTableMainGridHtml, ErrorLoadTableMainGridHtml);

function ResultLoadTableMainGridHtml(html) {
    debugger;
    alert("Ok");
}

function ErrorLoadTableMainGridHtml() {
    debugger;
    alert("Error");
}

function NewAddBtn() {
    debugger;
    alert("Yuppi");
}
</script>

It won't work: Microsoft JScript runtime error: 'Application' is undefined How can I manage this?

1
  • do you have a asp:ScriptManager with a asp:ServiceReference on the page? Commented May 16, 2011 at 7:05

2 Answers 2

1

you can use JSON to achieve this in Javascript. Here is an example http://dotnetbyexample.blogspot.com/2008/02/calling-wcf-service-from-javascript.html

Sign up to request clarification or add additional context in comments.

Comments

0

i tried code similar to yours, and i think that since you are using an empty namespace in

 [ServiceContract(Namespace = "")]

the javascript generated for the proxy is accessible using

ProductService.GetMainGridProductHtml

directly, and without the namespace Application.Services

EDIT: code sample:

in the svc file:

<%@ ServiceHost Language="C#" Debug="true" 
Service="Application.Services.ProductService" 
Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory"
CodeBehind="ProductService.svc.cs" %> // your cs file name may be different 

in your aspx page you should have the script manager and reference (change the reference to your service ):

 <asp:ScriptManager ID="ScriptManager1" runat="server">
            <Services>
                <asp:ServiceReference Path="~/Service1.svc" />
            </Services>
        </asp:ScriptManager>

in your web.config , when you added the WCF enabled WCF service, VS2010 adds the servcie model settings for WCF and AJAX: your class names and namespaces are probably different, this was created on my machine:

<system.serviceModel>
        <behaviors>
          <endpointBehaviors>
            <behavior name="WebApplication1.Service1AspNetAjaxBehavior">
              <enableWebScript />
            </behavior>
          </endpointBehaviors>
        </behaviors>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
          multipleSiteBindingsEnabled="true" />
        <services>
          <service name="WebApplication1.Service1">
            <endpoint address="" 
              behaviorConfiguration="WebApplication1.Service1AspNetAjaxBehavior"
              binding="webHttpBinding" contract="WebApplication1.Service1" />
          </service>
        </services>
      </system.serviceModel>

with all this in place you should be able to call the service on the client side usign ProductService.GetMainGridProductHtml().
(you may also see it in the debugger watch window, as you expected)

to make sure you get the javascript genereted correctly, browse to the url of the service + /jsdebug i.e. http://localhost:3953/Service1.svc/jsdebug

12 Comments

in the .svc file do you have a Factory directive?
Loks like this: <%@ ServiceHost Language="C#" Debug="true" Service="Application.Services.ProductService" CodeBehind="ProductService.svc.cs" %> But when I try to view Watch, it doesn't see my servise and namespace at all.
try adding this after the Service="..." : Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory"
I try but error in javascript manager. Microsoft JScript runtime error: 'Services' is undefined...or 'Application' is undefined when I typed Application.Services
dont use Application or Services. just use the ProductService directly.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.