我正在使用 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 - 错误请求
非常感谢特拉维斯