title: How to run non-static code that uses dependency injection in .net core
body: <p>I created a new console app in VS2017 and I am trying to run this code to demonstrate I can encrypt and decrypt strings in .NET Core. I have tried calling RunSample from the Program.cs Main but it wants it to be a static method. If I make RunSample static then I'm getting a null reference exception when trying to set the var protectedpayload.</p>
  
  <pre><code>using System;
  using Microsoft.AspNetCore.DataProtection;
  
  public class MyClass
  {
      private readonly IDataProtector _protector;
  
      public MyClass(IDataProtectionProvider provider)
      {
          _protector = provider.CreateProtector("Contoso.MyClass.v1");
      }
  
      public void RunSample()
      {
          Console.WriteLine("Enter input:");
          string input = Console.ReadLine();
  
          var protectedPayLoad = _protector.Protect(input);
          Console.WriteLine($"Protect returned: {protectedPayLoad}");
  
          var unprotectedPayLoad = _protector.Unprotect(protectedPayLoad);
          Console.WriteLine($"Unprotect returned: {unprotectedPayLoad}");
      }
  }
  </code></pre>
  
  <p>How can I run it?</p>
  
  <p>UPDATE:
  Trying to run it from Program.cs, I have the following but <code>.MyClass</code> has a "cannot resolve" syntax error / red underline on it.</p>
  
  <pre><code>using System;
  
  namespace encrypttest
  {
      class Program
      {
          static void Main(string[] args)
          {
              Console.WriteLine("Hello World!");
              new Program().MyClass.RunSample();
          }
      }
  }
  </code></pre>
  
  <p>UPDATE 2:</p>
  
  <p>I created my app by going to new project, .NET Core and then Console App (.NET Core):
  <a href="https://i.stack.imgur.com/nVDZ0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nVDZ0.png" alt="enter image description here"></a></p>
  
  <p>UPDATE 3:
  I've changed my code as suggested but have the following error:
  <a href="https://i.stack.imgur.com/JKrKi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JKrKi.png" alt="enter image description here"></a></p>
  
  <p>UPDATE 4:
  I've removed the using as suggested, but now I get:
  <a href="https://i.stack.imgur.com/BFuik.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BFuik.png" alt="enter image description here"></a></p>