簡體   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