Why have I this TypeInitializationException calling a method in the execution of a DAO method only when I...












0















I am pretty new in .NET and SharePoint (I came from Java EE environment) and I have the following strange problem working on a SharePoint 2013 project.



Basically in my SharePoint application I access to a SQL Server database using some classes into a DAO project (that is in my solution). This DAO project is based on ADO.NET. It works fine. The prolem is that I created a new NUnit project in my solution because I want to test this DAO method defined in my DAO project performing some integration test. In this case I am obtaining a strange exception.



I try to explain you my situation in details.



In my solution I have a project that act as DAO (it contains all the databse query and handle the DB connection). This project is called something like MyPrjSqlEngine. Then I have another project called MyMainPrj that contains some timer job. In this main project I have a timer job containing the following logic:



namespace MyMainPrj.TimerJob.Notifiche
{
public class NotificheGiornaliereView : SPJobDefinition
{


public NotificheGiornaliereView() : base() { }

public NotificheGiornaliereView(string jobName, SPService service) :
base(jobName, service, null, SPJobLockType.None)
{
this.Title = "Notifiche giornaliere protocollo View";
}

public NotificheGiornaliereView(string jobName, SPWebApplication webapp) :
base(jobName, webapp, null, SPJobLockType.ContentDatabase)
{
this.Title = "Notifiche giornaliere protocollo View";
}

public override void Execute(Guid targetInstanceId)
{
List<string> tenantList = ProtUtils.GetTenantList();

DBConnection dbConfig = new DBConnection();
dbConfig.Tenant = "XXX";

ArrayList listaUtenti = new ArrayList();

List<string> urlList = IndirizziProtocolliSQL.GetListaIndirizziSiti(dbConfig);
}
}
}


The logic is implemented into the Execute() method that retrieves a list of string from the DB. The IndirizziProtocolliSQL is a class of my MyPrjSqlEngine DAO project.



This is the code of the called method implemented in this DAO project:



public static List<string> GetListaIndirizziSiti(DBConnection dbConf)
{
string url = null;

List<string> urlList = new List<string>();

string query = PROT_INDIRIZZI_PROTOCOLLI.SELECT_LISTA_INDIRIZZI_SITI;
using (SqlConnection con = PrjConnection.getStringConnection(dbConf.Tenant + "_PROTOCOLLO"))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
try
{
con.Open();

using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
url = reader.GetString(reader.GetOrdinal("Url"));
urlList.Add(url);
}
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
}
}
return urlList;
}


As you can see, here it obtain the connection to the DB:



using (SqlConnection con = PrjConnection.getStringConnection(dbConf.Tenant + "_PROTOCOLLO"))


This is the code of the getStringConnection() method (defined always in the same DAO project):



[MethodImpl(MethodImplOptions.Synchronized)]
public static SqlConnection gettringConnection(string name)
{
SqlConnectionStringBuilder bu = new SqlConnectionStringBuilder();

XmlDocument config = new XmlDocument();
config.Load(@"C:Program FilesInformation Workers GroupMyPrj" + MyPrjSqlEngine.Properties.Resources.Progetto + @"cfgconfig.xml");

XmlNode xmlRoot = config.SelectSingleNode("connection_config");
XmlNode root = xmlRoot.SelectSingleNode("database");
string user = root.SelectSingleNode("user").InnerText;
string passwordCrypted = root.SelectSingleNode("password").InnerText;
string password = Crypto.Crypto.DecriptAes(passwordCrypted);
string server = root.SelectSingleNode("server").InnerText;
string database = name;

bu.DataSource = @server;

bu.InitialCatalog = database;

bu.IntegratedSecurity = false; // Sql Server Authentification

bu.UserID = user;

bu.Password = password;
bu.MultipleActiveResultSets = true;
SqlConnection mycon = new SqlConnection();
mycon.ConnectionString = bu.ConnectionString;

return mycon;
}


As you can see in this method it retrieves and load an XML configuration file that contains the database connection info, something like this:



