Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing from outside of a controller #46

Open
MattHartz opened this issue Nov 13, 2017 · 3 comments
Open

Accessing from outside of a controller #46

MattHartz opened this issue Nov 13, 2017 · 3 comments

Comments

@MattHartz
Copy link

Hi, I'm trying to access my websockets from outside in another service. Is there a way similar to how SignalR allow you to access Hubs via IHubContext?

Thanks!

@radu-matei
Copy link
Owner

Take a look at this sample here

Basically, you can have a messageHandler injected via constructor injection in any class you need.

@MattHartz
Copy link
Author

MattHartz commented Nov 13, 2017

hmm, well for my example, i'm utilizing Service Fabric which has this code in Program.cs. Which I want to pass the handlers into the constructor of "Service", but at this point Startup.cs has yet to be called therefore the handlers have yet been stored in the IOC container... does that make sense?

private static void Main()
{
	try
	{
		// The ServiceManifest.XML file defines one or more service type names.
		// Registering a service maps a service type name to a .NET type.
		// When Service Fabric creates an instance of this service type,
		// an instance of the class is created in this host process.

		ServiceRuntime.RegisterServiceAsync("ServiceType",
			context => new Service(context)).GetAwaiter().GetResult();

		ServiceEventSource.Current.ServiceTypeRegistered(Process.GetCurrentProcess().Id, typeof(NotificationAPIService).Name);

		// Prevents this host process from terminating so services keeps running. 
		Thread.Sleep(Timeout.Infinite);
	}
	catch (Exception e)
	{
		ServiceEventSource.Current.ServiceHostInitializationFailed(e.ToString());
		throw;
	}
}

@MattHartz
Copy link
Author

Would i need to do something like such inside of Program.cs?

public static void Main(string[] args)
    {
        //setup our DI
        var serviceProvider = new ServiceCollection()
            .AddLogging()
            .AddSingleton<IFooService, FooService>()
            .AddSingleton<IBarService, BarService>()
            .BuildServiceProvider();

        //configure console logging
        serviceProvider
            .GetService<ILoggerFactory>()
            .AddConsole(LogLevel.Debug);

        var logger = serviceProvider.GetService<ILoggerFactory>()
            .CreateLogger<Program>();
        logger.LogDebug("Starting application");

        //do the actual work here
        var bar = serviceProvider.GetService<IBarService>();
        bar.DoSomeRealWork();

        logger.LogDebug("All done!");

    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants