I have troubles to understand dependecy injection with ASP.NET CORE. I don't understand how can I inject a signalR context in a class that I have made.
In particular this class is responsable of calling a client specific method when an event occour. But I have trouble to have an instance of the hub context in this class, and I'm not able to call the client methods.
public class ServerStatus
{
private readonly IHubContext<MyHub, ITypedHubClient> _hubContext;
private int idserver;
private string des;
private string ipserver;
private int attemptfailded = 0;
public ServerStatus(IHubContext<MyHub, ITypedHubClient> hubContext, int id, string description, string ip)
{
this.idserver = id;
this.des = description;
this.ipserver = ip;
_hubContext = hubContext;
}
...
public async Task SendMessageToClients()
{
await _hubContext.Clients.All.BroadcastMessage("Server",
"ServerDown");
}
}
I have done some research in this forum, and I have discovered that I can use a FactoryPattern to instantiate this classes with a hubcontext and so I have made this factory class:
public class ServerStatusFactory
{
private readonly IHubContext<MyHub, ITypedHubClient> _hubContext;
public ServerStatusFactory(IHubContext<MyHub, ITypedHubClient> hubContext)
{
_hubContext = hubContext;
}
public ServerStatus GetServerStatus(int id, string description, string ip)
{
return new ServerStatus(_hubContext, id, description, ip);
}
}
Now I'm not able to instatiate this class factory beacause I need to pass the hubcontext to the constructor of the factory class. So I thought that this class need to added to services of my app, like so:
services.AddScoped<ServerStatusFactory>();
But here I'm not sure this is the correct way and then how to use it to create new instances of ServerStatus object with the hubcontext in other classes that I have made.
Please can someone help me, by telling me how can I create and use this Factory class to instantiate my objects inside another class when I need them.
Thanks in advance.
EDIT
So, from my further research i discovered that there is a procedure to build up a service provider. See post: https://stackoverflow.com/a/45756192/10506397
Therefore, I create in a my custom methods in one of my class this peace of code:
public static class MyClass
{
public static List<ServerStatus> servers = new List<ServerStatus>();
public static void initializeServers()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton<IServerStatusFactory, ServerStatusFactory>(factory => new ServerStatusFactory(/* what arguments???? */));
// create service provider
var serviceProvider = serviceCollection.BuildServiceProvider();
ServerStatusFactory serverfactory = serviceProvider.GetService<ServerStatusFactory>();
ServerStatus s = serverfactory.GetServerStatus(/* my params */);
servers.Add(s);
}
}
But now I get an error when I execute the code above because serverfactory is null. I think this happen because I'm not passing the Hub in the correct way to the ServerStatusFactory.
I also have tried to add in the initializeServers:
serviceCollection.AddSingleton<IHubContext<MyHub, ITypedHubClient>>();
but I get this exception, when I buildServiceProvider:
System.ArgumentException
HResult=0x80070057
Message=Cannot instantiate implementation type
'Microsoft.AspNetCore.SignalR.IHubContext`2
This is my ConfigureServices:
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
...
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddSignalR();
services.AddSingleton<IConfiguration>(Configuration);
services.AddSingleton<IServerStatusFactory, ServerStatusFactory>( factory => new ServerStatusFactory(/* what arguments???? */);
MyClass.initializeServers();
}
User contributions licensed under CC BY-SA 3.0