我试图从.NET核心调用soap服务。
我使用dotnet-svcutil构建了代理,发现它与同一个端点的旧.NET 4.6实现有很大不同。
.NET核心代理没有继承自System.Web.Services.Protocols.SoapHttpClientProtocol的类。我知道这个名称空间在.NET核心中消失了,但是是什么取代了它呢?
发布于 2022-11-16 16:25:13
我的建议是请该服务的制造者创建一项新的服务,该服务可以夸夸其谈。最近我不得不使用一个soap服务,遇到了各种各样的特色菜。决定跳过core.net中半实现的soap,并使用带有soap信封的简单post请求调用它。您可以使用wsdl来创建您需要序列化为xml的类。(在VS中使用粘贴xml作为类)
private async Task<EnvelopeBody> ExecuteRequest(Envelope request)
{
EnvelopeBody body = new EnvelopeBody();
var httpWebRequest = new HttpRequestMessage(HttpMethod.Post, _serviceUrl);
string soapMessage = XmlSerializerGeneric<Envelope>.Serialize(request);
httpWebRequest.Content = new StringContent(soapMessage);
var httpResponseMessage = await _client.SendAsync(httpWebRequest);
if (httpResponseMessage.IsSuccessStatusCode)
{
using var contentStream = await httpResponseMessage.Content.ReadAsStreamAsync();
Envelope soapResult;
var mySerializer = new XmlSerializer(typeof(Envelope));
using (StreamReader streamReader = new StreamReader(contentStream))
{
soapResult = (Envelope) mySerializer.Deserialize(streamReader);
}
body = soapResult.Body;
}
return body;
}
My soap envelope looks like this:
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://schemas.xmlsoap.org/soap/envelope/", IsNullable = false)]
public partial class Envelope
{
private object headerField;
private EnvelopeBody bodyField;
/// <remarks/>
public object Header
{
get
{
return this.headerField;
}
set
{
this.headerField = value;
}
}
/// <remarks/>
public EnvelopeBody Body
{
get
{
return this.bodyField;
}
set
{
this.bodyField = value;
}
}
}https://stackoverflow.com/questions/68165488
复制相似问题