2012-10-26 82 views
0

我正在使用WCF服务并收到错误消息“远程服务器返回错误:(400)错误请求”。通过Web客户端消耗时正在通过webclient调用WCF方法 - HTTP 400 - 错误请求

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 - 错误的请求

非常感谢 特拉维斯

回答

0

的问题完全是我的错 - 我是在“邮报”的请求,其收缩为“取”请求

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

希望这会有所帮助。

+0

感谢/帮助提示Swani上述服务 - 这有助于解决问题 –

+0

@ TravisIngram,你欢迎:)。我没有注意到“发布”和“获取”:) –