【问题标题】:WCF Service Error 400 Bad Request In VS2017VS2017 中的 WCF 服务错误 400 错误请求
【发布时间】:2018-10-14 16:03:38
【问题描述】:

我在 VS2017 中有一个非常简单的 WCF 服务项目。但是当我尝试访问端点时,我不断收到错误 400。我已阅读此处发布的有关同一问题的其他问题,但到目前为止我已经尝试过,但没有成功。

我的服务合同:

[ServiceContract]
public interface IService
{
    [OperationContract]
    [WebInvoke(Method = "GET",
            BodyStyle = WebMessageBodyStyle.Wrapped,
            RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json,
            UriTemplate = "/GetData/{value}")]
    string GetData(string value);
}

我的服务:

public class Service : IService
{
    public string GetData(string value)
    {
        return string.Format("You entered: {0}", value);
    }
}

我的网络配置:

  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="myBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <services>
      <service behaviorConfiguration="myBehavior" name="WCGFService1.Service">
        <endpoint address="" contract="WCGFService1.IService" binding="webHttpBinding">
        </endpoint>
      </service>
    </services>

当我访问 http://localhost:61648/Service.svc/GetData/12 时,我收到 HTTP 400 错误请求。我试过浏览器和邮递员。我做错了什么?

我正在使用 VS2017。我的 IService.cs 和 Service.cs 位于 App_Code 文件夹中,而 Service.svc 位于根文件夹中。另外,当我尝试在 web.config 中添加 namecontract 时,VS2013 会建议我命名空间和接口/类名,而 VS2017 并没有建议我任何东西,所以我必须手动输入。

另外,在 VS2013 中,接口和类位于根文件夹中,而不是 App_Code 文件夹中。该项目是VS2017中的WCF应用程序。我的 .NET 版本是 4.5.2。

【问题讨论】:

    标签: wcf


    【解决方案1】:

    已修复,web.config 文件有一些问题:

    <system.serviceModel>
        <behaviors>
          <!-- Fix 1: Added a endpointBehavoir with webHttp -->
          <endpointBehaviors>
            <behavior name="web">
              <webHttp />
            </behavior>
          </endpointBehaviors>
          <serviceBehaviors>
            <behavior name="myBehavior">
              <serviceMetadata httpGetEnabled="true" />
            </behavior>
          </serviceBehaviors>
    
        </behaviors>
    
        <services>
          <!-- Fix 2: Instead of using the full name (WCGFService1.Service), used just the class name (Service) -->
          <service behaviorConfiguration="myBehavior" name="Service">
          <!-- Fix 3: Same thing here, used just the IService instead of full name, also, used the aforementioned endpointBehavoir -->
            <endpoint address="" contract="IService" binding="webHttpBinding" behaviorConfiguration="web">
            </endpoint>
          </service>
        </services>
    
        <protocolMapping>
            <add binding="basicHttpsBinding" scheme="https" />
        </protocolMapping>    
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
      </system.serviceModel>
    

    无论出于何种原因,这对我来说是这样的/接口名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-27
      • 2015-11-24
      • 2011-09-29
      • 2014-01-11
      • 1970-01-01
      • 1970-01-01
      • 2013-06-22
      • 2017-08-16
      相关资源
      最近更新 更多