2017-03-23 51 views
0

我是新來的WCF REST錯誤的請求,我創建了一個簡單的WCF應用程序,並嘗試使用它,但我不斷收到一個錯誤:WCF REST的服務回報(400)

The remote server returned an error: (400) Bad Request

我的方法:

[OperationContract] 
[WebGet(UriTemplate="mymethod")] 
string nameInput(); 

消耗通過使用此代碼:

string uri = "http://localhost:53551/HelloNameService.svc/mymethod"; 
req = (HttpWebRequest)WebRequest.Create(uri); 

try 
{ 
    resp = req.GetResponse() as HttpWebResponse; 
} 
catch(Exception ex) 
{ 
    Console.WriteLine(ex); 
} 

Stream stream = resp.GetResponseStream(); 
StreamReader reader = new StreamReader(stream); 

string value = reader.ReadToEnd(); 
label1.Text = value; 

的web.config:

<configuration> 
    <system.serviceModel> 
     <behaviors> 
      <serviceBehaviors> 
       <behavior name="mexBehaviors"> 
        <serviceMetadata httpGetEnabled="true"/> 
       </behavior> 
      </serviceBehaviors> 
     </behaviors> 
     <services> 
      <service name="MyServiceBecouseError.MyNameService" 
        behaviorConfiguration="mexBehaviors"> 
       <endpoint 
        address="" 
        binding="basicHttpBinding" 
        contract="MyServiceBecouseError.IMyNameService"/> 
      </service> 
     </services> 
    </system.serviceModel> 
    <system.webServer> 
     <directoryBrowse enabled="true"/> 
    </system.webServer> 
    <system.web> 
     <compilation debug="true"/> 
    </system.web> 
</configuration> 
+0

您的localhost:53551是否可以訪問? –

+0

@ThiyaguRajendran我怎麼知道? –

+0

嘗試導航到http:// localhost:53551/HelloNameService.svc。端點應公開服務元數據。 – forik

回答

0

您缺少webHttpBinding的端點。請使用以下配置,你應該很好。

<configuration> 
    <system.serviceModel> 
     <services> 
     <service behaviorConfiguration="MyServiceBehavior" name="MyService"> 
      <endpoint address="" binding="webHttpBinding" behaviorConfiguration="web" contract="MyServiceBecouseError.IMyNameService"/>     
      <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
     </service> 
    </services> 
    <behaviors> 
     <endpointBehaviors> 
     <behavior name="web"> 
      <webHttp /> 
     </behavior> 
     </endpointBehaviors> 
     <serviceBehaviors> 
     <behavior name="MyServiceBehavior"> 
      <serviceMetadata httpGetEnabled="true" /> 
      <serviceDebug includeExceptionDetailInFaults="false" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 
    </configuration>