【问题标题】:Self hosted WCF (Rest) allows only POST method自托管 WCF (Rest) 仅允许 POST 方法
【发布时间】:2015-07-14 00:04:15
【问题描述】:

我的 WCF 服务是自托管的,我使用 WebServiceHost 进行托管,使用以下代码:

WcfGatewayDatas.cs

 public WcfGatewayDatas()
 {
     Uri baseAddress = new Uri("http://localhost:1478/");
     this.Host = new WebServiceHost(this, baseAddress);
     this.host.open();
 }

我的基本界面是这样的(不用担心新的):

IWcfGatewayDatas.cs

 [ServiceContract]
        public interface IWcfGatewayDatas : IExposeDatas
        {
            [WebGet(/*Method="GET", */UriTemplate = "Alarms/Last")]
            [OperationContract]
            new JaguarEvitech.AgentService.SpyAlarm GetLastAlarm();
        }

还有我的配置文件:

<system.serviceModel>
   <behaviors>
      <endpointBehaviors>
        <behavior name="Wcf">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <bindings>
      <basicHttpBinding>
        <binding name="WcfBinding"/>
      </basicHttpBinding>
    </bindings>
    <services>
      <service name="MilestonePlugin.Background.ExposeDatas.WcfGatewayDatas">
        <endpoint behaviorConfiguration="Wcf" binding="webHttpBinding" contract="MilestonePlugin.Background.ExposeDatas.IWcfGatewayDatas" address="GatewayJaguar"></endpoint>
      </service>
    </services>
  </system.serviceModel>

当服务启动时,我尝试使用我的网络浏览器访问 url (http://localhost:1478/GatewayJaguar/Alarms/Last),但我总是遇到相同的错误:不允许使用方法。唯一允许的一个是 post(无论我在我的界面 WebGet 或 WebInvoke 中写什么)。我错过了什么吗?

编辑:即使我的 url 与 WCF 函数不匹配,我也会遇到此错误。

【问题讨论】:

    标签: c# wcf http hosting


    【解决方案1】:

    WCF 默认只使用 HTTP Post。此链接说明如何选择使用 HTTP Get https://msdn.microsoft.com/en-us/library/bb628610(v=vs.110).aspx

    【讨论】:

    • 我已经将 WebGet 设置为我的函数以执行 GET 请求。我已经将 WebGetAttribute 添加到我的函数中
    • 您可以尝试 [WebGet(UriTemplate = "/Alarms/Last")] 在警报前有一个“/”
    【解决方案2】:

    好的,我发现了问题。问题是错误的 url 请求。我想请求http://localhost:1478/GatewayJaguar/Alarms/Last,但我的程序正在收听http://localhost:1478/Gatewayjaguar/GatewayJaguar/Alarms/Last。我更改了我的 uri,它现在可以工作了。

    【讨论】:

      猜你喜欢
      • 2015-11-20
      • 1970-01-01
      • 2011-10-01
      • 1970-01-01
      • 2018-07-21
      • 2021-04-05
      • 2013-09-06
      • 2012-07-10
      • 2019-10-27
      相关资源
      最近更新 更多