<connection_config>
<database>
<user>MY-USER</user>
<password>CRYPTED-PSWD</password>
<server>DB-SERVER-NAME</server>
</database>
</connection_config>


and then retrieve these information from this XML file and use it to create a DB connection string.



As explained at the beginning of the post, it works fine performing the query from the SharePoint application (I execute the job and the query is performed as I expect, no problem).



The problem is: then I defined a new NUnit in my solution, to test the DAO methods as integration test, so in this test project I have this class:



namespace Tests
{
public class Tests
{
[SetUp]
public void Setup()
{
}

[Test]
public void Test1()
{
Assert.Pass("PASSED");
}


[Test]
public void GetIndirizzoProtocollo_XXX_YYY_ReturnSingleValue()
{
DBConnection dbConfig = new DBConnection();
dbConfig.Tenant = "XXX";

List<string> urlList = IndirizziProtocolliSQL.GetListaIndirizziSiti(dbConfig);

Assert.Pass("OK");
}

}
}


At the moment it contains only a minimalistic test method. As you can see I am testing the same DAO method called from my timer job.



Debugging the execution of this test method I have that:




  1. I correctly enter into the IndirizziProtocolliSQL.GetListaIndirizziSiti() DAO method.


  2. Here it correctly enter into the getStringConnection() method where start to correctly retrieve the information from the XML configuration file.



  3. Now the passwordCrypted variable contains the crypted password retrieved from the XML document. Then it try to perform this line to decrypt the crypted password:



    string password = Crypto.Crypto.DecriptAes(passwordCrypted);


    and now I obtain the following exception in the stacktrace:



    Exception thrown: 'System.TypeInitializationException' in MyPrjSqlEngine.dll
    An exception of type 'System.TypeInitializationException' occurred in MyPrjSqlEngine.dll but was not handled in user code
    The type initializer for '<Module>' threw an exception.



In the popup on the line where the exception happen I have a message like this:



    System.TypeInitializationException: 'The type initializer for '<Module>' threw an exception.'
EntryPointNotFoundException: A library name must be specified in a DllImport attribute applied to non-IJW methods.


The inner exception is:



-       InnerException  {<CrtImplementationDetails>.ModuleLoadException: The C++ module failed to load.
---> System.EntryPointNotFoundException: A library name must be specified in a DllImport attribute applied to non-IJW methods.
at _getFiberPtrId()
at <CrtImplementationDetails>.LanguageSupport._Initialize(LanguageSupport* )
at <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* )
--- End of inner exception stack trace ---
at <CrtImplementationDetails>.ThrowModuleLoadException(String errorMessage, Exception innerException)
at <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* )
at .cctor()} System.Exception {<CrtImplementationDetails>.ModuleLoadException}


Debugging this unit test I have that when it try to execute this line:



string password = Crypto.Crypto.DecriptAes(passwordCrypted);


I can't do Step Into this DecriptAes() method. I don't know why because it is into a class defined into the same MyPrjSqlEngine project.



It seems that it can't find this Crypto class or something like this.



This Cyrpto class is defined into the same MyPrjSqlEngine pèroject but in another namesapace:



namespace MyPrjSqlEngine.Crypto
{
public class Crypto
{
........................
........................
........................
}
}


Why I have this problem calling from unit test and I have not calling directly from the SharePoint timer job? How can I try to fix it?










