首页 > 解决方案 > 使用 WCF 端点时出现 400 错误请求响应

问题描述

尝试使用 HttpClient 向本地 iis 托管的 WCF 服务的端点发出请求时,我收到 400 Bad Request 响应。该调用在 .NET Core 微服务中实现,如下所示:

    private static readonly HttpClient client = new HttpClient();
    
    public async Task<ActionResponse> SomeAction(ActionRequest request, ShoppingCart shoppingCart) {

        //non-related code
 
        var requestObject = new WcfServiceRequest() {
            OrderData = shoppingCart.Order,
            VoucherCode = request.VoucherCode,
            Check = request.Check,
            Identifier = shoppingCart.Order.Identifier,
            DontGenerateNewOrdernumber = true
        };
    
        var requestContent = JsonConvert.SerializeObject(requestObject, settings);
        HttpResponseMessage serviceResponse = await client.PostAsync(new Uri("http://localhost/SomeApplicationServicePortal/Services/WcfPortalService.svc/web/SomeEndpoint"), 
        new StringContent(requestContent, Encoding.UTF8, "application/json"));

       //more non-related code

    }

另一方面,合同如下:

[DataContract]
public class WcfServiceRequest {
    [DataMember]
    public string Identifier { get; set; }
    [DataMember]
    public Order OrderData { get; set; }
    [DataMember]
    public Client ClientData { get; set; }
    [DataMember]
    public string VoucherCode { get; set; }
    [DataMember]
    public bool Check;
    [DataMember]
    public bool ExistingOrder;
    [DataMember]
    public bool DontGenerateNewOrdernumber { get; set; }
}

此刻我不知道该怎么做。如果有人帮助我解决这个问题,我将非常感激。

编辑:

其 web.config 文件中包含的 WCF serviceModel 如下:

 <system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="BasicBinding" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
      <security>
        <transport>
          <extendedProtectionPolicy policyEnforcement="Never" />
        </transport>
      </security>
    </binding>
  </basicHttpBinding>
  <webHttpBinding>
    <binding name="webHttpEndpointBinding" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
    </binding>
    <binding name="webHttpEndpointBindingSsl" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
      <security mode="Transport" />
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
    </binding>
  </webHttpBinding>
</bindings>
<behaviors>
  <serviceBehaviors>
    <behavior name="ApplicationManager.ServicePortal.Services.WcfPortalServiceBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="WebBehavior">
      <webHttp />
      <dataContractSerializer maxItemsInObjectGraph="2147483647" />
    </behavior>
  </endpointBehaviors>
</behaviors>
<services>
  <service behaviorConfiguration="ApplicationManager.ServicePortal.Services.WcfPortalServiceBehavior" name="ApplicationManager.ServicePortal.Services.WcfPortalService">
    <endpoint address="" binding="basicHttpBinding" bindingConfiguration="BasicBinding" contract="ApplicationManager.ServiceContracts.IApplicationManagerServicePortal">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
    <endpoint address="web" behaviorConfiguration="WebBehavior" binding="webHttpBinding" bindingConfiguration="webHttpEndpointBinding" contract="ApplicationManager.ServiceContracts.IApplicationManagerServicePortal" />
    <endpoint address="web" behaviorConfiguration="WebBehavior" binding="webHttpBinding" bindingConfiguration="webHttpEndpointBindingSsl" contract="ApplicationManager.ServiceContracts.IApplicationManagerServicePortal" />
  </service>
</services>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" /></system.serviceModel>

标签: c#wcf.net-core

解决方案


推荐阅读