【问题标题】:Calling a REST service through Azure Service Bus: 405 method not allowed通过 Azure 服务总线调用 REST 服务:不允许 405 方法
【发布时间】:2015-03-02 19:45:54
【问题描述】:

我正在尝试使用 Windows Azure 服务总线创建连接到 REST 服务的 Windows Phone。

我开发了我的 rest 服务(它是一个 Windows 服务),现在我使用 fiddler 来测试它。

但我不断收到错误“405 方法不允许”。

我注意到的事情:

  • 我的服务在线,因为当我关闭它时,fiddler 又返回一个错误。

  • 我的 Servicebus 已启动并正在运行,我可以在 Azure 门户上看到它。

这是我试图在 C# 中调用的操作

[WebInvoke(
//UriTemplate = "GetEmployees?Action=Get", // URI:"https://<MyBus>.servicebus.windows.net/GetEmployeesRest/GetEmployees?Action=Create"
UriTemplate = "GetEmployees?Action=Create", 
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest)]

public DEVEmployee[] GetEmployees()
{

}

我在app.config中配置的url是:

<baseAddresses>
<add baseAddress="https://<MyBus>.servicebus.windows.net/GetEmployeesRest/"/>
</baseAddresses>

我这样称呼它是:

https://.servicebus.windows.net/GetEmployeesRest/GetEmployees?Action=Create

带有授权标头。

有人知道我做错了吗?

提前致谢。

【问题讨论】:

  • 您在使用服务总线中继吗?
  • 是的,我正在使用服务总线中继。我可以在 azure 门户中看到我的服务的巴士。但它总是返回 405。

标签: rest azure servicebus


【解决方案1】:

默认情况下,WebInvoke 使用 POST 请求(WebGet 使用 GET 请求)。如果您的客户在您的示例中使用 POST 请求以外的任何方法调用 GetEmployees 方法,则服务将以 405 响应。

可以像这样覆盖默认的 HTTP 方法:

[OperationContract]
[WebInvoke(Method="PUT", UriTemplate = "?Action:Create",
  RequestFormat = WebMessageFormat.Json,
  ResponseFormat = WebMessageFormat.Json,
  BodyStyle = WebMessageBodyStyle.WrappedRequest)]
void CreateEmployee(string id, string name);

如果您使用 Fiddler 进行测试,那么 Composer 允许您在提交请求时选择要使用的方法。

如果您想从 Fiddler 进行测试而不需要包含授权标头,您可以将此配置用于您的服务:

<bindings>
  <webHttpRelayBinding>
    <binding name="default">
      <security relayClientAuthenticationType="None" />
    </binding>
  </webHttpRelayBinding>
</bindings>

【讨论】:

    猜你喜欢
    • 2017-11-18
    • 2016-09-23
    • 2017-08-16
    • 2014-07-17
    • 1970-01-01
    • 1970-01-01
    • 2016-06-13
    • 2011-01-13
    • 2017-12-03
    相关资源
    最近更新 更多