hegde
hegde

Reputation: 3

Mypage.aspx.cs throws a compiler error: class does not contain a constructor that takes 0 argument

MyPage.aspx.cs:

public partial class Mypage: System.Web.UI.Page
{
    public IConfigurationClient _configurationClient;
 
    public MyPage(IConfigurationClient configurationClient )
    {
        _configurationClient = configurationClient ;
    }
 
    //rest of the code
}

I am not sure what the issue is. I have also tried extending the base constructor with no arguments, but it still does not work.

public MyPage(IConfigurationClient configurationClient) : base()
{
    _configurationClient = configurationClient ;
}

Furthermore, I have tried adding a parameterless constructor which solves the issue but when I call _configurationClient.GetConfigAsync() it gives an object reference error as _configurationClient is null.

Upvotes: 0

Views: 32

Answers (1)

M.B.
M.B.

Reputation: 1082

If this is the only constructor available in code, it won't work because a constructor with zero arguments is expected. You could inject the needed argument using method injection instead of constructor injection.

public void SetConfigurationClient(IConfigurationClient client)
{
    _configurationClient = client;
}

Upvotes: 2

Related Questions