share|improve this question



























    0















    I am pretty new in .NET and SharePoint (I came from Java EE environment) and I have the following strange problem working on a SharePoint 2013 project.



    Basically in my SharePoint application I access to a SQL Server database using some classes into a DAO project (that is in my solution). This DAO project is based on ADO.NET. It works fine. The prolem is that I created a new NUnit project in my solution because I want to test this DAO method defined in my DAO project performing some integration test. In this case I am obtaining a strange exception.



    I try to explain you my situation in details.



    In my solution I have a project that act as DAO (it contains all the databse query and handle the DB connection). This project is called something like MyPrjSqlEngine. Then I have another project called MyMainPrj that contains some timer job. In this main project I have a timer job containing the following logic:



    namespace MyMainPrj.TimerJob.Notifiche
    {
    public class NotificheGiornaliereView : SPJobDefinition
    {


    public NotificheGiornaliereView() : base() { }

    public NotificheGiornaliereView(string jobName, SPService service) :
    base(jobName, service, null, SPJobLockType.None)
    {
    this.Title = "Notifiche giornaliere protocollo View";
    }

    public NotificheGiornaliereView(string jobName, SPWebApplication webapp) :
    base(jobName, webapp, null, SPJobLockType.ContentDatabase)
    {
    this.Title = "Notifiche giornaliere protocollo View";
    }

    public override void Execute(Guid targetInstanceId)
    {
    List<string> tenantList = ProtUtils.GetTenantList();

    DBConnection dbConfig = new DBConnection();
    dbConfig.Tenant = "XXX";

    ArrayList listaUtenti = new ArrayList();

    List<string> urlList = IndirizziProtocolliSQL.GetListaIndirizziSiti(dbConfig);
    }
    }
    }


    The logic is implemented into the Execute() method that retrieves a list of string from the DB. The IndirizziProtocolliSQL is a class of my MyPrjSqlEngine DAO project.



    This is the code of the called method implemented in this DAO project:



    public static List<string> GetListaIndirizziSiti(DBConnection dbConf)
    {
    string url = null;

    List<string> urlList = new List<string>();

    string query = PROT_INDIRIZZI_PROTOCOLLI.SELECT_LISTA_INDIRIZZI_SITI;
    using (SqlConnection con = PrjConnection.getStringConnection(dbConf.Tenant + "_PROTOCOLLO"))
    {
    using (SqlCommand cmd = new SqlCommand(query, con))
    {
    try
    {
    con.Open();

    using (SqlDataReader reader = cmd.ExecuteReader())
    {
    if (reader.HasRows)
    {
    while (reader.Read())
    {
    url = reader.GetString(reader.GetOrdinal("Url"));
    urlList.Add(url);
    }
    }
    }
    }
    catch (Exception ex)
    {
    throw ex;
    }
    finally
    {
    con.Close();
    }
    }
    }
    return urlList;
    }


    As you can see, here it obtain the connection to the DB:



    using (SqlConnection con = PrjConnection.getStringConnection(dbConf.Tenant + "_PROTOCOLLO"))


    This is the code of the getStringConnection() method (defined always in the same DAO project):



    [MethodImpl(MethodImplOptions.Synchronized)]
    public static SqlConnection gettringConnection(string name)
    {
    SqlConnectionStringBuilder bu = new SqlConnectionStringBuilder();

    XmlDocument config = new XmlDocument();
    config.Load(@"C:Program FilesInformation Workers GroupMyPrj" + MyPrjSqlEngine.Properties.Resources.Progetto + @"cfgconfig.xml");

    XmlNode xmlRoot = config.SelectSingleNode("connection_config");
    XmlNode root = xmlRoot.SelectSingleNode("database");
    string user = root.SelectSingleNode("user").InnerText;
    string passwordCrypted = root.SelectSingleNode("password").InnerText;
    string password = Crypto.Crypto.DecriptAes(passwordCrypted);
    string server = root.SelectSingleNode("server").InnerText;
    string database = name;

    bu.DataSource = @server;

    bu.InitialCatalog = database;

    bu.IntegratedSecurity = false; // Sql Server Authentification

    bu.UserID = user;

    bu.Password = password;
    bu.MultipleActiveResultSets = true;
    SqlConnection mycon = new SqlConnection();
    mycon.ConnectionString = bu.ConnectionString;

    return mycon;
    }


    As you can see in this method it retrieves and load an XML configuration file that contains the database connection info, something like this:



    <connection_config>
    <database>
    <user>MY-USER</user>
    <password>CRYPTED-PSWD</password>
    <server>DB-SERVER-NAME</server>
    </database>
    </connection_config>


    and then retrieve these information from this XML file and use it to create a DB connection string.



    As explained at the beginning of the post, it works fine performing the query from the SharePoint application (I execute the job and the query is performed as I expect, no problem).



    The problem is: then I defined a new NUnit in my solution, to test the DAO methods as integration test, so in this test project I have this class:



    namespace Tests
    {
    public class Tests
    {
    [SetUp]
    public void Setup()
    {
    }

    [Test]
    public void Test1()
    {
    Assert.Pass("PASSED");
    }


    [Test]
    public void GetIndirizzoProtocollo_XXX_YYY_ReturnSingleValue()
    {
    DBConnection dbConfig = new DBConnection();
    dbConfig.Tenant = "XXX";

    List<string> urlList = IndirizziProtocolliSQL.GetListaIndirizziSiti(dbConfig);

    Assert.Pass("OK");
    }

    }
    }


    At the moment it contains only a minimalistic test method. As you can see I am testing the same DAO method called from my timer job.



    Debugging the execution of this test method I have that:




    1. I correctly enter into the IndirizziProtocolliSQL.GetListaIndirizziSiti() DAO method.


    2. Here it correctly enter into the getStringConnection() method where start to correctly retrieve the information from the XML configuration file.



    3. Now the passwordCrypted variable contains the crypted password retrieved from the XML document. Then it try to perform this line to decrypt the crypted password:



      string password = Crypto.Crypto.DecriptAes(passwordCrypted);


      and now I obtain the following exception in the stacktrace:



      Exception thrown: 'System.TypeInitializationException' in MyPrjSqlEngine.dll
      An exception of type 'System.TypeInitializationException' occurred in MyPrjSqlEngine.dll but was not handled in user code
      The type initializer for '<Module>' threw an exception.



    In the popup on the line where the exception happen I have a message like this:



        System.TypeInitializationException: 'The type initializer for '<Module>' threw an exception.'
    EntryPointNotFoundException: A library name must be specified in a DllImport attribute applied to non-IJW methods.


    The inner exception is:



    -       InnerException  {<CrtImplementationDetails>.ModuleLoadException: The C++ module failed to load.
    ---> System.EntryPointNotFoundException: A library name must be specified in a DllImport attribute applied to non-IJW methods.
    at _getFiberPtrId()
    at <CrtImplementationDetails>.LanguageSupport._Initialize(LanguageSupport* )
    at <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* )
    --- End of inner exception stack trace ---
    at <CrtImplementationDetails>.ThrowModuleLoadException(String errorMessage, Exception innerException)
    at <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* )
    at .cctor()} System.Exception {<CrtImplementationDetails>.ModuleLoadException}


    Debugging this unit test I have that when it try to execute this line:



    string password = Crypto.Crypto.DecriptAes(passwordCrypted);


    I can't do Step Into this DecriptAes() method. I don't know why because it is into a class defined into the same MyPrjSqlEngine project.



    It seems that it can't find this Crypto class or something like this.



    This Cyrpto class is defined into the same MyPrjSqlEngine pèroject but in another namesapace:



    namespace MyPrjSqlEngine.Crypto
    {
    public class Crypto
    {
    ........................
    ........................
    ........................
    }
    }


    Why I have this problem calling from unit test and I have not calling directly from the SharePoint timer job? How can I try to fix it?










    share|improve this question

























      0












      0








      0








      I am pretty new in .NET and SharePoint (I came from Java EE environment) and I have the following strange problem working on a SharePoint 2013 project.



      Basically in my SharePoint application I access to a SQL Server database using some classes into a DAO project (that is in my solution). This DAO project is based on ADO.NET. It works fine. The prolem is that I created a new NUnit project in my solution because I want to test this DAO method defined in my DAO project performing some integration test. In this case I am obtaining a strange exception.



      I try to explain you my situation in details.



      In my solution I have a project that act as DAO (it contains all the databse query and handle the DB connection). This project is called something like MyPrjSqlEngine. Then I have another project called MyMainPrj that contains some timer job. In this main project I have a timer job containing the following logic:



      namespace MyMainPrj.TimerJob.Notifiche
      {
      public class NotificheGiornaliereView : SPJobDefinition
      {


      public NotificheGiornaliereView() : base() { }

      public NotificheGiornaliereView(string jobName, SPService service) :
      base(jobName, service, null, SPJobLockType.None)
      {
      this.Title = "Notifiche giornaliere protocollo View";
      }

      public NotificheGiornaliereView(string jobName, SPWebApplication webapp) :
      base(jobName, webapp, null, SPJobLockType.ContentDatabase)
      {
      this.Title = "Notifiche giornaliere protocollo View";
      }

      public override void Execute(Guid targetInstanceId)
      {
      List<string> tenantList = ProtUtils.GetTenantList();

      DBConnection dbConfig = new DBConnection();
      dbConfig.Tenant = "XXX";

      ArrayList listaUtenti = new ArrayList();

      List<string> urlList = IndirizziProtocolliSQL.GetListaIndirizziSiti(dbConfig);
      }
      }
      }


      The logic is implemented into the Execute() method that retrieves a list of string from the DB. The IndirizziProtocolliSQL is a class of my MyPrjSqlEngine DAO project.



      This is the code of the called method implemented in this DAO project:



      public static List<string> GetListaIndirizziSiti(DBConnection dbConf)
      {
      string url = null;

      List<string> urlList = new List<string>();

      string query = PROT_INDIRIZZI_PROTOCOLLI.SELECT_LISTA_INDIRIZZI_SITI;
      using (SqlConnection con = PrjConnection.getStringConnection(dbConf.Tenant + "_PROTOCOLLO"))
      {
      using (SqlCommand cmd = new SqlCommand(query, con))
      {
      try
      {
      con.Open();

      using (SqlDataReader reader = cmd.ExecuteReader())
      {
      if (reader.HasRows)
      {
      while (reader.Read())
      {
      url = reader.GetString(reader.GetOrdinal("Url"));
      urlList.Add(url);
      }
      }
      }
      }
      catch (Exception ex)
      {
      throw ex;
      }
      finally
      {
      con.Close();
      }
      }
      }
      return urlList;
      }


      As you can see, here it obtain the connection to the DB:



      using (SqlConnection con = PrjConnection.getStringConnection(dbConf.Tenant + "_PROTOCOLLO"))


      This is the code of the getStringConnection() method (defined always in the same DAO project):



      [MethodImpl(MethodImplOptions.Synchronized)]
      public static SqlConnection gettringConnection(string name)
      {
      SqlConnectionStringBuilder bu = new SqlConnectionStringBuilder();

      XmlDocument config = new XmlDocument();
      config.Load(@"C:Program FilesInformation Workers GroupMyPrj" + MyPrjSqlEngine.Properties.Resources.Progetto + @"cfgconfig.xml");

      XmlNode xmlRoot = config.SelectSingleNode("connection_config");
      XmlNode root = xmlRoot.SelectSingleNode("database");
      string user = root.SelectSingleNode("user").InnerText;
      string passwordCrypted = root.SelectSingleNode("password").InnerText;
      string password = Crypto.Crypto.DecriptAes(passwordCrypted);
      string server = root.SelectSingleNode("server").InnerText;
      string database = name;

      bu.DataSource = @server;

      bu.InitialCatalog = database;

      bu.IntegratedSecurity = false; // Sql Server Authentification

      bu.UserID = user;

      bu.Password = password;
      bu.MultipleActiveResultSets = true;
      SqlConnection mycon = new SqlConnection();
      mycon.ConnectionString = bu.ConnectionString;

      return mycon;
      }


      As you can see in this method it retrieves and load an XML configuration file that contains the database connection info, something like this:



      <connection_config>
      <database>
      <user>MY-USER</user>
      <password>CRYPTED-PSWD</password>
      <server>DB-SERVER-NAME</server>
      </database>
      </connection_config>


      and then retrieve these information from this XML file and use it to create a DB connection string.



      As explained at the beginning of the post, it works fine performing the query from the SharePoint application (I execute the job and the query is performed as I expect, no problem).



      The problem is: then I defined a new NUnit in my solution, to test the DAO methods as integration test, so in this test project I have this class:



      namespace Tests
      {
      public class Tests
      {
      [SetUp]
      public void Setup()
      {
      }

      [Test]
      public void Test1()
      {
      Assert.Pass("PASSED");
      }


      [Test]
      public void GetIndirizzoProtocollo_XXX_YYY_ReturnSingleValue()
      {
      DBConnection dbConfig = new DBConnection();
      dbConfig.Tenant = "XXX";

      List<string> urlList = IndirizziProtocolliSQL.GetListaIndirizziSiti(dbConfig);

      Assert.Pass("OK");
      }

      }
      }


      At the moment it contains only a minimalistic test method. As you can see I am testing the same DAO method called from my timer job.



      Debugging the execution of this test method I have that:




      1. I correctly enter into the IndirizziProtocolliSQL.GetListaIndirizziSiti() DAO method.


      2. Here it correctly enter into the getStringConnection() method where start to correctly retrieve the information from the XML configuration file.



      3. Now the passwordCrypted variable contains the crypted password retrieved from the XML document. Then it try to perform this line to decrypt the crypted password:



        string password = Crypto.Crypto.DecriptAes(passwordCrypted);


        and now I obtain the following exception in the stacktrace:



        Exception thrown: 'System.TypeInitializationException' in MyPrjSqlEngine.dll
        An exception of type 'System.TypeInitializationException' occurred in MyPrjSqlEngine.dll but was not handled in user code
        The type initializer for '<Module>' threw an exception.



      In the popup on the line where the exception happen I have a message like this:



          System.TypeInitializationException: 'The type initializer for '<Module>' threw an exception.'
      EntryPointNotFoundException: A library name must be specified in a DllImport attribute applied to non-IJW methods.


      The inner exception is:



      -       InnerException  {<CrtImplementationDetails>.ModuleLoadException: The C++ module failed to load.
      ---> System.EntryPointNotFoundException: A library name must be specified in a DllImport attribute applied to non-IJW methods.
      at _getFiberPtrId()
      at <CrtImplementationDetails>.LanguageSupport._Initialize(LanguageSupport* )
      at <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* )
      --- End of inner exception stack trace ---
      at <CrtImplementationDetails>.ThrowModuleLoadException(String errorMessage, Exception innerException)
      at <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* )
      at .cctor()} System.Exception {<CrtImplementationDetails>.ModuleLoadException}


      Debugging this unit test I have that when it try to execute this line:



      string password = Crypto.Crypto.DecriptAes(passwordCrypted);


      I can't do Step Into this DecriptAes() method. I don't know why because it is into a class defined into the same MyPrjSqlEngine project.



      It seems that it can't find this Crypto class or something like this.



      This Cyrpto class is defined into the same MyPrjSqlEngine pèroject but in another namesapace:



      namespace MyPrjSqlEngine.Crypto
      {
      public class Crypto
      {
      ........................
      ........................
      ........................
      }
      }


      Why I have this problem calling from unit test and I have not calling directly from the SharePoint timer job? How can I try to fix it?










      share|improve this question














      I am pretty new in .NET and SharePoint (I came from Java EE environment) and I have the following strange problem working on a SharePoint 2013 project.



      Basically in my SharePoint application I access to a SQL Server database using some classes into a DAO project (that is in my solution). This DAO project is based on ADO.NET. It works fine. The prolem is that I created a new NUnit project in my solution because I want to test this DAO method defined in my DAO project performing some integration test. In this case I am obtaining a strange exception.



      I try to explain you my situation in details.



      In my solution I have a project that act as DAO (it contains all the databse query and handle the DB connection). This project is called something like MyPrjSqlEngine. Then I have another project called MyMainPrj that contains some timer job. In this main project I have a timer job containing the following logic:



      namespace MyMainPrj.TimerJob.Notifiche
      {
      public class NotificheGiornaliereView : SPJobDefinition
      {


      public NotificheGiornaliereView() : base() { }

      public NotificheGiornaliereView(string jobName, SPService service) :
      base(jobName, service, null, SPJobLockType.None)
      {
      this.Title = "Notifiche giornaliere protocollo View";
      }

      public NotificheGiornaliereView(string jobName, SPWebApplication webapp) :
      base(jobName, webapp, null, SPJobLockType.ContentDatabase)
      {
      this.Title = "Notifiche giornaliere protocollo View";
      }

      public override void Execute(Guid targetInstanceId)
      {
      List<string> tenantList = ProtUtils.GetTenantList();

      DBConnection dbConfig = new DBConnection();
      dbConfig.Tenant = "XXX";

      ArrayList listaUtenti = new ArrayList();

      List<string> urlList = IndirizziProtocolliSQL.GetListaIndirizziSiti(dbConfig);
      }
      }
      }


      The logic is implemented into the Execute() method that retrieves a list of string from the DB. The IndirizziProtocolliSQL is a class of my MyPrjSqlEngine DAO project.



      This is the code of the called method implemented in this DAO project:



      public static List<string> GetListaIndirizziSiti(DBConnection dbConf)
      {
      string url = null;

      List<string> urlList = new List<string>();

      string query = PROT_INDIRIZZI_PROTOCOLLI.SELECT_LISTA_INDIRIZZI_SITI;
      using (SqlConnection con = PrjConnection.getStringConnection(dbConf.Tenant + "_PROTOCOLLO"))
      {
      using (SqlCommand cmd = new SqlCommand(query, con))
      {
      try
      {
      con.Open();

      using (SqlDataReader reader = cmd.ExecuteReader())
      {
      if (reader.HasRows)
      {
      while (reader.Read())
      {
      url = reader.GetString(reader.GetOrdinal("Url"));
      urlList.Add(url);
      }
      }
      }
      }
      catch (Exception ex)
      {
      throw ex;
      }
      finally
      {
      con.Close();
      }
      }
      }
      return urlList;
      }


      As you can see, here it obtain the connection to the DB:



      using (SqlConnection con = PrjConnection.getStringConnection(dbConf.Tenant + "_PROTOCOLLO"))


      This is the code of the getStringConnection() method (defined always in the same DAO project):



      [MethodImpl(MethodImplOptions.Synchronized)]
      public static SqlConnection gettringConnection(string name)
      {
      SqlConnectionStringBuilder bu = new SqlConnectionStringBuilder();

      XmlDocument config = new XmlDocument();
      config.Load(@"C:Program FilesInformation Workers GroupMyPrj" + MyPrjSqlEngine.Properties.Resources.Progetto + @"cfgconfig.xml");

      XmlNode xmlRoot = config.SelectSingleNode("connection_config");
      XmlNode root = xmlRoot.SelectSingleNode("database");
      string user = root.SelectSingleNode("user").InnerText;
      string passwordCrypted = root.SelectSingleNode("password").InnerText;
      string password = Crypto.Crypto.DecriptAes(passwordCrypted);
      string server = root.SelectSingleNode("server").InnerText;
      string database = name;

      bu.DataSource = @server;

      bu.InitialCatalog = database;

      bu.IntegratedSecurity = false; // Sql Server Authentification

      bu.UserID = user;

      bu.Password = password;
      bu.MultipleActiveResultSets = true;
      SqlConnection mycon = new SqlConnection();
      mycon.ConnectionString = bu.ConnectionString;

      return mycon;
      }


      As you can see in this method it retrieves and load an XML configuration file that contains the database connection info, something like this:



      <connection_config>
      <database>
      <user>MY-USER</user>
      <password>CRYPTED-PSWD</password>
      <server>DB-SERVER-NAME</server>
      </database>
      </connection_config>


      and then retrieve these information from this XML file and use it to create a DB connection string.



      As explained at the beginning of the post, it works fine performing the query from the SharePoint application (I execute the job and the query is performed as I expect, no problem).



      The problem is: then I defined a new NUnit in my solution, to test the DAO methods as integration test, so in this test project I have this class:



      namespace Tests
      {
      public class Tests
      {
      [SetUp]
      public void Setup()
      {
      }

      [Test]
      public void Test1()
      {
      Assert.Pass("PASSED");
      }


      [Test]
      public void GetIndirizzoProtocollo_XXX_YYY_ReturnSingleValue()
      {
      DBConnection dbConfig = new DBConnection();
      dbConfig.Tenant = "XXX";

      List<string> urlList = IndirizziProtocolliSQL.GetListaIndirizziSiti(dbConfig);

      Assert.Pass("OK");
      }

      }
      }


      At the moment it contains only a minimalistic test method. As you can see I am testing the same DAO method called from my timer job.



      Debugging the execution of this test method I have that:




      1. I correctly enter into the IndirizziProtocolliSQL.GetListaIndirizziSiti() DAO method.


      2. Here it correctly enter into the getStringConnection() method where start to correctly retrieve the information from the XML configuration file.



      3. Now the passwordCrypted variable contains the crypted password retrieved from the XML document. Then it try to perform this line to decrypt the crypted password:



        string password = Crypto.Crypto.DecriptAes(passwordCrypted);


        and now I obtain the following exception in the stacktrace:



        Exception thrown: 'System.TypeInitializationException' in MyPrjSqlEngine.dll
        An exception of type 'System.TypeInitializationException' occurred in MyPrjSqlEngine.dll but was not handled in user code
        The type initializer for '<Module>' threw an exception.



      In the popup on the line where the exception happen I have a message like this:



          System.TypeInitializationException: 'The type initializer for '<Module>' threw an exception.'
      EntryPointNotFoundException: A library name must be specified in a DllImport attribute applied to non-IJW methods.


      The inner exception is:



      -       InnerException  {<CrtImplementationDetails>.ModuleLoadException: The C++ module failed to load.
      ---> System.EntryPointNotFoundException: A library name must be specified in a DllImport attribute applied to non-IJW methods.
      at _getFiberPtrId()
      at <CrtImplementationDetails>.LanguageSupport._Initialize(LanguageSupport* )
      at <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* )
      --- End of inner exception stack trace ---
      at <CrtImplementationDetails>.ThrowModuleLoadException(String errorMessage, Exception innerException)
      at <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* )
      at .cctor()} System.Exception {<CrtImplementationDetails>.ModuleLoadException}


      Debugging this unit test I have that when it try to execute this line:



      string password = Crypto.Crypto.DecriptAes(passwordCrypted);


      I can't do Step Into this DecriptAes() method. I don't know why because it is into a class defined into the same MyPrjSqlEngine project.



      It seems that it can't find this Crypto class or something like this.



      This Cyrpto class is defined into the same MyPrjSqlEngine pèroject but in another namesapace:



      namespace MyPrjSqlEngine.Crypto
      {
      public class Crypto
      {
      ........................
      ........................
      ........................
      }
      }


      Why I have this problem calling from unit test and I have not calling directly from the SharePoint timer job? How can I try to fix it?







      c# .net sharepoint nunit sharepoint-2013






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 31 '18 at 10:53









      AndreaNobiliAndreaNobili

      13.1k57181327




      13.1k57181327
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53986596%2fwhy-have-i-this-typeinitializationexception-calling-a-method-in-the-execution-of%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53986596%2fwhy-have-i-this-typeinitializationexception-calling-a-method-in-the-execution-of%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Mossoró

          Error while reading .h5 file using the rhdf5 package in R

          Pushsharp Apns notification error: 'InvalidToken'