4 .net c# wcf soap web-services
我正在建立一个wcf客户,该客户使用来自巴西政府机构的服务。此连接使用Soap 1.2,并且需要使用数字证书进行签名。
本示例中使用的代码是使用.Net 4.6.1的控制台应用程序。主要应用程序是WPF应用程序(我不使用IIS)。该代码在Windows 10上可以正常工作,但是当我尝试在Windows 7上运行它时,出现以下错误:
System.ServiceModel.CommunicationException:向https://nfce-homologacao.svrs.rs.gov.br/ws/NfeStatusServico/NfeStatusServico2.asmx发出HTTP请求时发生错误。这可能是由于在HTTPS情况下未使用HTTP.SYS正确配置服务器证书。这也可能是由客户端和服务器之间的安全绑定不匹配引起的。---> System.Net.WebException:基础连接已关闭:发送时发生意外错误。---> System.IO.IOException:无法从传输连接中读取数据:远程主机强行关闭了现有连接。---> System.Net.Sockets.SocketException:现有连接被远程主机强行关闭。
这是客户呼叫代码:
XmlNode node = null;
var parametro = new TConsStatServ();
parametro.cUF = NFeAPI.XMLSchemas.NfeStatusServico2.Envio.TCodUfIBGE.Item53;
parametro.tpAmb = NFeAPI.XMLSchemas.NfeStatusServico2.Envio.TAmb.Item2;
parametro.versao = "3.10";
parametro.xServ = TConsStatServXServ.STATUS;
var certificate = GetCertificateByName("Certificate Name", false);
string nFeNamespaceName = "http://www.portalfiscal.inf.br/nfe";
string parametroXML = XmlUtil.Serialize(parametro, nFeNamespaceName);
XmlDocument doc = new XmlDocument();
XmlReader reader = XmlReader.Create(new StringReader(parametroXML));
reader.MoveToContent();
node = doc.ReadNode(reader);
nfeCabecMsg soapHeader = new nfeCabecMsg();
soapHeader.cUF = parametro.cUF.ToString().Replace("Item", "");
soapHeader.versaoDados = "3.10";
var soapClient = new NfeStatusServico2SoapClient("NfeStatusServico2Soap");
soapClient.ClientCredentials.ClientCertificate.Certificate = certificate;
XmlNode result = soapClient.nfeStatusServicoNF2(ref soapHeader, node);
Run Code Online (Sandbox Code Playgroud)
这是我的App.config:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="NfeStatusServico2Soap">
<security mode="Transport">
<transport clientCredentialType="Certificate"/>
</security>
</binding>
<binding name="NfeStatusServico2Soap1" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://nfce-homologacao.svrs.rs.gov.br/ws/NfeStatusServico/NfeStatusServico2.asmx"
binding="basicHttpBinding" bindingConfiguration="NfeStatusServico2Soap"
contract="NfeStatusServico2.NfeStatusServico2Soap" name="NfeStatusServico2Soap" />
</client>
</system.serviceModel>
Run Code Online (Sandbox Code Playgroud)
GetCertificateByName是我创建的帮助程序方法,用于返回服务需要的X509Certificate2。
我已经尝试禁用Windows 7防火墙,然后转到程序和功能->打开或关闭Windows功能,并为wcf调用启用.net 3框架节点。
我还尝试过将WebReference与.NET 2.0应用程序结合使用,但它给出了相同的错误。我将代码升级为在.net 4.6.1中使用wcf,以希望它能正常工作。
我试图使用提琴手来跟踪问题,它返回了代码200,但并没有太多帮助。
已经有5天了,我无法解决这个问题。因此,我将放弃对应用程序的Windows 7支持。
就我而言,问题是我的项目仍在使用.Net Framework 4.0,它不支持TLS 1.1或1.2,并且自2018年1月1日起,我连接的服务已关闭了对TLS 1.0的支持。将项目升级到.Net Framework 4.5并强制使用TLS 1.2,一切正常。
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11054 次 |
| 最近记录: |