【问题标题】:405 method not allowed using [webHttpBinding]405 方法不允许使用 [webHttpBinding]
【发布时间】:2011-09-01 12:35:34
【问题描述】:

我在使用 jquery 调用 wcf 时遇到问题。我已经搜索并找到了一些解决方案,但它仍然给我一个错误“405 方法不允许”。下面是可能的代码

-接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.ServiceModel;

namespace WcfServiceLibrary
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IContactService" in both code and config file together.
    [ServiceContract]
    public interface IContactService
    {
        [WebInvoke(Method = "GET",
         ResponseFormat = WebMessageFormat.Json)]
        [OperationContract]
        Contact GetContactInfo();
    }
}
  • 我的服务

    使用系统; 使用 System.Collections.Generic; 使用 System.Linq; 使用 System.Runtime.Serialization; 使用 System.ServiceModel; 使用 System.Text; 使用 System.ServiceModel.Activation; 使用 System.ServiceModel.Web; 使用 System.ServiceModel;

    命名空间 WcfServiceLibrary { // 注意:您可以使用“重构”菜单上的“重命名”命令将代码和配置文件中的类名“ContactService”一起更改。

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ContactService : IContactService
    {
    
        public Contact GetContactInfo()
        {
            ContactBL contactBL = new ContactBL();
    
            return contactBL.GetContact();
        }
    
    }
    

    }

  • 我的对象

使用系统; 使用 System.Collections.Generic; 使用 System.Linq; 使用 System.Text; 使用 System.Runtime; 使用 System.Runtime.Serialization;

namespace WcfServiceLibrary
{
    [DataContract]
    public class Contact
    {
        [DataMember]
        public int Id {get;set;}
        [DataMember]
        public string Fullname {get; set;}
        [DataMember]
        public string email { get; set; }
    }
}
  • BL

使用系统; 使用 System.Collections.Generic; 使用 System.Linq; 使用 System.Text;

namespace WcfServiceLibrary
{
    class ContactBL
    {
        public ContactBL() { }
        public Contact GetContact()
        {
            return new Contact {email="thang.nguyen@saas.com.vn", Fullname="NVTThang",Id=2 };
        }
    }
}

还有我的 WCF 配置:

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
  </system.web>
  <system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding name="webBinding">
          <security mode="None" />
        </binding>
      </webHttpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="myBehavior" name="WcfServiceLibrary.ContactService">
        <endpoint address="ajaxEp" behaviorConfiguration="epAjaxBehavior"
          binding="webHttpBinding" bindingConfiguration="webBinding" name="epWebHttp"
          contract="WcfServiceLibrary.IContactService" />
        <endpoint address="mex" binding="mexHttpBinding" name="epMex"
          contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="epAjaxBehavior">
          <webHttp />
          <enableWebScript/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="myBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true">
      <baseAddressPrefixFilters>
        <add prefix="http://localhost"/>
      </baseAddressPrefixFilters>
    </serviceHostingEnvironment>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

我在 IIS 7.5 上部署了我的 wcf,然后我使用 jquery ajax 调用我的服务创建了一个 Web 客户端。

$.ajax({
                type: "GET", 
                url: "http://localhost/WcfTestService/Service.svc/ajaxEp/GetContactInfo", 
                data: '',
                contentType: "application/json;charset=utf-8", 
                dataType: "json", 
                processdata: true,
                success: function (msg) {
                    alert(msg);
                    //ServiceSucceeded(msg);
                },
                error: ServiceFailed
            });

 function ServiceFailed(err){
            alert(err.responseText);

            return;
        }

当我调用我的服务时,它总是提示我“405 Method Not Allowed”,我尝试过 aspnet_regiis -i 和 ServiceModelReg -i 但没有效果。请建议我任何解决方案。

提前致谢!

【问题讨论】:

    标签: wcf iis-7.5 wcf-binding wcf-ria-services wcf-client


    【解决方案1】:

    我发现您的服务存在一些问题:

    对于“GET”请求,您应该在合同中使用 WebGet,而不是 WebInvoke:

    [ServiceContract]
    public interface IContactService
    {
        [WebGet(ResponseFormat = WebMessageFormat.Json)]
        [OperationContract]
        Contact GetContactInfo();
    }
    

    在您的端点行为中,您有 2 个脚本行为:webHttp 和 enableWebScript。你只需要前者。后者专门用于与 ASP.NET AJAX 集成。

    <endpointBehaviors>
      <behavior name="epAjaxBehavior">
        <webHttp />
      </behavior>
    </endpointBehaviors>
    

    这可能不会导致任何问题,但是在 $.ajax 调用中,您不需要指定内容类型,因为它是一个 GET 请求(因此没有内容)。此外,由于您没有传递任何参数,因此您也不需要 data 参数。

    $.ajax({
        type: "GET", 
        url: "http://localhost/WcfTestService/Service.svc/ajaxEp/GetContactInfo", 
        dataType: "json", 
        success: function (msg) {
            alert(msg);
            //ServiceSucceeded(msg);
        },
        error: ServiceFailed
    });
    

    【讨论】:

    • 我已经更改了我的配置以及我的 jquery 脚本来调用 wcf。但它向我显示空数据。这是我的脚本 $.get('localhost/WcfTestService/Service.svc/ajaxEp/GetContactInfo', function (data) { alert(data); });
    • 能否通过Fiddler(www.fiddlertool.com)运行一下,看看客户端向服务器发送了什么,服务器返回了什么?
    【解决方案2】:

    在您的服务中试试这个:

    [ScriptMethod]
    public Contact GetContact(...)
    

    【讨论】:

    • [ScriptMethod] 对于 WCF 服务不是必需的;该方法可由脚本通过使用适当的绑定 (webHttpBinding) 和行为 (webHttp) 调用
    • 他的代码存在一些问题,我在答案中指出了这些问题。 [ScriptMethod] 不会解决问题,因为它只在派生自 System.Web.Services.WebService 的类中使用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-29
    • 2013-03-13
    • 2016-03-08
    • 2014-09-19
    • 2016-04-20
    • 2019-03-01
    相关资源
    最近更新 更多