Reputation: 9396
I have a requirement that my webservice (written in WebAPI) should print some data to the printer that is associated with my server.
(In short, my clients will send some data to my WebAPI service, and a printer in my domain (server domain) should print it.)
I host the service in IIS and use PrintManager to print the data. But, it seems we should not do that because the web services hosted in IIS should not actually communicate with local hardware (it seems to not be a recommended approach).
While there are many tutorials about self hosting WCF or WebAPI, I am trying to find out if I can use self hosting in this case.
Can i self host my WebAPI application in a Windows service so that the service will have rights to access the printer?
Upvotes: 2
Views: 4529
Reputation: 2696
Self hosting wcf services means that instead of relying on IIS to start your server, you start the service yourself as host.
Here is what MSDN says about hosts:
The role of the host application is to start and stop the service, listen for requests from clients, direct those requests to the service and send responses back to the clients.
The host application can be a console application, a windows service or anything else.
So to answer your question, yes, you could use a service for this purpose.
Upvotes: 3
Reputation: 8147
Self-hosting is the ability to host your Web API in an environment you control (WinForms, Console, Windows Service etc).
So, in short, Yes, you can self-host Web API solutions and you would do it with some code similar to the following:
var config = new HttpSelfHostConfiguration("http://localhost:8080");
config.Routes.MapHttpRoute(
"API Default", "api/{controller}/{id}",
new { id = RouteParameter.Optional });
using (HttpSelfHostServer server = new HttpSelfHostServer(config))
{
server.OpenAsync().Wait();
Console.WriteLine("Running on port 8080 - Press Enter to quit.");
Console.ReadLine();
}
Upvotes: 4