自托管WCF(Rest)仅允许POST方法

时间:2015-07-13 13:24:37

标签: c# wcf http hosting

我的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>

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

编辑:即使我的网址与WCF功能不匹配,我也有此错误。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

好的,我发现了问题。问题是网址请求不好。我想请求http://localhost:1478/GatewayJaguar/Alarms/Last,但我的计划正在监听http://localhost:1478/Gatewayjaguar/GatewayJaguar/Alarms/Last。我改变了我的uri,现在正在工作。