【问题标题】:Use JQuery AJAX to call WCF Service with multiple bindings使用 JQuery AJAX 调用具有多个绑定的 WCF 服务
【发布时间】:2013-08-23 23:08:01
【问题描述】:

我有一个 WCF 服务,它具有三个具有不同绑定的端点(soap 客户端的 basicHttpBinding、jquery ajax 客户端的 webHttpBinding 和 mex)。

我可以通过 SOAP 访问服务(然后添加服务引用):

        ServiceRef.IService service = new ServiceClient("BasicHttpBinding_IService");
        Response.Write(service.TestMethod("hi"));

但是为什么我从 Jquery Ajax 调用我得到一个 404 错误。

如果我将服务复制到一个测试项目中,只定义一个webHttpBinding,我可以通过jquery调用服务。

IService.cs

   [ServiceContract]
    public interface IService
    {
        [OperationContract]
        [WebInvoke(Method = "POST",
         ResponseFormat = WebMessageFormat.Json)]
         Response TestMethod(string Id);
....

Service.cs

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior]
public class Service : IService
{
    [OperationBehavior]
    public Response TestMethod(string Id)
    {
        Response resp = new Response();
        resp.Message = "hello";
        return resp;
    }
....

Web.Config

  <system.serviceModel>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="false" aspNetCompatibilityEnabled="true" />
<services>
  <service
   name="eBooks.Presentation.Wcf.Service"
   behaviorConfiguration="eBooks.Presentation.Wcf.ServiceBehavior">
    <!-- use base address provided by host -->
    <!-- specify BasicHttp binding and a binding configuration to use -->
    <endpoint address="soap"
              binding="basicHttpBinding"
              bindingConfiguration="Binding1"
              contract="eBooks.Presentation.Wcf.IService" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      <endpoint address="" binding="webHttpBinding"
          contract="eBooks.Presentation.Wcf.IService" behaviorConfiguration="EndpBehavior"/>
  </service>
</services>
<bindings>
  <!-- 
      Following is the expanded configuration section for a BasicHttpBinding.
      Each property is configured with the default value.
      See the TransportSecurity, and MessageSecurity samples in the
      Basic directory to learn how to configure these features.
      -->
  <basicHttpBinding>
    <binding name="Binding1"
             hostNameComparisonMode="StrongWildcard"
             receiveTimeout="00:10:00"
             sendTimeout="00:10:00"
             openTimeout="00:10:00"
             closeTimeout="00:10:00"
             maxReceivedMessageSize="65536"
             maxBufferSize="65536"
             maxBufferPoolSize="524288"
             transferMode="Buffered"
             messageEncoding="Text"
             textEncoding="utf-8"
             bypassProxyOnLocal="false"
             useDefaultWebProxy="true" >
      <security mode="None" />
    </binding>
  </basicHttpBinding>
</bindings>
<behaviors>
  <serviceBehaviors>
    <behavior name="eBooks.Presentation.Wcf.ServiceBehavior">
      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above 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="true"/>
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="EndpBehavior">
      <webHttp/>
    </behavior>
  </endpointBehaviors>
</behaviors>

jQuery

    function CallService() {
        $.ajax({
            type: "POST", //GET or POST or PUT or DELETE verb
            url: "http://localhost:888/Service.svc/TestMethod", // Location of the service
            data: '{"Id": "1"}', //Data sent to server
            contentType: "application/json; charset=utf-8", // content type sent to server
            dataType: "json", //Expected data format from server
            processdata: ProcessData, //True or False
            success: function (msg) {//On Successfull service call
                ServiceSucceeded(msg);
            },
            error: ServiceFailed// When Service call fails
        });
    }

在我的端点配置中,我将 webHttp 端点的地址设置为“”,因为我不知道从 jquery 调用时如何选择特定端点。

有人知道为什么我会收到此 404 错误吗?

【问题讨论】:

    标签: wcf jquery


    【解决方案1】:

    1) 确保您的 Service.svc 位于根目录中。

    2) 添加

    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json)]
    

    到您实例化您的 TestMethod 的地方。像这样:

    [OperationBehavior]
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json)]
    public Response TestMethod(string Id)
    {
        Response resp = new Response();
        resp.Message = "hello";
        return resp;
    }
    

    这些有帮助吗?

    【讨论】:

    • 是的,[WebInvoke] 可能不需要。但值得一试。
    猜你喜欢
    • 2010-11-14
    • 2015-04-27
    • 2014-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-05
    相关资源
    最近更新 更多