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

标签 javascript c# asp.net wcf asp.net-ajax

我已按照以下链接在我的项目中创建支持 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/

相关文章:

c# - 向框中添加换行符

javascript - 轮播外的文本保持网站响应

javascript - 为什么 jsfiddle 会抛出函数未定义的错误?

c# - ElasticSearch NEST OR查询

c# - 如何一次性删除 Visual Studio 中当前文档中的所有 C# 方法/属性/字段 "summary"注释(以///开头)?

c# - Application_End() 无法通过 HttpContext.Current.Cache[key] 访问缓存

javascript - 我可以在 Node 6 中表示大整数,仅进行求幂而不具有依赖性吗?

javascript - 如何使用 angularjs 从对象数组中删除项目?

c# - 多个下拉框值发送到 Controller

javascript - 外部js文件问题