RE: Pass a parameter to Startup class

357 views
Skip to first unread message

Chris R

unread,
Mar 21, 2013, 12:07:05 PM3/21/13
to net-http-a...@googlegroups.com
I think Louis set up a way to do this via dependency injection, but I'm a bit fuzzy on the details.  Louis?


Date: Wed, 20 Mar 2013 20:20:12 -0700
From: ate...@gmail.com
To: net-http-a...@googlegroups.com
Subject: Pass a parameter to Startup class

(sorry for my bad english) I am developing an app thats creates a http layer to comunicate with an external application. I already have a class library that communicate with the external application. Now I created a console application (for test purpouses) on wich I host a ASP.NET Web API and SignalR. This works fine, when I run the console app, I can access my controllers and signalR works perfeclty. Now I need to be able from a web api controller to access my external application but I also need to be able to control this application from the main app.
So, basically, what I need, on my app is this:

//MyClass controls the external application
MyClass myClass = new MyClass();
//how can I pass myClass instance to Startup?
WebApplication.Start<Startup>(url);

PS.: I cannot create another myClass instance inside my Controller, it needs to be the same instance used by the application hosting it.


--
You received this message because you are subscribed to the Google Groups ".NET HTTP Abstractions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to net-http-abstrac...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Damian Hickey

unread,
Mar 22, 2013, 10:58:19 AM3/22/13
to net-http-a...@googlegroups.com
You can do something like this:

IServiceProvider serviceProvider = DefaultServices.Create(defaultServiceProvider => defaultServiceProvider.Add<IMyService, MyServiceImpl>());
using (WebApplication.Start<Startup>(services: serviceProvider))
{
Console.ReadLine();
}

Or you can implement your own System.IServiceProvider over your container of choice.

Leo Mck

unread,
Mar 22, 2013, 6:52:27 PM3/22/13
to net-http-a...@googlegroups.com
This worked perfectly, thanks!

Louis DeJardin

unread,
Mar 22, 2013, 7:49:15 PM3/22/13
to net-http-a...@googlegroups.com

Ah, yeah.

 

Of course if you need literally the same instance, singleton IoC, then you’ll want to do the “implement your own System.IServiceProvider over your container of choice” and register it as singleton in your container… Otherwise the Startup class and you controller will get different instances of MyService.

 

If you check out the unit tests in katana you should find examples of implementing IServiceProvider for five or six different IoC container types. Those should be samples instead of unit tests, really, but it is what it is.

--

Reply all
Reply to author
Forward
0 new messages