【问题标题】:WCF method being called via webclient - HTTP 400 - Bad Request通过 webclient 调用 WCF 方法 - HTTP 400 - 错误请求
【发布时间】:2012-10-26 01:26:23
【问题描述】:

我正在使用 WCF 服务并收到错误消息“远程服务器返回错误:(400) 错误请求。”通过 WebClient 消费时

public class WebClientService : WebClient
            ...
            base.Credentials = CredentialCache.DefaultCredentials;
            base.OpenReadCompleted += new OpenReadCompletedEventHandler(ReadJsonStringAsyncComplete);
            base.OpenReadAsync(new Uri("http://localhost/xxxx.svc/GetData"));
            ...

要消费的WCF服务如下(注意两者都存在于同一个解决方案中(不同的项目)

合约/接口:-

[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "/GetData", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
IList<MyType> GetData();

实现:-

public IList<MyType> GetData()
{
    return (new MyTypeRepository()).All.ToList();
}

配置:-

<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<services>
  <service name="xxxx.Service.MyService" behaviorConfiguration="DataExtractBehavior">
    <endpoint address="http://localhost:1422/MyService.svc" binding="webHttpBinding" bindingConfiguration="DataExtractBinding" behaviorConfiguration="MyEndpointBehavior" contract="xxxx.Service.Common.IMyService"></endpoint>
  </service>
</services>
<behaviors>
  <endpointBehaviors>
    <behavior name="MyEndpointBehavior">
      <webHttp />
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="Default">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
      <serviceAuthorization principalPermissionMode="UseAspNetRoles" />
      <serviceCredentials>
        <windowsAuthentication allowAnonymousLogons="false" includeWindowsGroups="true" />
      </serviceCredentials>
    </behavior>
    <behavior name="DataExtractBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
      <etwTracking profileName="EndToEndMonitoring Tracking Profile" />
    </behavior>
    <behavior name="">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
      <etwTracking profileName="EndToEndMonitoring Tracking Profile" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<bindings>
  <webHttpBinding>
    <binding name="DataExtractBinding">
      <security mode="TransportCredentialOnly">
        <transport clientCredentialType="Windows"></transport>
      </security>
    </binding>
  </webHttpBinding>
</bindings>

我可以通过 http://localhost:1422/xxxx.svc 查看服务定义页面,但是我似乎无法调用 GetData 方法(添加到 URI 的末尾 - http://localhost:1422/xxxx.svc/GetData

有人知道为什么我会收到 HTTP 400 - 错误请求

非常感谢 特拉维斯

【问题讨论】:

    标签: wcf uri webclient


    【解决方案1】:

    相同的代码对我来说工作正常。如下更改您的配置设置,

      <endpointBehaviors>
        <behavior name="MyEndpointBehavior">
            <webHttp helpEnabled="true" automaticFormatSelectionEnabled="true" />
        </behavior>
      </endpointBehaviors>
    

    并用http://localhost:1422/xxxx.svc/help浏览你的服务你可以GetData操作。现在试试http://localhost:1422/xxxx.svc/GetData,它会起作用的。

    另外,如果是这样,您的应用程序正在另一个端口(1422 以外)上运行,请转到您的应用程序(项目)-> 属性->Web-> 特定端口(输入 1422)和现在试试这个浏览器。

    希望这会有所帮助。

    【讨论】:

    • 感谢 /help 提示 Swani - 这有助于解决问题
    • @TravisIngram,欢迎您:)。我没有注意到“发布”和“获取”:)
    【解决方案2】:

    问题完全是我的错 - 我正在向上述服务发出“发布”请求,该请求被约定为“获取”请求

    【讨论】:

      猜你喜欢
      • 2015-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-26
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      相关资源
      最近更新 更多