Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

How can I use/inject a service in a "normal" c# class like in Blazor @inject ClassName classObject

$
0
0

I have a Blazor Project, in the Program.cs(formaly aka Startup.cs) I added a service

builder.Services.AddSingleton<Models.UserVisit>();

I can use/access that service on a razor/blazor page like this :

@inject Models.UserVisit userVisitObj

(UserVisit is a "normal" C# .cs Class in the folder Models)

What I don't know is how can I use this "userVisitObj" in a normal C# Class that does not have a razorpage (where I would use the @inject)?

How do I use that in here (normal C# Class in the same project but without a blazor/razor-componentpage):

public class UserModel{    [BsonId]    public Guid Id { get; set; }    public CultureInfo UserCultureInfo { get; set; }    ...    public UserModel()    {        [Inject]        Models.UserVisit userVisitObj;   // THAT DOESN'T WORK -- ERROR    }}

I hope I could make my question somewhat clear (please be kind I'm still a beginner).


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>