I want to send data from an existing project over HTTP. To do that I decided to use OWIN to self-host a web server from that particular project (I followed this, not sure this is the best way though). This existing project has some objects that need to serve as a model for the controller's web API. My problem is that I don't know how to pass an existing object to a controller. Dependency injection seems to only allow me to create new objects, furthermore the object is only available at the point where I start the web app. Here is the code
private void Initialize()
{
// TODO: this object needs to somehow end up in the controllers.
CustomObject object = base.object;
_webApp = WebApp.Start(baseAddress, Startup);
}
private void Startup(IAppBuilder appBuilder)
{
// Configure Web API for self-host.
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute(
name: "REST API",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Services.Replace(typeof(IAssembliesResolver), new MyAssembliesResolver());
appBuilder.UseWebApi(config);
}
public class ValuesController : ApiController
{
// TODO: so this needs to be filled in with the object from the Initialize() method above.
private CustomObject _customObject;
// GET api/values
public IEnumerable<string> GetAll()
{
// I want to use this customObject to send information about the project that is actually hosting the web api.
// return _customObject.Data;
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…