【问题标题】:WCF Method returns 400 Bad Request on ProductionWCF 方法在生产中返回 400 Bad Request
【发布时间】:2020-06-01 13:14:13
【问题描述】:

WCF REST 服务的方法是使用 C# WCFClient 针对生产服务器。 他们还在本地主机上的开发环境中工作。 服务和 wsdl 也可以通过浏览器访问生产环境。

Access to svc

Access to wsdl

但是当我从 Production-Server 上的服务访问任何方法时,将返回 400 Bad Request。 IIS 日志也显示 400:GET /WCD/WCF.svc/GetNextGuestNumber - 443 - Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+ Chrome/83.0.4103.61+Safari/537.36 https://www.DS.XXX.com/qa/dLobby 400 0 0 46

这是我的生产环境的 web.config:

<?xml version="1.0"?>
  <configuration>  
    <appSettings>
      <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
      <add key="WBCCNN" value="PQkSAXkRXNsanbA5xK1KBZZqu+oxrJbssQlEiM1Fi+N2Vnz0F5QfAetqILb0QeLwlF7jMZ57k9J8sIlAJ1TRjtfgwh0V88Q2Kl/20Cny2WCTawWyDSECKg=="/>
    </appSettings>
    <system.web>  
      <compilation debug="false" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5"/>
    </system.web>  
    <system.serviceModel>  
      <services>  
        <service name="DS.XXX.Services.WCF" behaviorConfiguration="DSServiceBehavior">  
          <endpoint address=""  
              binding="basicHttpBinding"  
              bindingConfiguration="secureHttpBinding"  
              contract="DS.XXX.Services.IWCF"/>  
          <endpoint address="mex"  
              binding="mexHttpsBinding"  
              contract="IMetadataExchange" />  
        </service>  
      </services>  
      <bindings>  
        <basicHttpBinding>  
          <binding name="secureHttpBinding">  
            <security mode="Transport"> 
            </security>  
          </binding>  
        </basicHttpBinding>  
      </bindings>  
      <behaviors>  
        <serviceBehaviors>
          <behavior name="DSServiceBehavior">
            <serviceMetadata httpsGetEnabled="true" httpsGetUrl="" />
            <serviceDebug includeExceptionDetailInFaults="false"/>
          </behavior> 
        </serviceBehaviors>  
      </behaviors>  
      <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />  
    </system.serviceModel>  
    <system.webServer>  
      <modules runAllManagedModulesForAllRequests="true"/>  
    </system.webServer>  
  </configuration>

这些方法是从带有 Axios 的 REACT JS 应用程序调用的

const res = await API.get("/GetNextGuestNumber");

Axios 的 API 配置如下:

import axios from "axios";
import global from "../config";

export default axios.create({
  baseURL: global.webServicesUrl,
  headers: { "Content-Type": "application/json" }
});

由于服务在 dev 上运行,并且 svc 和 wsdl 在 prod 上是可访问的,所以它必须与绑定和端点配置有关?

我正在使用第三方供应商提供的证书,该证书也可以正常工作。 我没看到。任何帮助表示赞赏。如果您需要更多详细信息,请告诉我。

环境:Windows Server 2016、IIS

【问题讨论】:

    标签: wcf axios wcf-binding


    【解决方案1】:

    绑定有错误。您应该使用webhttpbinding来构建WCF REST Service,但是我在配置文件中看到您使用了basichttpbinding。在这种情况下,当您使用JS访问该服务时,您会收到400错误。

            <services>
            <service name="Demo_rest_IIS.Service1"
                     behaviorConfiguration="UserServiceBehavior">
                <endpoint address=""
                          binding="webHttpBinding"
                          contract="Demo_rest_IIS.IService1"
                          behaviorConfiguration="ESEndPointBehavior" bindingConfiguration="bind"/>
            </service>
        </services>
        <behaviors>
            <endpointBehaviors>
                <behavior name="ESEndPointBehavior">
                    <webHttp helpEnabled="true"/>
                </behavior>
            </endpointBehaviors>
            <serviceBehaviors>
                <behavior name="UserServiceBehavior">
                    <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
                </behavior>
            </serviceBehaviors>
        </behaviors>
    

    这是我在IIS中部署的WCF rest服务的配置文件。

        [OperationContract]
        [WebGet(UriTemplate = "user/?name={name}",RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        Result GetUserData(string name);
        [OperationContract]
        [WebInvoke(UriTemplate = "user", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        Result PostUserData(UserData user);
        [OperationContract]
        [WebInvoke(Method = "PUT", UriTemplate = "user", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        Result PutUserData(UserData user);
        [OperationContract]
        [WebInvoke(Method = "DELETE", UriTemplate = "user", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        Result DeleteUserData(UserData user);
    

    这是服务的接口。在配置文件中,我将helpenabled的值设置为true。部署服务后,我们使用帮助文档查看如何访问它。

    这是服务的帮助文档。

    有关 WCF REST 服务的更多信息,请参考以下链接:

    https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/wcf-web-http-programming-model

    【讨论】:

    • 您指向的是 http,但我使用的是 https。但无论如何,您关于 webHttpBinding 的提示为我指明了正确的方向。首先,我通过该设置获得了 500,但是通过向我的端点 () 添加了一个 behaviorConfiguration,我现在让它工作了。谢谢。这篇文章也感谢 Derek W:stackoverflow.com/questions/29246633/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-27
    • 2011-09-25
    • 2012-09-18
    • 2013-04-16
    • 2020-11-01
    相关资源
    最近更新 更多