aspnet / SignalR

[Archived] Incredibly simple real-time web for ASP.NET Core. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
2.38k stars 446 forks source link

Dependency Injection in Hubs #68

Closed harisbotic closed 7 years ago

harisbotic commented 7 years ago

I wanted to access some repositories/services from within a hub, is that possible now? Will you implement this feature later?

BrennanConroy commented 7 years ago

This should be possible, give it a try.

harisbotic commented 7 years ago
public class Chat : Hub
{
        private IP2PRepository _p2pRepo;

        public Chat(IP2PRepository p2pRepo)
        {
            _p2pRepo = p2pRepo;
        }

        public override Task OnConnectedAsync()
        {
            System.Console.WriteLine("SOMEONE CONNECTED");
            return Task.CompletedTask;
        }

        public override Task OnDisconnectedAsync()
        {
            System.Console.WriteLine("SOMEONE DISCONNECTED");
            return Task.CompletedTask;
        }
}

and the Error in console is


System.MissingMethodException: No parameterless constructor defined for this object.
   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance[T]()
   at Microsoft.AspNetCore.SignalR.HubEndPoint`2.<OnConnectedAsync>d__8.MoveNext()```
BrennanConroy commented 7 years ago

Oh yeah, I fixed that a few days ago. What version of Microsoft.AspNetCore.SignalR are you using?

harisbotic commented 7 years ago

Saw it just now... Thank you, I fixed it

harisbotic commented 7 years ago

And now, I can't get of this error whenever someone tries to connect

System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.AspNetCore.SignalR.JsonNetInvocationAdapter.<>c__DisplayClass2_0.<ReadInvocationDescriptorAsync>b__0()
   at System.Threading.Tasks.Task`1.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.SignalR.HubEndPoint`2.<DispatchMessagesAsync>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.SignalR.HubEndPoint`2.<OnConnectedAsync>d__8.MoveNext()
BrennanConroy commented 7 years ago

Not related to this issue, look at #64 and #62

moozzyk commented 7 years ago

@harisbotic - hub method names are no longer fully qualified names but just method names.

avijassra commented 6 years ago

I am having the same issue. I am not able to use DI in SignalR (1.0.0-alpha2-final) with AspNetCore (2.0.0). Any pointers

davidfowl commented 6 years ago

Can you make a minimal repro of the issue on GitHub?

avijassra commented 6 years ago

@davidfowl, i tried to create new project using "dotnet new angular" cmd on VS Code. Now its throwing a weird error on the start of the application.

**

An unhandled exception occurred while processing the request.

NodeInvocationException: Uncaught (in promise): Error: Cannot resolve '/chat'. Error: Cannot resolve '/chat'.

**

Please find the repo @ https://github.com/avijassra/signalrDi. Thanks

avijassra commented 6 years ago

@davidfowl , when i am trying to start the connection, i am getting this new error .... at this line

File -> https://github.com/avijassra/signalrDi/blob/v1-reboot/ClientApp/app/services/pubsub.service.ts#L16

this.connection = new HubConnection('chat'); //this.startedConnection = this.connection.start();

File -> https://github.com/avijassra/signalrDi/blob/v1-reboot/Startup.cs#L47

app.UseSignalR(routes => routes.MapHub("chat"));

This is different issue then my DI issue as mentioned earlier .... thanks

davidfowl commented 6 years ago

@avijassra why not file a different bug then?

avijassra commented 6 years ago

@davidfowl Done

avijassra commented 6 years ago

Singleton not working in my app was a code issue and not SignalR issue. Thanks to @davidfowl for your time and help.