[英]WCF REST service returns (400) Bad Request
我是WCF REST的新手,我創建了一個簡單的WCF應用程序並嘗試使用它,但我不斷收到錯誤消息:
遠程服務器返回錯誤:(400)錯誤的請求
我的方法:
[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>
您缺少帶有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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.