启用Ajax的WCF服务在发布时无法正常工作

时间:2011-02-21 22:31:14

标签: ajax wcf visual-studio-2010 iis

我不知道我的问题属于哪个类别:编程错误(就我而言)或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>

它工作正常,直到我将其发布到Web服务器。然后我得到一个未定义的&#34; EmployeeServices&#34;错误。

如何解决此问题?我真诚地感谢你的帮助。

1 个答案:

答案 0 :(得分:1)

为您的svc文件提供命名空间,如[ServiceContract(Namespace = "namespacw")],jquery中的ncall服务使用此命名空间,如<yournamespace>.service.DoWork(pass, fail, null)