在 HttpWebRequest.GetResponse() 上生成的动态程序集
我们的应用程序似乎存在程序集泄漏。我注意到,在使用 HttpWebRequest 对象调用 Web 服务调用的任何调用中,调用 httpWebRequest.GetResponse() 时都会加载动态程序集,
我可以看到程序集通过调试器加载(“w3wp.exe”(托管):已加载“7-6jav6v”,未加载符号。)但我无法弄清楚为什么会发生这种情况。
以前有其他人经历过这种情况吗?
编辑: 对此问题进行补充说明。 在 C# 中,当您创建 XmlSerializer 时,会创建一个程序集来完成序列化。除非您提前使用工具来完成此操作,否则这种情况总会发生。如果使用 (Type type) 或 (Type type, string "namespace") 的构造函数,则只会生成 1 个程序集。如果您使用任何其他构造函数,则将为每个序列化生成一个新的程序集。
上述问题并非如此。
我们的代码库中有一个代码块,它手动进行肥皂调用并返回一个字符串(该字符串是 xml,例如:)。每次执行该代码块时都会创建一个新的程序集。在检查其中一个程序集时,将引用“XmlSerializationWriter1.Microsoft.Xml.Serialization.GenerateAssembly.XmlSerializationReader1.XmlSerializer1.ArrayOfObjectSerializer.ArrayOfObjectSerializer1.ArrayOfObjectSerializer2”
为了更好地理解 - 代码块如下所示,当最后一行执行程序集时生成...多个程序集,每次运行该块时都会生成一个程序集。
HttpWebRequest oHttpWebRequest =(HttpWebRequest)WebRequest.Create("URL TO WEBSERVICE");
oHttpWebRequest.Timeout =((1000*60)*30);
oHttpWebRequest.Method ="POST" ;
oHttpWebRequest.ContentType ="text/xml" ;
oHttpWebRequest.Headers.Add("SOAPAction: http://www.tempuri.com/"+WebMethodName);
StreamWriter oStreamWriter = new StreamWriter(oHttpWebRequest.GetRequestStream()) ;
string SoapRequest=@"<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""><soap:Body>";
SoapRequest=SoapRequest + HttpUtility.HtmlDecode(XmlHttpRequestData);
SoapRequest=SoapRequest + @"</soap:Body></soap:Envelope>";
oStreamWriter.Write(SoapRequest);
oStreamWriter.Close();
oHttpWebRequest.ProtocolVersion.Build;
WebResponse oWebResponse = oHttpWebRequest.GetResponse() ;
It appears our application has an assembly leak. I noticed that on any calls where a web service call is invoked using the HttpWebRequest object a dynamic assembly is loaded on the call httpWebRequest.GetResponse()
I can see the assembly get loaded through the debugger ('w3wp.exe' (Managed): Loaded '7-6jav6v', No symbols loaded.) but I cannot figure out why this would occur.
Has anyone else experienced this before?
Edit:
To add clarifications to this question.
In c# when you create an XmlSerializer an assembly is created to complete the serialization. This always will occur unless you use a tool to do this for you in advance. If you use the constructor of (Type type) or (Type type, string "namespace") then only 1 assembly will be generated. If you use any other constructor then a new assembly will be generated for each serialization.
THis is not the case in the problem stated above.
There is a block of code in our codebase that manually makes a soap call and returns a string (the string is xml, ex: ). Each time this block of code executes a new assembly gets created. When examining one of these assemblies this is referenced "XmlSerializationWriter1.Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.XmlSerializer1.ArrayOfObjectSerializer.ArrayOfObjectSerializer1.ArrayOfObjectSerializer2"
For a better understanding - the code block looks like below and when the last line executes the assembly gets generated...multiple assemblies, one for each time this block runs.
HttpWebRequest oHttpWebRequest =(HttpWebRequest)WebRequest.Create("URL TO WEBSERVICE");
oHttpWebRequest.Timeout =((1000*60)*30);
oHttpWebRequest.Method ="POST" ;
oHttpWebRequest.ContentType ="text/xml" ;
oHttpWebRequest.Headers.Add("SOAPAction: http://www.tempuri.com/"+WebMethodName);
StreamWriter oStreamWriter = new StreamWriter(oHttpWebRequest.GetRequestStream()) ;
string SoapRequest=@"<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""><soap:Body>";
SoapRequest=SoapRequest + HttpUtility.HtmlDecode(XmlHttpRequestData);
SoapRequest=SoapRequest + @"</soap:Body></soap:Envelope>";
oStreamWriter.Write(SoapRequest);
oStreamWriter.Close();
oHttpWebRequest.ProtocolVersion.Build;
WebResponse oWebResponse = oHttpWebRequest.GetResponse() ;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您在 Sky Sanders 的回答下面的评论,生成的程序集用于 XML 序列化。序列化程序集是动态生成的,除非您使用 XML 序列化程序生成器工具预先生成它们(Sgen.exe)。如果这样做,将使用现有程序集并且不会生成任何程序集
According to your comment below Sky Sanders' answer, the generated assemblies are for XML serialization. Serialization assemblies are dynamically generated, unless you pre-generate them using the XML Serializer Generator Tool (Sgen.exe). If you do that, the existing assemblies will be used and no assembly will be generated
您调用的 Web 服务的 xml 架构是固定的还是动态的?如果您正在调用任意 Web 服务,每个服务都将任意 XML 消息作为输入并返回任意 XML 消息作为输出...那么 XmlSerializer 将为每个架构创建一个新程序集。如果每条消息本质上使用相同的架构,但结构上有足够的差异,即使它们可以使用通用架构,XmlSerializer 也只能做到这一点......它将生成一个程序集来处理它标识的每个特定架构。
就像 Thomas 所说,如果您的架构是固定的,请使用 XML Serializer Generator Tool 预先生成序列化程序集。
Is the schema of the xml for the web services you call fixed, or dynamic? If you are calling arbitrary web services that each take arbitrary XML messages as input and return arbitrary XML messages as output...then the XmlSerializer is going to create a new assembly for each schema. If each message essentially uses the same schema, but varies enough in structure, even though they could use a common schema, the XmlSerializer is only so capable...its going to generate a assembly to handle each specific schema it identifies.
Like Thomas said, if your schema is fixed, use the XML Serializer Generator Tool to pre-generate your serialization assemblies.