使用控制台应用程序测试WCF Web服务会返回错误(application / xop + xml”与预期的类型“ text / html; charset = UTF-8”不匹配)

时间:2019-07-16 08:20:34

标签: c#

我创建一个控制台应用程序来测试WCF Web服务。为了测试我的服务,我添加了带有我的WSDL(https://myservices.fr/Connectors/TokenConnector/ServiceToken.svc?wsdl)URL的参考服务。

我在main方法中放入了获取新令牌的代码,但是我遇到了错误:

  

application / xop + xml”与预期的类型“ text / html; charset = UTF-8”不匹配,iscontenttypesupported方法已正确实现。

您有什么想法要解决吗?

    ChannelFactory<IServiceTokenChannel> factory = new ChannelFactory<IServiceTokenChannel>("BasicHttpBinding_IServiceToken");
    factory.Open();
    IServiceTokenChannel wcfClientChannel = factory.CreateChannel();
    // Making calls.
    Console.WriteLine("Le service return: " + 
wcfClientChannel.getToken("myLogin", "mypassword", "myEmail")); //Error occurs here.

2 个答案:

答案 0 :(得分:1)

尝试在配置文件中使用mtomMessageEncoding

<configuration>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding messageEncoding="Mtom">          
        </binding>
      </basicHttpBinding>
    </bindings>
  </system.serviceModel>
</configuration>

答案 1 :(得分:0)

经过3天的奋斗,终于找到了解决我问题的方法。发生错误是因为我没有为我的呼叫请求定义证书。实际上,为了编程Windows Communication Foundation(WCF)安全性,通常使用X.509数字证书来认证客户端和服务器,对消息进行加密和数字签名(更多详细信息:Working with Certificates)。 综上所述,如果要使用控制台应用程序基于https协议测试WCF Web服务,可以按照以下步骤操作:

  1. 在Visual Studio中创建控制台应用程序。
  2. 添加新引用(System.ServiceModel)
  3. 添加新的参考服务(定义 您的网络服务,例如: https://myservices.fr/Connectors/TokenConnector/ServiceToken.svc
  4. 在programm类中,将其调整为适合您的情况后粘贴以下代码:

        static void Main(string[] args)
        {
            //instantiate the class ServiceTokenClient wish provide the getToken method with endpoint configuration name and remote adress. 
            //look at web.config or app.config to get the name of endpoint configuration <binding name="BasicHttpBinding_IServiceToken" />
            //https://myservices.fr/Connectors/TokenConnector/ServiceToken.svc  is the URL of our web services provided by the host.
    
            ServiceTokenClient _srvToken = new ServiceTokenClient("BasicHttpBinding_IServiceToken", "https://myservices.fr/Connectors/TokenConnector/ServiceToken.svc");
    
            //Set a certificate
            // Use the X509Store class to get a handle to the local certificate stores. "My" is the "Personal" store. Don't forget using System.Security.Cryptography and System.Security.Cryptography.X509Certificates;
            _srvToken.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.CurrentUser, StoreName.My, X509FindType.FindBySerialNumber, "XXXXXXXXXXXXXXXXXXXXXXXXXXX");
            ((BasicHttpBinding)_srvToken.Endpoint.Binding).Security.Mode = BasicHttpSecurityMode.Transport; //set the security mode
            ((BasicHttpBinding)_srvToken.Endpoint.Binding).Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate; //set the type of client credential.
    
            //Making Call
            var _objToken = _srvToken.getToken("myLogin", "mypassword", "myEmail");
            Console.WriteLine("{0}", _objToken.Token);  // Display the token
            Console.ReadKey();
    
        }