【问题标题】:Ajax-Enabled WCF Service not working when published启用 Ajax 的 WCF 服务在发布时不起作用
【发布时间】:2011-02-21 22:31:13
【问题描述】:

我不知道我的问题属于哪一类:编程错误(就我而言)或 IIS 错误。

我从 Visual Studio 2010 模板创建了一个非常简单的启用 Ajax 的 WCF 服务。代码如下:

CS 文件:

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

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class EmployeeServices
{
    [OperationContract]
    public int DoWork()
    {
        return 1;
    }
}

WEB.CONFIG(与 WCF 相关)

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

和 .SVC 文件:

<%@ ServiceHost Language="C#" Debug="true" Service="EmployeeServices" CodeBehind="~/App_Code/EmployeeServices.cs" %>

现在的问题: 当我在我的本地机器上使用 javascript 在一个网页上测试这个时,看起来像这样:

<form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
        <Services>
            <asp:ServiceReference Path="EmployeeServices.svc" />
        </Services>
    </asp:ScriptManager>
<div>
    <script type="text/javascript">
        function pass(result) {
            alert(result);
        }

        function fail(result) {
            alert(result);
        }

        $(document).ready(function () {
            var service = new EmployeeServices();
            service.DoWork(pass, fail, null);
        });
    </script>
</div>
</form>

它工作正常,直到我将它发布到网络服务器。然后我收到“EmployeeServices not defined”错误。

如何解决此问题?衷心感谢您的帮助。

【问题讨论】:

  • 您使用什么作为客户端 JavaScript 框架?微软 AJAX 的东西?
  • 我编辑了我的帖子以表明我已经放入了一个脚本管理器来连接对服务的访问。但是,我使用 jQuery 来实际编写支持 ajax 的代码。

标签: ajax wcf visual-studio-2010 iis


【解决方案1】:

为您的 svc 文件(如 [ServiceContract(Namespace = "namespacw")])和 jquery 中的 ncall 服务提供一个命名空间,并使用此命名空间(如 &lt;yournamespace&gt;.service.DoWork(pass, fail, null))。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-20
    • 1970-01-01
    • 1970-01-01
    • 2011-03-29
    • 1970-01-01
    • 2012-12-24
    相关资源
    最近更新 更多