首页 > 解决方案 > 分配证书时 C# 对象是只读的

问题描述

我正在尝试调用 WSDL 文件提供的 Web 服务。

在将 WSDL 添加为服务引用并指向我的计算机上 WSDL 文件的位置之后。

使用代码我尝试将证书分配给 Web 服务,如下所示:

   NeqatyService.NeqatyWSAPIPortTypeClient mPortType = new NeqatyWSAPIPortTypeClient();

  Byte[] rawData = File.ReadAllBytes(@"c:\cert.p12");
                String cert64 = Convert.ToBase64String(rawData);
                X509Certificate2 certificates = new X509Certificate2(rawData, "pass", X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.UserKeySet);
                mPortType.ClientCredentials.ClientCertificate.Certificate = certificates;

调试器在此行引发错误:

mPortType.ClientCredentials.ClientCertificate.Certificate = certificates;

说明:对象是只读的

我想注意到,我曾经使用提供证书和密码的 SOAPUI 调用 Web 服务,并且它有效。

任何人都可以帮我解决这个问题。

标签: c#wsdlcertificate

解决方案


尝试在绑定配置中将 messageEncoding 的值修改为“Mtom”。这是我最近使用的配置示例。当值设置为“文本”时,我收到了该错误。

            <binding name="AWDProcessingServiceBinding" closeTimeout="00:01:00"
                openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                maxBufferSize="655360" maxBufferPoolSize="524288" maxReceivedMessageSize="655360"
                messageEncoding="Mtom" textEncoding="utf-8" transferMode="Buffered"
                useDefaultWebProxy="true">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <security mode="Transport">
                    <transport clientCredentialType="Certificate" proxyCredentialType="Basic"
                        realm="" />
                    <message clientCredentialType="UserName" algorithmSuite="Default" />
                </security>
            </binding>

推荐阅读