繁体   English   中英

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

[英]ASMX Service Error - Use the XmlInclude or SoapInclude attribute to specify types that are not known statically

我有一个带有 web 方法的 Asmx 服务。 在该 web 方法中,我正在调用 rest 端点(逻辑应用程序端点)。 逻辑应用 rest 端点接受请求并返回成功 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()

我发现了错误。 webmethod 是异步的,这导致了这个问题。 我使 web 方法返回 void 并将 http 客户端发布请求代码行更改为以下代码:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM