【问题标题】:How to call WCF service from javascript in .NET 3.5?如何从 .NET 3.5 中的 javascript 调用 WCF 服务?
【发布时间】:2011-05-16 06:48:29
【问题描述】:

我在我的项目中添加 WCF 服务(Forder Services 中的 ProductService.svc):

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 :)";
        }
    } 
 }

并尝试从 javascript 文件中调用服务:

<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>

它不起作用:Microsoft JScript 运行时错误:“应用程序”未定义 我该如何管理?

【问题讨论】:

  • 页面上有asp:ScriptManagerasp:ServiceReference 吗?

标签: c# javascript jquery asp.net wcf


【解决方案1】:

您可以在 Javascript 中使用 JSON 来实现这一点。 这是一个例子http://dotnetbyexample.blogspot.com/2008/02/calling-wcf-service-from-javascript.html

【讨论】:

    【解决方案2】:

    我尝试了与您类似的代码,我认为 因为你在

    中使用了一个空的命名空间
     [ServiceContract(Namespace = "")]
    

    使用

    可以访问为代理生成的 javascript
    ProductService.GetMainGridProductHtml
    

    直接,没有命名空间Application.Services

    编辑:代码示例:

    svc 文件中:

    <%@ 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 
    

    在您的 aspx 页面中,您应该有脚本管理器和参考 (更改对您服务的引用):

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

    在您的 web.config 中,当您添加启用 WCF 的 WCF 服务时,VS2010 会添加 WCF 和 AJAX 的服务模型设置: 你的类名和命名空间可能不同,这是在我的机器上创建的:

    <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>
    

    所有这些都准备好后,您应该可以在客户端调用服务了 签名ProductService.GetMainGridProductHtml().
    (如您所料,您也可能在调试器监视窗口中看到它)

    为确保您正确生成了 javascript,请浏览到服务的 url + /jsdebug 即http://localhost:3953/Service1.svc/jsdebug

    【讨论】:

    • .svc 文件中是否有Factory 指令?
    • 看起来像这样: 但是当我尝试要查看 Watch,它根本看不到我的服务和命名空间。
    • 尝试在Service="..." 之后添加这个:Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory"
    • 我尝试在 javascript 管理器中出错。 Microsoft JScript 运行时错误:“服务”未定义...或“应用程序”在我键入 Application.Services 时未定义
    • 不要使用ApplicationServices。直接使用ProductService即可。
    猜你喜欢
    • 2012-05-29
    • 2011-08-04
    • 2010-10-16
    • 2012-09-29
    • 2010-10-19
    • 1970-01-01
    • 2017-11-29
    • 1970-01-01
    相关资源
    最近更新 更多