使用客户端 Web 服务时出现内容 mismtach 错误

问题描述:

我在使用客户端 Web 服务时遇到以下错误.

I am getting following error while I am consuming client web service.

错误:响应消息的内容类型 text/xml;charset=utf-8 与绑定的内容类型(application/soap+msbin1+gzip)不匹配.如果使用自定义编码器,请确保正确实现 IsContentTypeSupported 方法.

Error: The content type text/xml;charset=utf-8 of the response message does not match the content type of the binding (application/soap+msbin1+gzip). If using a custom encoder, be sure that the IsContentTypeSupported method is implemented properly.

响应的前 807 个字节是:

The first 807 bytes of the response were:

'肥皂环境:客户端必须使用 HTTP 压缩 (RFC 1952 - GZIP) 发送请求消息.请查看位于 https://www.irs.gov/for-Tax-Pros/Software-Developers/Information-Returns/Affordable-Care-Act-Information-返回-AIR-Program,更正任何问题,然后重试.TPE1112'.========================错误:System.Net.WebException:远程服务器返回错误:(500) 内部服务器错误.在 System.Net.HttpWebRequest.GetResponse()在 System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)

' soapenv:Client The request message must be sent using HTTP compression (RFC 1952 - GZIP). Please review the transmission instructions outlined in Section 5 of the AIR Submission Composition and Reference Guide located at https://www.irs.gov/for-Tax-Pros/Software-Developers/Information-Returns/Affordable-Care-Act-Information-Return-AIR-Program, correct any issues, and try again. TPE1112 '.==========================Error: System.Net.WebException: The remote server returned an error: (500) Internal Server Error. at System.Net.HttpWebRequest.GetResponse() at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)

我知道有很多关于此错误的文章,但我无法获得解决方案或提示,如何解决此问题?

I know there are many articles are available about this error but I am unable to get solution or hint, how to solve this issue?

基本上,客户端提供了 WSDL 文件,我已将其作为服务引用"添加到我的控制台应用程序中.

Basically Client has provided WSDL file and I have added it as "Service References" into my console application.

这是我的配置文件

<bindings>
  <customBinding>
    <binding name="BulkRequestTransmitterBinding"  >
      <binaryMessageEncoding compressionFormat="GZip" />
      <httpsTransport/>
    </binding>
  </customBinding>
</bindings>
<client>
  <endpoint address="MYENDPOINTURL"
      binding="customBinding" 
      bindingConfiguration="BulkRequestTransmitterBinding"
      contract="CONTRACTNAME" 
      name="BulkRequestTransmitterPort" />
</client>

要开始,您需要下载 这个来自 Microsoft 的示例编码器

设置它并将其作为项目添加到您的解决方案中.之后,您需要进行一些调整.

Set it up and add it as a project to your solution. Afterwards you need to make some tweaks.

  1. 首先,您需要将内容类型更改为text/xml"而不是application/x-gzip".通过改变

  1. First you need to change the content type to be"text/xml" instead of "application/x-gzip". by changing

class GZipMessageEncoder : MessageEncoder
{
static string GZipContentType = "application/x-gzip";

static string GZipContentType = "text/xml";

  • 在传输之前,您还需要更改内容编码标头,这篇博文对其进行了更详细的描述,但代码在这里:

    using (new OperationContextScope(transmitter.InnerChannel))
    {
    // Add a HTTP Header to an outgoing request
    HttpRequestMessageProperty requestMessage = new HttpRequestMessageProperty();
    requestMessage.Headers["Content-Encoding"] = "gzip";
    OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = requestMessage;
    
    var response = transmitter.BulkRequestTransmitter(
        securityHeader,
        security,
        ref businessHeader,
        transmitterManifestReqDtl,
        transmitterType);
    ParseSubmitResponse(response);
    }
    

  • 美国国税局在要求您发送压缩的所有内容的同时,以未压缩的格式进行响应(至少到发送器部分,谁知道他们决定对状态部分做什么),因此需要禁用解压缩例程,它位于

  • The IRS, while demanding you send everything compressed, responds in uncompressed format (at least to the transmitter section, who knows what they decided to do with the status portion) so the decompression routine needs to be disabled, it is located in

    ZipEncoder.CZipMessageEncoderFactory.GZipMessageEncoder.DecompressBuffer
    

    你只需要注释掉using语句并交换

    You just need to comment out the using statement and swap

    int bytesRead = gzStream.Read(tempBuffer, 0, blockSize);
    

    int bytesRead = memoryStream.Read(tempBuffer, 0, blockSize);
    

  • 接下来,您需要更改内部消息编码消息版本以与 IRS 兼容.所以在

  • Next, You need to change the inner message encoding message version to be compatible with the IRS. So in

    GZipEncoder.GZipMessageEncodingElement.ApplyConfiguration
    

    您需要将 TextMessageEncodingBindingElement 的构造函数更改为:

    You need to change the constructor for TextMessageEncodingBindingElement to this:

    binding.InnerMessageEncodingBindingElement =
        new TextMessageEncodingBindingElement(MessageVersion.Soap11WSAddressing10, Encoding.UTF8);
    

  • <system.serviceModel>
      <extensions>
        <bindingElementExtensions>
          <add name="gzipMessageEncoding" type="GZipEncoder.GZipMessageEncodingElement, GZipEncoder, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null" />
        </bindingElementExtensions>
      </extensions>
    

  • 现在你可以让你的绑定看起来像这样:

  • Now you can make your binding look like this:

    <binding name="BulkRequestTransmitterBinding">
      <gzipMessageEncoding innerMessageEncoding="textMessageEncoding" />
      <httpsTransport />
    </binding>
    

  • 这样,您的代码应该可以成功传输到 IRS.

    With that, your code should successfully transmit to the IRS.