【问题标题】:WCF REST service returns (400) Bad RequestWCF REST 服务返回 (400) 错误请求
【发布时间】:2017-08-16 02:27:59
【问题描述】:

我是 WCF REST 新手,我创建了一个简单的 WCF 应用程序并尝试使用它,但我不断收到错误消息:

远程服务器返回错误:(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>

【问题讨论】:

  • 你的 localhost:53551 可以访问吗?
  • @ThiyaguRajendran 我怎么知道??
  • 尝试导航到localhost:53551/HelloNameService.svc。端点应该公开服务元数据。
  • 请发布端点配置。
  • @Pankaj Kapare 我更新了我的 web.config 文件,请检查

标签: c# rest wcf


【解决方案1】:

您缺少带有 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-29
    • 1970-01-01
    • 1970-01-01
    • 2013-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多