0

I'm trying to use SqlDependencyEx it accepts the first argument as a connection string, if I use my DB connection string directly it works but I don't want to give it directly instead I want it to grab DefaultConnection using Configuration. but I'm getting this error

A field initializer cannot reference the non-static field, method, or property 'SignalServer.connectionString' (CS0236)

public class SignalServer : Hub
        {
            public readonly TestController _testController;
            public IConfiguration Configuration { get; }
            string connectionString = "";

            public SignalServer(TestController testController, IConfiguration configuration)
            {
                Configuration = configuration;
                _testController = testController;
                connectionString = Configuration.GetConnectionString("DefaultConnection");
            }

            public async void NotifyConnection()
            {
                await Clients.All.SendAsync("TestBrodcasting", _testController.GetAllApps());
            }

            SqlDependencyEx sqlDependency = new SqlDependencyEx(connectionString);

        }

Update:

public class SignalServer : Hub
{
    public readonly TestController _testController;
    public IConfiguration Configuration { get; }

    public SignalServer(TestController testController, IConfiguration configuration)
    {
        Configuration = configuration;
        _testController = testController;
        SqlDependencyEx sqlDependency = new SqlDependencyEx(Configuration.GetConnectionString("DefaultConnection"), "dbName", "tablename");
    }

    public async void NotifyConnection()
    {
        await Clients.All.SendAsync("TestBrodcasting", _testController.GetAllApps());
    }


    private void RegisterNotification()
    {
        sqlDependency.TableChanged += OnDataChange;
        sqlDependency.Start();
    }

    private void UnregisterNotification()
    {
        sqlDependency.Stop();
        sqlDependency.TableChanged -= OnDataChange;
    }

    private void OnDataChange(object sender, SqlDependencyEx.TableChangedEventArgs e)
    {

    }

    private void Dispose()
    {
        UnregisterNotification();
    }
}
Sam
  • 471
  • 7
  • 24
  • 1
    I'm not sure what the question is. Is something unclear? You're trying to use `connectionString` when initializing `sqlDependency` and it's not static. – itsme86 Aug 28 '19 at 18:56
  • Hi Sam, does [this question](https://stackoverflow.com/questions/11015591/error-a-field-initializer-cannot-reference-the-non-static-field-method-or-pro) help you out? – KevinLamb Aug 28 '19 at 18:57
  • @itsme86 I'm trying to use `SqlDependencyEx` it accepts the first argument as a connection string, if I use my DB connection string directly it works but I don't want to give it directly instead I want it to grab `DefaultConnection` using `Configuration` – Sam Aug 28 '19 at 19:00
  • You can keep the `SqlDependencyEx sqlDependency` declaration where it is, but move the initialization to the constructor after you set `connectionString`: `sqlDependency = new SqlDependencyEx(connectionString);` – itsme86 Aug 28 '19 at 19:02
  • @KevinLamb not really it taking connection string directly I want it to grab from Configurations – Sam Aug 28 '19 at 19:04
  • @itsme86 can you show using my code ? – Sam Aug 28 '19 at 19:06

1 Answers1

4

Your problem is this line:

SqlDependencyEx sqlDependency = new SqlDependencyEx(connectionString);

Move it inside the constructor instead:

SqlDependencyEx sqlDependency;
public SignalServer(TestController testController, IConfiguration configuration)
{
    Configuration = configuration;
    _testController = testController;
    connectionString = Configuration.GetConnectionString("DefaultConnection");
    //Put it here
    sqlDependency = new SqlDependencyEx(connectionString);
}

I don't know much about your design but you probably don't even need to save the connection string in the connectionString field. You might be able to just use it directly from the call to the Configuration.GetConnectionString method or the reference to the Configuration object you are storing.

Gary Stewart
  • 164
  • 5
JuanR
  • 7,405
  • 1
  • 19
  • 30
  • if I use it directly gives this error: `An object reference is required for the non-static field, method, or property 'SignalServer.Configuration' (CS0120)` – Sam Aug 28 '19 at 19:13
  • @Sam: Yes. You still need to do it within the constructor as shown in my answer. – JuanR Aug 28 '19 at 19:19
  • @Sam Which line of code is causing that error? – Gary Stewart Aug 28 '19 at 19:19
  • @GaryStewart: I think his comment refers to the last paragraph of my answer. – JuanR Aug 28 '19 at 19:20
  • @JuanR, I'm just wondering if there is a namespacing issue since he's using the a variable called "Configuration" – Gary Stewart Aug 28 '19 at 19:21
  • @GaryStewart: I thought so too but moving the initialization of `sqlDependency` to the constructor as shown in my answer should solve any non-static issues. – JuanR Aug 28 '19 at 19:23
  • @JuanR I have updated my question even if I use your answer it still showing the same error. – Sam Aug 28 '19 at 19:27
  • @Sam: Your update is even worse. Just do what I put in my answer and remove the line that tries to create the dependency outside of the constructor. the line that assigns `sqlDependency` **should only exist in the constructor, nowhere else**. – JuanR Aug 28 '19 at 19:32
  • @GaryStewart the error is on `Configuration` – Sam Aug 28 '19 at 19:33
  • @JuanR I have updated my question again but now If I try to use `sqlDependency` in `RegisterNotification()` it is giving error `The name 'sqlDependency' does not exist in the current context (CS0103)` – Sam Aug 28 '19 at 19:37
  • @JuanR I got it thnx – Sam Aug 28 '19 at 19:47
  • @GaryStewart: Thank you for the correction on the variable. :-) – JuanR Aug 28 '19 at 20:17