none
System.TypeInitializationException in ctor of System.Data.SqlClient.SqlConnection in DatabaseAssembly

    Question

  • Hi all,

    since today morning we facing following exception in our DatabaseAssembly (MSSQL 2012) when we create a new instance of SqlConnection [using (SqlConnection conn = new SqlConnection("context connection=true"))]

    System.TypeInitializationException: Der Typeninitialisierer für "System.Data.SqlClient.SqlConnection" hat eine Ausnahme verursacht. ---> System.TypeInitializationException: Der Typeninitialisierer für "System.Data.SqlClient.SqlConnectionFactory" hat eine Ausnahme verursacht. ---> System.TypeInitializationException: Der Typeninitialisierer für "System.Data.SqlClient.SqlPerformanceCounters" hat eine Ausnahme verursacht. ---> System.MethodAccessException: Fehler beim Versuch der Methode "System.Configuration.TypeUtil.CreateInstanceRestricted(System.Type, System.Type)", auf Methode "System.Diagnostics.SwitchElementsCollection..ctor()" zuzugreifen. ---> System.Security.SecurityException: Fehler bei der Anfrage.

    System.Security.SecurityException:

       bei System.Security.CodeAccessSecurityEngine.ThrowSecurityException(RuntimeAssembly asm, PermissionSet granted, PermissionSet refused, RuntimeMethodHandleInternal rmh, SecurityAction action, Object demand, IPermission permThatFailed)

       bei System.Security.CodeAccessSecurityEngine.CheckSetHelper(PermissionSet grants, PermissionSet refused, PermissionSet demands, RuntimeMethodHandleInternal rmh, Object assemblyOrString, SecurityAction action, Boolean throwException)

       bei System.Security.PermissionListSet.CheckSetDemandWithModification(PermissionSet pset, PermissionSet& alteredDemandSet, RuntimeMethodHandleInternal rmh)

       bei System.Security.PermissionListSet.CheckSetDemand(PermissionSet pset, RuntimeMethodHandleInternal rmh)

       bei System.Security.PermissionListSet.DemandFlagsOrGrantSet(Int32 flags, PermissionSet grantSet)

    ...

    The only changes came from the Microsoft .Net 4.5 updates today morning.

    Any solutions?

    Wednesday, July 10, 2013 11:35 AM

Answers

  • Hi Stefan,

    thanks.

    For me there is another solution:

    After removing .NET Update KB2840628 it works fine.

    Thursday, July 11, 2013 11:11 AM
  • To fix this issue just install the following update: http://support.microsoft.com/kb/2872041

    Installing the .Net Framework 4.5 seems to fix this as well.

    • Marked as answer by Stefan Licht Wednesday, July 31, 2013 11:07 AM
    • Edited by Stefan Licht Wednesday, July 31, 2013 11:08 AM
    Wednesday, July 31, 2013 11:07 AM

All replies

  • That sounds serious. It looks like you should open a case with PSS immediately, and look into ways to roll back the update.

    Bob

    Wednesday, July 10, 2013 2:43 PM
  • Following up...

    Hi Stefan,

    I just installed all latest updates on a machine (SQL Server 2012 SP1 (11.0.3128.0) running on Windows Server 2012) to test this. I got 2 updates for .NET 4.5, KB2840632 and KB2833958. After rebooting the OS (required by the updates in order to install them), context connection (SqlConnection with connection string of 'context connection=true') worked just fine for me.

    What OS and what version of SQL Server 2012 are you running?  

    Cheers, Bob

    Wednesday, July 10, 2013 5:03 PM
  • Hi guys,

    I am facing same problem today.

    Windows 7 and SQL 2012 Express.

    I can connect to the database but when i am executing a function, this error comes up.

    Any ideas? Have downloaded all updates, but no improvement.

    Best regards from Germany

    David

    Thursday, July 11, 2013 8:20 AM
  • Having the same issue.

    Windows Server 2008 R2

    SQL Server 2012 Standart

    CLR SqlCommand Security Issue

    Thursday, July 11, 2013 8:49 AM
  • It is a Windows 7. After changeing PERMISSION_SET from SAFE to UNSAFE we don't get the exception anymore.

    However, this worked fine prior the update installation.

    Thursday, July 11, 2013 10:40 AM
  • Hi Stefan,

    where? How to?

    Thank you

    Thursday, July 11, 2013 10:50 AM
  • Hi Didikong,

    when you create the databaseassembly like

    CREATE ASSEMBLY [EQUIcon.EQSped.DatabaseProcedures]
    AUTHORIZATION [dbo]
    FROM [...]
    WITH PERMISSION_SET = UNSAFE

    you can choose the permissionset.

    More info here http://msdn.microsoft.com/de-de/library/ms189524.aspx

    Thursday, July 11, 2013 11:03 AM
  • Hi Stefan,

    thanks.

    For me there is another solution:

    After removing .NET Update KB2840628 it works fine.

    Thursday, July 11, 2013 11:11 AM
  • Confirmed, Didikong - uninstalling that update cleared up the issue here.  Anyone know if it'll come back next time my machine updates?
    Thursday, July 11, 2013 1:16 PM
  • Same issue here, caught me by surprise!

    Fixed by removing .NET update KB2840628.

    Bug raised onto Microsoft Connect:

    https://connect.microsoft.com/VisualStudio/feedback/details/793500/sqlclr-typeinitializationexception-in-sqlconnection-since-kb2840628


    -lexxy

    Thursday, July 11, 2013 1:52 PM
  • Mostly the cause of the System.TypeInitializationException is assembly mismatch, which means the assembly your code is referring is different from the assemble actually used in your code.

    Cheers

    Abhijeet

    Thursday, July 11, 2013 2:00 PM
  • Removing kb2840628 worked for us too. Thanks!

    Windows Server 2008 R2

    SQL Server 2012 Standard

    • Edited by KeithHarrison Thursday, July 11, 2013 3:53 PM more detail
    Thursday, July 11, 2013 3:51 PM
  • Hi Stefan,

    thanks.

    For me there is another solution:

    After removing .NET Update KB2840628 it works fine.


    It worked for me. Windows 7 x64 and SQL 2012 Express Edition.
    Thursday, July 11, 2013 9:29 PM
  • Hey Philipp,

    there is a function on right click to hide the update. Then it'll be not installed.

    Friday, July 12, 2013 10:01 AM
  • To fix this issue just install the following update: http://support.microsoft.com/kb/2872041

    Installing the .Net Framework 4.5 seems to fix this as well.

    • Marked as answer by Stefan Licht Wednesday, July 31, 2013 11:07 AM
    • Edited by Stefan Licht Wednesday, July 31, 2013 11:08 AM
    Wednesday, July 31, 2013 11:07 AM