【发布时间】:2015-12-22 11:52:25
【问题描述】:
我有一个使用 Visual Studio Community 构建的 C# Web 应用程序。在应用程序中,我创建了一个 WCF(启用 ajax)服务,该服务位于名为 Service1.svc 的 [Services] 文件夹中。该应用程序在本地运行良好。
我已成功将应用程序上传到共享的 GoDaddy 托管帐户。但是,对 [Service1.svc] 的 JavaScript 调用会导致以下错误:
- 加载资源失败:服务器响应状态为 404(未找到)http://XXXXX.COM/WebApplication1/Services/Service1.svc/js
- Uncaught ReferenceError: Service1 is not defined]
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 c# asp.net wcf asp.net-ajax