2012-10-26 49 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,你歡迎:)。我沒有注意到「發佈」和「獲取」:) –