An object reference is required for the nonstatic ...
# help-with-umbraco
m
In an Umbraco 10 razor page I have:
@using Sitename.App_Code.Logic
async void VideoGated() { if (Model.Value("gated") && !GatedAccessLogic.UserIsRegistered()) { @await Component.InvokeAsync("GatedAccess") } }` and then this in the GatedAccessLogic.cs file:
Copy code
public class GatedAccessLogic
    {
        private readonly IScopeProvider scopeProvider;      

        public GatedAccessLogic(IScopeProvider scopeProvider)
        {
            this.scopeProvider = scopeProvider;            
        }
public bool UserIsRegistered()
        {                          
                using (var scope = scopeProvider.CreateScope(autoComplete: true))
                {
                    var db = scope.Database;
                    string strSProc = String.Format(";EXEC sp_gac_check @@gacGuid={0}, @@gac_isValid=1", gacGuid);
                    
                    etc etc
                }
         
            return true
        }
However I'm getting an error on the method call:GatedAccessLogic.UserIsRegistered() - "An object reference is required for the nonstatic field, method, or property 'member'". I tried var gatedAccessLogic = new GatedAccessLogic(); in VideoGated() but that just leads to another error that "there is no arguement given that corresponds to the required parameter scopeprovider". Feel like I'm missing something basic. Thanks!
d
Hi there! Reading your example code, I think you need to get a little bit more familiar with dependency injection. I expect you need to register your GatedAccessLogic class in the dependency injection container and inject an instance of your class into your razor view. You can read about injecting services into views here: https://learn.microsoft.com/en-us/aspnet/core/mvc/views/dependency-injection?view=aspnetcore-7.0#service-injection
c
Or here, even, if you want to be Umbraco specific, which I guess you do 😉 https://docs.umbraco.com/umbraco-cms/reference/using-ioc
m
Thanks both of you - will check them out.