首页 > 解决方案 > C# ASMX Service throwing 内容类型 text/html; charset=UTF-8 的响应信息与内容类型不匹配错误

问题描述

ASMX通过右键单击根目录->添加服务引用,为我的项目添加了服务引用。

我的web.config文件中有这样的内容:

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="xxx" />
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="serviceaddress"
        binding="basicHttpBinding" bindingConfiguration="xxx"
        contract="xxx" name="xxx" />
    </client>
  </system.serviceModel>

该服务有一个方法可以接收string带有用户名的 a 并验证它是否存在。

问题是我正在对其进行测试,Postman它返回以下错误消息:

The content type text/html; charset=UTF-8 of the response message does not match the content type

我已经检查过与此类似的其他帖子,但我无法找到解决方案。

这是我调用的引发错误的方法:

public static List<UserInformation> GetUsersByUserName(string userName)
        {
            try
            {
                var usersServiceClient = new LDapServicesSoapClient();
                var requestMessage = new LDapUserNameLookupRequest();
                requestMessage.UserName = userName;
                requestMessage.AccessKey = "secretkey";
                var response = usersServiceClient.LDapGetUserByUserName(requestMessage);
                return response.Users.ToList();
            }
            catch (CommunicationException e)
            {
                if (e.InnerException is QuotaExceededException)
                {
                    throw new Exception("We have found many users, please write another filter");
                }
                else
                {
                    throw new Exception(e.Message, e);
                }
            }
        }

标签: c#web-servicesasmx

解决方案


将此配置添加到我的 web.config 文件中起到了神奇的作用:

 <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="LDapServicesSoap" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" allowCookies="false" bypassProxyOnLocal="false" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text">
          <security mode="Transport">
            <transport clientCredentialType="None" proxyCredentialType="None" realm="" />
            <message clientCredentialType="Certificate" algorithmSuite="Default" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="address"
        binding="basicHttpBinding" bindingConfiguration="LDapServicesSoap"
        contract="LDapServices.LDapServicesSoap" name="LDapServicesSoap" />
    </client>
  </system.serviceModel>

推荐阅读