title: How to fix ‘Object reference not set to an instance of an object’ error in C# Web Service SSIS - Impersonation
body: <p>I’m building a web service application that executes an SSIS package, but when I try to consume the service (published to IIS on localhost as Web Deploy - File System) I get this response (locally and from the outside):</p>
  
  <blockquote>
    <p><strong>System.NullReferenceException: Object reference not set to an instance of an object.</strong>
       at <strong>WebServiceApplication.CallSP.RunSSIS()</strong> in C:\Users\developers\source\repos\ajfmo\WebServiceApplication\WebServiceApplication\CallSP.asmx.cs: <strong>line 23</strong></p>
  </blockquote>
  
  <p>Which refers to this Line: </p>
  
  <pre><code>string connectionString = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
  </code></pre>
  
  <p>I've read all the other questions so in this case is <strong>not a duplicate</strong>.</p>
  
  <p>This is my web.config setting for the connection.</p>
  
  <pre><code>&lt;connectionStrings&gt;
      &lt;clear /&gt;
      &lt;add name="DBConnection"
           connectionString="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=master;"
           providerName="System.Data.SqlClient"/&gt;
  &lt;/connectionStrings&gt;
  </code></pre>
  
  <p>But when I'm debugging from VS it runs without any issue, Is it related to the IIS setup?.</p>
  
  <p>Already tried <a href="https://docs.microsoft.com/en-us/sql/integration-services/run-manage-packages-programmatically/loading-and-running-a-remote-package-programmatically?view=sql-server-2017#service" rel="nofollow noreferrer">(Using a Web Service or Remote Component to Run a Remote Package Programmatically)</a> and got compilation errors - also don't understand the parameter that I need to pass.</p>
  
  <p>This is my code right now.</p>
  
  <pre><code>[WebMethod]
      public string RunSSIS()
      {
          var connectionString = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
          using (var connection = new SqlConnection(connectionString))
          {
              try
              {
                  // Create the Integration Services object
                  IntegrationServices integrationServices = new IntegrationServices(connection);
                  // Get the Integration Services catalog
                  Catalog catalog = integrationServices.Catalogs["SSISDB"];
                  // Get the folder
                  CatalogFolder folder = catalog.Folders["SF2SQL"];
                  // Get the project
                  ProjectInfo project = folder.Projects["SF2SQL"];
                  // Get the package
                  PackageInfo package = project.Packages["Package.dtsx"];
                  // Run the package
                  long executionIdentifier = package.Execute(false, null);
                  // Checking completion status
                  ExecutionOperation executionOperation = catalog.Executions[executionIdentifier];
                  while (!executionOperation.Completed)
                  {
                      System.Threading.Thread.Sleep(5000);
                      System.Diagnostics.Debug.WriteLine("Running");
                      executionOperation.Refresh();
                  }
                  if (executionOperation.Status == Operation.ServerOperationStatus.Success)
                  {
                      System.Diagnostics.Debug.WriteLine("Success");
                      return "Success";
                  }
                  else if (executionOperation.Status == Operation.ServerOperationStatus.Failed)
                  {
                      System.Diagnostics.Debug.WriteLine("Failed");
                      return "Failed";
                  }
                  else
                  {
                      System.Diagnostics.Debug.WriteLine("Something else…");
                      return "Something else…";
                  }
              }
              catch (SqlException e)
              {
                  System.Diagnostics.Debug.WriteLine("SqlException: " + e.Message);
                  return "Ooopss";
              }
              finally
              {
                  connection.Close();
              }
          }
      }
  </code></pre>
  
  <p>I had use breakpoints and debugged in those lines, the result in the variable is Null, but if I hardcode the value or pass the string directly to the SqlConnection instance it also gives me <strong>System.NullReferenceException:  Object reference not set to an instance of an object.</strong></p>
  
  <p>Please help. </p>