c# - 如何通过客户端显示 WCF 服务的结果
问题描述
我一直在关注有关设置启用 AJAX 的 WCF 服务并使用客户端访问它们的 Microsoft 教程。但是,尽管完全按照教程进行操作,但结果不会按预期显示。
我已按照以下教程中的每一步进行操作。如果您想追溯我的工作,这可能很有用。我在 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' 未定义”。解决此问题的任何帮助将不胜感激。
解决方案
将 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.svc和WebForm3.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>
推荐阅读
- tensorflow - keras 中的预训练对象检测模型
- swift - 故事板分解
- tcl - ITCL:如何显示条目中的值?
- java - 从嵌套同级模块导入时出现 Maven 编译错误
- python - 如何一次性预处理 NLP 文本(小写、删除特殊字符、删除数字、删除电子邮件等)?
- php - 如何在laravel中设置用户管理员
- c# - 如何检查单击事件处理程序单击了哪个控件
- vb6 - VB6 无法向项目添加新表单
- sql - 如何使用 SQL 从数据库中获取一条记录并获取该记录的所有警告?
- css - GET http://localhost:8080/static/css/1.e8747c0d.chunk.css net::ERR_ABORTED 404