使用[webHttpBinding]不允许405方法

时间:2011-05-25 07:14:41

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

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

-Interface

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; 使用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; 使用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; 使用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但它没有效果。请建议我任何解决方案。

提前致谢!

2 个答案:

答案 0 :(得分:1)

我可以在您的服务中看到一些问题:

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

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

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

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

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

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

答案 1 :(得分:-1)

在您的服务中尝试此操作:

[ScriptMethod]
public Contact GetContact(...)
相关问题