patterncsharpMinor
Send message with SignalR to client(s) from injected (IoC) method
Viewed 0 times
iocmethodwithmessageinjectedclientsignalrfromsend
Problem
I am new with
In my web app I have a
During this Setup process I want to inform the client of the progress. This is my current implementation and it works fine:
SignalR and still a newbie with IoC Container SimpleInjector and I am wondering if there are any side effects and/or if I'm save with my chosen approach.In my web app I have a
SetupHub that invokes a method on an object resolved with SimpleInjector. This method may do anything that I'm not aware about. Maybe it contains Tasks and it may consume x units of time.During this Setup process I want to inform the client of the progress. This is my current implementation and it works fine:
public class SetupHub : Hub
{
public void Start()
{
// My SimpleInjector resolve wrapper
IocContainer.Instance.GetInstance().Start(UpdateProgress);
}
private void UpdateProgress(double percentage)
{
Clients.Caller.updateProgress(percentage);
}
}
public class Setup : ISetup
{
public void Start(Action updateProgress)
{
Task.Factory.StartNew(() =>
{
updateProgress(10); // Bogus progress
Thread.Sleep(100); // Simulate x units of time
CreateRoles();
updateProgress(25);
Thread.Sleep(400);
CreateUsers();
updateProgress(60);
Thread.Sleep(200);
CreateRelations();
updateProgress(100);
//CreateRelationGroups();
});
}
}
public interface ISetup
{
void Start(Action updateProgress);
}Solution
Currently, your setup doesn't allow for any cancellation or error handling. The latter is an implementation detail, so I guess you've handled that in the actual code. I would consider the following:
- Return the started task out of
Start
- Will you ever need to provide a result from the started task? If so, provide an overload for
Func
- Provide cancellation support via a cancellation token (if business logic dictates this is acceptable)
- Provide a
Stopmethod that also that can stop the task created byStart
Context
StackExchange Code Review Q#23881, answer score: 6
Revisions (0)
No revisions yet.