Use CodeDoM to load code from file and compile it at Runtime in

Public Sub LoadCodeFromFileAndExecute() 
        Dim objCodeCompiler As System.CodeDom.Compiler.CodeDomProvider = System.CodeDom.Compiler.CodeDomProvider.CreateProvider("VisualBasic")

        Dim objCompilerParameters As New System.CodeDom.Compiler.CompilerParameters
  ''The below one is a custom dll that I require for processing the logic (and have provided as an example, just so you can refer such custom ones as required)
  ''Again, the Nlog is used for logging and the configuration for Nlog is loaded from the web.config, when the application initializes itself.
        objCompilerParameters.GenerateInMemory = True

        ' Get the source code and compile it from the file system (assuming that your code itself is residing in the bin folder from where the application is being executed)
        Dim strCode As String = String.Empty
        Using sw As StreamReader = New StreamReader("D:\\DynamicCode\\bin\\GetLogDetailsFromDictionary.vb")
            strCode = sw.ReadToEnd()
        End Using

  ''Try to compile the code 
        Dim objCompileResults As System.CodeDom.Compiler.CompilerResults = objCodeCompiler.CompileAssemblyFromSource(objCompilerParameters, strCode)

        ' Check for compiler errors.  In my case I am just exiting the Sub
        If objCompileResults.Errors.HasErrors Then
            Exit Sub
        End If

        ' Get a reference to the NEW assembly emitted by CodeDom as part of the compilation performed in the earlier statement
        Dim objAssembly As System.Reflection.Assembly = objCompileResults.CompiledAssembly

        ' Create an instance of the DynamicCode class referenced in the source code.
        Dim objTheClass As Object = objAssembly.CreateInstance("HelloWorld")

  ''Verify if the CreateInstance was able to generate a new instance successfully
        If objTheClass Is Nothing Then
            Exit Sub
        End If

  ''Invoke the Method 
        Dim objResult As Object = objTheClass.GetType.InvokeMember("SayHello", _
                    BindingFlags.InvokeMethod, Nothing, objTheClass, Nothing)

    Catch ex As Exception

    End Try
End Sub

1,428 total views, 7 views today

Entity Framework – Performance Optimization Tips

The below are some tips towards optimizing the performance of applications that use Entity Framework.

  1. To disable AutoDetectChanges, when not needed ( /
  2. Even though the DBContext object is a lightweight one, its the number of entities that affects the performance.  For instance, after you are done creating a new DbContext, if you perform repeated .Add(..) and .SaveChanges(), at one point you will see a downgrade in performance and if investigated, it will point to the number of Entities that incrementally got included resulting in degrading the performance of the DBContext object.  A solution is to dispose the DBContext and create new objects for a set of operations (for instance every 1000 objects), however from a “Data Integrity / Atomicity” (ACID) perspective, every DbContext will use its own connection and multiple connections will require MSDTC, which at times can be a trouble to live with in the long run.  So, in such cases, we could resort to the option of using a single Connection and TransactionScope as outlined in the MSDN link. (
  3. As a rule of thumb, while retrieving data from the database, retrieve only those fields are required.  This is not just for including a where condition aimed at filtering the records, but also the fields that are required.
  4. Use Take & Skip in EF to query data based on “pages” (
  5. Avoid calling SaveChanges() for every transaction, but instead try to call the SaveChanges() for every 500 iterations and dispose the old dbcontext and generate a new one.  This approach eliminates the need to perform the change detection for each record (when saved individually) and as well removes network latency involved in persisting the changes to the data base.  However, regeneration of the context object would essentially mean a SQLConnection is assigned to the db context, which would result in MSDTC (as there are multiple connections involved – one per context).
  6. Use SQLBulkCopy if it involves inserting multiple records (thousands of rows)
  7. If you do not want to disable the AutoDetectChanges, another option is to detach the entities that are already committed through the SaveChanges() operation.  This would reduce the number of entities being accumulated in the dbContext as we progress through the loop/iterations and help eliminate the lag that gets induced when the entities count in the db context crosses a threshold (which could vary depending on how relational the entities are).

1,288 total views, 4 views today

Azure Quick Starts Code Samples, provided by Microsoft.

If you are looking for a good source of code examples towards getting started with Azure, then I would recommend to use something that’s available right off the Visual Studio IDE.

When you try to create a new project, under cloud select “Quick Starts” to view the grand list of Azure offerings and pick your type of project to automatically create a sample application along with the necessary plumbing logic to consume the particular service:



725 total views, 2 views today