【问题标题】:How to call a WCF service from client script using ajax?如何使用 ajax 从客户端脚本调用 WCF 服务?
【发布时间】:2014-10-04 03:20:19
【问题描述】:

我的服务合约,命名为IService1,如下

    using System.ServiceModel;
    using System.ServiceModel.Web;

    namespace WcfServiceDemo
    {
        [ServiceContract]
        public interface IService1
        {
            [OperationContract]
            [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json)]
            string GetData(int Value);
        }
    }

这是我的服务实现,

    using System.ServiceModel.Activation;

    namespace WcfServiceDemo
    {
        [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
        public class Service1 : IService1
        {
            public string GetData(int Value)
            {
                return string.Format("You entered: {0}", Value);
            }

        }
    }

我的 web.config,

    <?xml version="1.0"?>
    <configuration>

      <system.web>
        <compilation debug="true" targetFramework="4.0" />
      </system.web>
      <system.serviceModel>

        <behaviors>
          <serviceBehaviors>
            <behavior name ="ServiceBehavior">
              <!-- To avoid disclosing metadata information, set the value below to false before deployment -->
              <serviceMetadata httpGetEnabled="true"/>
              <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
              <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
          </serviceBehaviors>
          <endpointBehaviors>
            <behavior name="EndPointBehavior">
              <enableWebScript />
            </behavior>
          </endpointBehaviors>
        </behaviors>

        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

        <services>
          <service name ="WcfDemoService" behaviorConfiguration="ServiceBehavior">
            <endpoint address="" binding="webHttpBinding" contract="IService1" behaviorConfiguration="EndPointBehavior" />
          </service>
        </services>

      </system.serviceModel>
     <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
        <!--
            To browse web app root directory during debugging, set the value below to true.
            Set to false before deployment to avoid disclosing web app folder information.
          -->
        <directoryBrowse enabled="true"/>
      </system.webServer>

    </configuration>

使用 WCF 服务的脚本,

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Client.aspx.cs"       Inherits="WcfServiceDemo.Client" %>

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            Enter a number:<input type="text" id="txtNum"/>
            <input type="button" onclick="AlertEnteredNum();"/>
        </div>
        </form>
        <script type="text/javascript" src="Script/jquery-1.3.2.min.js"></script>
        <script type="text/javascript">
            function AlertEnteredNum() {
                var Value = $('#txtNum').val();
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: 'Service1.svc/GetData',
                    data: '{"Value": "' + Value + '"}',
                    dataType: "json",
                    processData: false,
                    success: function (data) {
                        alert("Success: " + data.d);
                    },
                    error: function (result) {
                        alert("error: " + result);
                    }
                });
            }
        </script>
    </body>
    </html>

总是错误回调被执行。有人可以告诉我我在这里缺少什么吗?

我想我需要在 IIS 上托管,因为我在我的配置中设置了兼容模式。但, 当我在 IIS 上托管此服务时,我收到解析器错误为“无法识别的属性 'targetFramework'。请注意,属性名称区分大小写。”

而且,如果我删除这个“targetFramework = 4”属性,我会收到一个异常“无法加载文件或程序集‘WcfServiceDemo’或其依赖项之一。此程序集由比当前加载的运行时更新的运行时构建运行时,无法加载。”

【问题讨论】:

  • 尝试数据:{Value:$('#txtNum').val()}
  • 试过了,还是不行。还有其他问题……

标签: asp.net ajax wcf


【解决方案1】:

我的错误终于解决了。

只需将配置中的服务元素更改为:

<services>
  <service name ="WcfServiceDemo.Service1" behaviorConfiguration="ServiceBehavior">
    <endpoint address="" binding="webHttpBinding" contract="WcfServiceDemo.IService1" behaviorConfiguration="EndPointBehavior" />
  </service>
</services>

其中 name ="WcfServiceDemo.Service1" 的格式为“namespace.ServiceImplementationName”,而contract="WcfServiceDemo.IService1" 的格式为“namespace.ServiceContractName”

【讨论】:

    【解决方案2】:

    针对 TargetFramework 问题。 您应检查您的应用程序池是 .net4.0 而不是 .net2.0

    javascript 似乎是正确的

    【讨论】:

    • 是的,我的应用程序池是“.net2.0”。但将其更改为“.net4.0”后,我收到不同的解析器错误,因为“targetFramework 属性的值无效:'4'。错误:FrameworkName 不能少于两个组件或多于三个组件。参数名称: 框架名称。”
    • 是的,它只是 4.0,就像 但我不明白,为什么我会得到这样的错误信息
    猜你喜欢
    • 2012-11-27
    • 1970-01-01
    • 1970-01-01
    • 2016-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多