C# 将文件作为MemoryStream流传输时,web服务中出现超时错误
我有一个web方法将文件读取为MemoryStream,但在测试我的服务时,我遇到了以下错误:C# 将文件作为MemoryStream流传输时,web服务中出现超时错误,c#,soap,timeout,webmethod,memorystream,C#,Soap,Timeout,Webmethod,Memorystream,我有一个web方法将文件读取为MemoryStream,但在测试我的服务时,我遇到了以下错误: System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: Timeouts are not supported on this stream. at System.IO.Stream.get_ReadTime
System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: Timeouts are not supported on this stream.
at System.IO.Stream.get_ReadTimeout()
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write4_MemoryStream(String n, String ns, MemoryStream o, Boolean isNullable, Boolean needType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write6_MemoryStream(Object o)
at Microsoft.Xml.Serialization.GeneratedAssembly.MemoryStreamSerializer.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()
不知道发生了什么,我很困惑,因为我知道MemoryStream没有超时限制。感谢您的帮助
这是我的web方法:
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public MemoryStream getApp()
{
string aid = "0";
if (Context.Request.QueryString["aid"] != null) aid=Context.Request.QueryString["aid"];
MemoryStream memSTR = new MemoryStream();
memSTR = CommonClasses.getAppStream(aid);
return memSTR;
}
我使用以下url调用我的服务:
这是CommonClasses.getAppStream(aid):
如果您认为web.config中的某些配置错误可能会导致问题,则以下是我的配置文件内容:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<httpRuntime/>
<webServices>
<protocols>
<add name="HttpGet"/>
</protocols>
</webServices>
<pages controlRenderingCompatibilityVersion="4.0"/>
</system.web>
<connectionStrings>
<remove name="LocalSqlServer"/>
<add name="DBCnn" connectionString="Data Source=.;Initial Catalog=myDatabase;Persist Security Info=True;User ID=myUserID;Password=myPassword; Connection Lifetime=0;Min Pool Size=10;Max Pool Size=300; Pooling=true;" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
显示来自CommonClasses.GetAppStream的代码感谢您的回复,我添加了所有必要的代码。在查看Show时,默认实现是为ReadTimeout属性引发异常,并且不会在MemoryStream、FileStram或BinaryStream中被覆盖。XmlSerializer似乎无法检查默认为false的CanTimeout属性。这就是为什么在以简单明了的方式实现和编程每件事情时它都会失败的原因,如上所述?它失败的原因是XmlSerializer读取Stream.ReadTimeout属性并引发异常。
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<httpRuntime/>
<webServices>
<protocols>
<add name="HttpGet"/>
</protocols>
</webServices>
<pages controlRenderingCompatibilityVersion="4.0"/>
</system.web>
<connectionStrings>
<remove name="LocalSqlServer"/>
<add name="DBCnn" connectionString="Data Source=.;Initial Catalog=myDatabase;Persist Security Info=True;User ID=myUserID;Password=myPassword; Connection Lifetime=0;Min Pool Size=10;Max Pool Size=300; Pooling=true;" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>