0

我正在使用 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 - 错误请求

非常感谢特拉维斯

4

2 回答 2

2

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

  <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)现在试试这个浏览。

希望这可以帮助。

于 2012-10-26T02:19:15.167 回答
0

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

于 2012-11-16T11:23:57.793 回答