For compatibility reasons between legacy ASP code and the new ASP.NET code, we have a bunch of .NET COM objects that provide some of our .NET applications for ASP. In some cases, we need to work with other COM objects inside our .NET COM shells. To provide high flexibility and avoid dependence on PIA, we decided to use dynamic code to work with these COM objects.
Simplified C # COM Object:
using System; using System.Text; using System.Runtime.InteropServices; namespace TestCom { [ComVisible(true)] [Guid("6DC92920-8C3C-4C81-A615-BD0E3A332024")] [InterfaceType(ComInterfaceType.InterfaceIsDual)] public interface ITestComObject { [DispId(1)] string MyMethod(dynamic dictionary); } [ComVisible(true)] [Guid("F52A463E-F03B-4703-860C-E86CDD6D04E3")] [ClassInterface(ClassInterfaceType.None)] [ProgId("TestCom.TestComObject")] public class TestComObject : ITestComObject { string ITestComObject.MyMethod(dynamic dictionary) { StringBuilder sb = new StringBuilder(); if (dictionary != null) { foreach (object key in dictionary) { object p = dictionary[key]; if (p != null) { sb.AppendFormat("{0}={1}{2}", key, p, Environment.NewLine); } } } return sb.ToString(); } } }
Testing an ASP Page:
<%@ Language=VBScript %> <% Dim testObj, params Set testObj = Server.CreateObject("TestCom.TestComObject") Set params = Server.CreateObject("Scripting.Dictionary") params("lang") = "en-US" params("num") = 42 Response.Write testObj.MyMethod(params) Set testObj = Nothing Set params = Nothing %>
In the normal case, dynamic code will be compiled only once, and subsequent calls will reuse it. However, in our case, it seems that the dynamic code is compiled for each individual call. When I attach the memory profiler to the IIS process, I can clearly see additional objects from the Microsoft.CSharp.RuntimeBinder.Semantics namespace appearing in gen2. This actually causes a memory leak in the IIS process.
Any ideas on fixing this issue with compiling dynamic code? Please note that rewriting all the code to use the PIA and COM interfaces is not always an option in our case.
source share