我是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>
答案 0 :(得分: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>