如何在微服务内的 C# 和 ASP.NET Core 6.0 中在运行时调用 WSDL 服务?
我尝试过
HttpClient
,其他解决方案,如通道工厂,但他们没有解决问题。
private static async Task LlamadaServicioAsync()
{
string wsdlUrl = "http://localhost/SCTSBJAX/ServiciosVEXJx?wsdl";
string operationName = "ConsultarSaldosCobros";
string soapAction = "http://localhost:7031/ConsultarSaldosCobros/";
string requestBody = "<cce:ConsultaCobroReq><sesionReq><sesionId>57301</sesionId><userId>8</userId><hashSesion>008B5AAD0643AE56C721CDF71AFE7D82A04F6C95031B650EA888FD7D512E9C95</hashSesion><codTran>1</codTran><codigoCanal>1</codigoCanal></sesionReq><servicioAdq><codigoEmpresaAdq>1</codigoEmpresaAdq><codigoAplicacionAdq>VEX</codigoAplicacionAdq><codigoServicioAdq>6</codigoServicioAdq><codigoCanalAdq>1201</codigoCanalAdq><referenciaAdquiriente>54214089</referenciaAdquiriente><fechaAdquiriente>2016-02-20T09:56:29-05:00</fechaAdquiriente><codigoComercio>0990858322001PG041</codigoComercio><codigoTerminal>123</codigoTerminal></servicioAdq><productoReq><codigoInstitucion>408</codigoInstitucion><codAplProducto>SER</codAplProducto><codigoProducto>386</codigoProducto><camposAdicionales>\"{\"campos\\\":[{\"codigo\":\"Tipo Identificacion\",\"valor\":\"CEDULA\",\"requerido\":\"M\"},{\"codigo\":\"Identificacion\",\"valor\":\"1304837162\",\"requerido\":\"M\"}]}\"</camposAdicionales></productoReq><codigoInstitucionAut>406</codigoInstitucionAut></cce:ConsultaCobroReq></cce:ConsultarSaldosCobro>";
await CallWebService(wsdlUrl, operationName, soapAction, requestBody);
}
public static async Task CallWebService(string wsdlUrl, string operationName, string soapAction, string requestBody)
{
// Download the WSDL file
string wsdl = await DownloadWsdl(wsdlUrl);
// Create a binding and endpoint address
BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress endpointAddress = new EndpointAddress(wsdlUrl);
// Create a channel factory
ChannelFactory<IClientChannel> factory = new ChannelFactory<IClientChannel>(binding, endpointAddress);
// Create a message
Message requestMessage = Message.CreateMessage(MessageVersion.Soap11, soapAction, new StringReader(requestBody));
// Create a channel
IClientChannel channel = factory.CreateChannel();
try
{
// Open the channel
channel.Open();
// Send the request and get the response
Message responseMessage = await channel.RequestAsync(requestMessage);
// Process the response
using (XmlReader reader = responseMessage.GetReaderAtBodyContents())
{
// Read the response body
XmlDocument responseXml = new XmlDocument();
responseXml.Load(reader);
// Process the response XML as needed
Console.WriteLine(responseXml.OuterXml);
}
}
finally
{
// Close the channel
if (channel.State == CommunicationState.Faulted)
{
channel.Abort();
}
else
{
channel.Close();
}
// Close the factory
factory.Close();
}
}
实际上我有一个肥皂服务有一个方法
ConsultarSaldosCobros
和一个请求,我需要在执行时调用。如果我添加服务引用,它可以正常工作,但是地址站点会根据环境而变化,因此我需要使用环境变量作为地址,执行时读取变量并调用方法。
代码是:
string direccion = Environment.GetEnvironmentVariable(StringHandler.LOGIN_URL);
using HttpContent content = new StringContent(text, Encoding.UTF8, "text/xml");
using HttpRequestMessage request = new(HttpMethod.Post, direccion);
request.Headers.Add("SOAPAction", direccion);
request.Content = content;
using HttpResponseMessage response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
string respuesta = await response.Content.ReadAsStringAsync();