ASMX服务错误-使用XmlInclude或SoapInclude属性指定静态未知的类型

问题描述 投票:0回答:1

我有一个带有Web方法的Asmx服务。在该网络方法中,我正在调用其他端点(逻辑应用程序端点)。逻辑应用程序其余端点接受请求并返回成功200响应。

  using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("base address");
                var content = new StringContent(JsonConvert.SerializeObject(request, Formatting.Indented), Encoding.UTF8, "application/json");
                var result = await client.PostAsync("some-uri", content);                  
            }

我一击到client.postasync行,就会收到以下错误:

System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: The type System.Threading.Tasks.Task`1[[System.Threading.Tasks.VoidTaskResult, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write2_Task(String n, String ns, Task o, Boolean isNullable, Boolean needType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write3_Task(Object o)
   at Microsoft.Xml.Serialization.GeneratedAssembly.TaskSerializer.Serialize(Object objectToSerialize, XmlSerializationWriter writer)
   at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
   --- End of inner exception stack trace ---
   at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
   at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o, XmlSerializerNamespaces namespaces)
   at System.Web.Services.Protocols.XmlReturnWriter.Write(HttpResponse response, Stream outputStream, Object returnValue)
   at System.Web.Services.Protocols.HttpServerProtocol.WriteReturns(Object[] returnValues, Stream outputStream)
   at System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[] returnValues)
   at System.Web.Services.Protocols.WebServiceHandler.Invoke()
c# xml asmx azure-logic-apps
1个回答
0
投票

我发现了错误。该网络方法被设为异步,这导致了此问题。我使Web方法返回void,并将http客户端发布请求的代码行更改为以下代码:

  var result = client.PostAsync(reqUri, requestContent).GetAwaiter().GetResult();

© www.soinside.com 2019 - 2024. All rights reserved.