【问题标题】:How to display result of a WCF service through a client如何通过客户端显示 WCF 服务的结果
【发布时间】:2019-12-07 03:36:46
【问题描述】:

我一直在关注有关设置启用 AJAX 的 WCF 服务并使用客户端访问它们的 Microsoft 教程。但是,尽管完全按照教程进行操作,但结果不会按预期显示。

我遵循了以下tutorial 的每一步。如果您想追溯我的工作,这可能很有用。我在 Visual Studio 2019 中工作,下面我粘贴了 WCF 服务和 Web 表单的代码。

//WCF service file defining the operation
namespace SandwichServices
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class CostService
    {
        [OperationContract]
        public double CostOfSandwiches(int quantity)
        {
            return 1.25 * quantity;
        }
    }
}
//.aspx file for the web form
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="SandwichServices.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">

        function Calculate() {
            CostService.CostOfSandwiches(3, onSuccess);
        }

        function onSuccess(result) {
            var myres = $get("additionResult");
            myres.innerHTML = result;
        }

    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
        </div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
            <Services>
                <asp:ServiceReference Path="~/CostService.svc" />
            </Services>
        </asp:ScriptManager>

        <input type="button" value="Price of 3 sandwiches" onclick="Calculate()" />
        <br />
        <span id="additionResult">
        <br />
        </span>
    </form>
</body>
</html>

调试时页面正常生成按钮,但是点击按钮应该会显示3.75的结果。相反,不显示任何结果。查看页面源代码并检查控制台会在单击按钮时显示以下错误:“SCRIPT5009:'CostService' 未定义”。对于解决此问题的任何帮助将不胜感激。

【问题讨论】:

  • 试试这个:document.getElementById("additionResult");而不是 $get("additionResult");
  • 我继续试了一下,调试时没有任何变化。
  • 检查您的浏览器调试器。看看你的javascript是否有错误。
  • 是的,正如@BlueEyedBehemoth 所建议的那样,找出答案的最佳方法是调试您的代码。试试这个教程来帮助你:developers.google.com/web/tools/chrome-devtools/javascript
  • 看起来问题出在Calculate函数上——如前所述,所涉及的错误是“未定义CostService”。不幸的是,我似乎找不到任何方法来解决这个问题。

标签: c# ajax wcf


【解决方案1】:

在将 CostService.svc 项添加到项目时,请确保配置文件 (web.config) 中有正确的 System.servicemodel 部分。

  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="WebApplication1.CostServiceAspNetAjaxBehavior">
          <enableWebScript />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
      multipleSiteBindingsEnabled="true" />
    <services>
      <service name="WebApplication1.CostService">
        <endpoint address="" behaviorConfiguration="WebApplication1.CostServiceAspNetAjaxBehavior"
          binding="webHttpBinding" contract="WebApplication1.CostService" />
      </service>
    </services>
  </system.serviceModel>

请使用实用的程序集/命名空间来替换上述属性。在我这边,我创建了一个 WebApplicaiton1 项目,默认命名空间是 WebApplicaiton1。另外,CostService.svcWebForm3.aspx都在项目的根目录下。
如果有什么可以帮助的,请随时告诉我。
已更新。
最近,我发现这个问题是一个错误。这是由于 IIS 中的无扩展 URL 处理程序导致 Web 服务无法识别。
https://support.microsoft.com/zh-cn/help/2520479/web-services-may-fail-on-microsoft-internet-information-services-iis-7
此外,这是一种解决方法。我们将相对服务地址替换为完整的服务地址(基于托管环境)。

<asp:ScriptManager ID="ScriptManager1" runat="server">
        <Services>
            <asp:ServiceReference Path="http://localhost:56697/Content/CostService.svc" />
        </Services>
    </asp:ScriptManager>

【讨论】:

  • 感谢您的回答,但是尽管实施了您的解决方案,但我仍然在浏览器调试器中遇到“未定义 CostService”。还有什么我可以尝试的吗?
  • 您能告诉我更多有关操作系统版本、项目目标框架、项目模板类型、用于调试的浏览器版本的详细信息吗?
  • 操作系统:Microsoft Windows 10 Pro 版本 10.0.18362 目标框架:.NET Framework 4.7.2 项目模板:空的 ASP.Net Web 应用程序调试工具:IIS Express (Microsoft Edge)
  • 兄弟,我找不到问题所在。相同的代码在我这边运行良好。运行环境基本相同。我对此感到抱歉。我建议您在另一台计算机上运行您的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-20
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
  • 2011-08-19
  • 1970-01-01
相关资源
最近更新 更多