我有一个使用Visual Studio Community构建的C#Web应用程序。在该应用程序中,我创建了一个WCF(启用了ajax的)服务,该服务位于[Services]文件夹中,称为Service1.svc。该应用程序在本地运行良好。

我已成功将应用程序上传到共享的GoDaddy托管帐户。但是,对[Service1.svc]的JavaScript调用导致以下错误:


加载资源失败:服务器响应状态为404(未找到)http://XXXXX.COM/WebApplication1/Services/Service1.svc/js
捕获的ReferenceError:未定义Service1]


以上错误发生在JavaScript的以下行中:


  var myService = new Service1();


为什么要尝试到达“ Service1.svc / js”?
我该如何调试此问题?

我还有一个问题是我应该在[ServiceContract(Namespace =“”))]中输入名称空间吗?

Service1.svc:

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;

namespace WebApplication1.Services
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service1
    {
        [OperationContract]
        public string DoWork(string m)
        {
            return m;
        }
    }
}


Site.Master的部分

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


Default.aspx

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">

    <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="CallMyWCF();" />

    <script>

        function CallMyWCF() {

            var myService = new Service1();
            myService.DoWork("hello world", onSuccess, null, null);
        }

        function onSuccess(result) {
            alert(result);
        }
    </script>

</asp:Content>


Web.Config的部分:

<configuration>
  ....

  <system.web>
    <customErrors mode="Off" />
    <trust level="Full" />

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

最佳答案

尝试看看为什么未创建javascript代理类。或它有什么问题。

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


该代码应生成代理类。看起来好像没有发生。

10-06 02:51