gpt4 book ai didi

javascript - 使用无法在 javascript 代码中访问的 AjaxEnabled WCF 服务

转载 作者:行者123 更新时间:2023-11-29 19:28:35 25 4
gpt4 key购买 nike

我已经按照下面的链接在我的项目中创建了一个支持 ajax 的服务 https://msdn.microsoft.com/en-us/library/bb924552(v=vs.110).aspx

我的 Asp.Net Web 窗体项目在不同的文件夹中有多个窗体。 admin 文件夹包含用于管理的表单。我从admin文件夹创建了wcf服务,代码如下

namespace App.secure.Admin
{
[ServiceContract(Namespace = "App.secure.Admin")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Admin
{
[OperationContract]
public string updateComp(string city, string desc )
{

在我的网络表单中,我无法添加单独的 ScriptManager,因为我已经在表单中添加了 ToolkitScriptManager。

我添加了如下服务引用

 <ajx:ToolkitScriptManager runat="server" ID="sm1">
<Services>
<asp:ServiceReference Path="Admin.svc" />
</Services>
</ajx:ToolkitScriptManager>

当我尝试调用 wcf 服务调用时,它无法实例化 wcf 服务中的类

 function UpdateStudent(e) {
city = $("#spnCity").text();
desc = $("#txtDescEd").val();
var service = new App.secure.Admin.Admin();
service.updateComp(city, desc);
}

当我运行应用程序时

Unhandled exception in http://localhost/secure/Admin/Add.aspx
0x800a1391 - JavaScript runtime error: 'App' is undefined

我曾尝试将 WCf 服务移动到根文件夹并尝试调用它,但它给出了同样的错误。

add.aspx.cs文件,我正在使用

namespace App.secure
{
public partial class Add : System.Web.UI.Page

web.config:

<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="App.AdminSvcAspNetAjaxBehavior">
<enableWebScript />
</behavior>
<behavior name="App.secure.Admin.AdminAspNetAjaxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
</behaviors> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<services>
<service name="App.AdminSvc">
<endpoint address="" behaviorConfiguration="App.AdminSvcAspNetAjaxBehavior" binding="webHttpBinding" contract="App.AdminSvc" />
</service>
<service name="App.secure.Admin.Admin">
<endpoint address="" behaviorConfiguration="App.secure.Admin.AdminAspNetAjaxBehavior" binding="webHttpBinding" contract="App.secure.Admin.Admin" />
</service>
</services>
</system.serviceModel>
</configuration>

谢谢

最佳答案

您需要添加服务行为和额外的 mex 端点才能获取 svc 元数据。

配置应该是这样的:

<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
<behaviors>
<endpointBehaviors>
<behavior name="AspNetAjaxBehavior">
<enableWebScript />
</behavior>
<behavior name="Call_WCF_WebService_REST_With_jQuery.service.AjaxServiceAspNetAjaxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="MyAjaxSvcBehaviour">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="Call_WCF_WebService_REST_With_jQuery.service.AjaxService" behaviorConfiguration="MyAjaxSvcBehaviour">
<endpoint address="" behaviorConfiguration="Call_WCF_WebService_REST_With_jQuery.service.AjaxServiceAspNetAjaxBehavior"
binding="webHttpBinding" contract="Call_WCF_WebService_REST_With_jQuery.service.AjaxService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>

HTML:

<script>
var svc = new AjaxService();
var res = svc.DoWork();
var x = 0;
</script>

服务:

namespace Call_WCF_WebService_REST_With_jQuery.service
{
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class AjaxService
{
// To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)
// To create an operation that returns XML,
// add [WebGet(ResponseFormat=WebMessageFormat.Xml)],
// and include the following line in the operation body:
// WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
[OperationContract]
public Person DoWork()
{
// Add your operation implementation here
return new Person() { Age = 12, Name = "Jo" };
}

// Add more operations here and mark them with [OperationContract]
}
}

关于javascript - 使用无法在 javascript 代码中访问的 AjaxEnabled WCF 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29638932/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com