Can a Winforms/WPF app act as HTTP server?

2019-04-12 16:02发布

问题:

I have a desktop written with Winforms. Now I have requests that people want to access the data from other machines. First I thought about a regular Client/Server app but now I think best would be if the app could act as HTTP server and send HTML to connected browsers.

Does anybody know if there is a library available to add HTTP server functionality to an app? Ideally it would be able to serve ASP.NET content.

回答1:

To host ASP.NET content (such as .aspx pages) from your own application (without IIS), use the classes in the System.Web.Hosting namespace.

The canonical example usage of this is the standalone testing web server that comes with Visual Studio (based on "Cassini"). Source code for "Cassini" it is available here: http://blogs.msdn.com/dmitryr/archive/2008/10/03/cassini-for-framework-3-5.aspx

Here is a (somewhat dated) MSDN article on the subject: http://msdn.microsoft.com/en-us/magazine/cc188791.aspx

Expecting your server to be as robust as IIS is a long shot. So be sure to carefully evaluate your needs and manage your expectations :)



回答2:

As Jon has said, you can use HttpListener to implement a HTTP server. For serving ASP.NET content, see the System.Web.Hosting namespace.

One thing to be aware of with your design is that your HTTP server will be available only while the app is running. So when the user of the desktop app closes it or logs off, other people will no longer be able to access the data via your Web interface. You may therefore want to reconsider your approach and instead factor out the data access functionality into a separate DLL that you can then call from an IIS-hosted ASP.NET app.



回答3:

You can absolutely do this. Have a look at HttpListener. I don't think it can easily host full-on ASP.NET, although I'm sure there are ways of doing that. Sending simple HTML isn't hard though.

Having said that, I'm not sure that client/server wouldn't be a better model:

  • How are other people going to know which machine to connect to?
  • What if you accidentally close down the application when other people are using it?
  • What if you want to shut down the machine or reboot it?
  • It's likely to be more complicated to run than a straight ASP.NET application.

Basically all the tools are geared up to make it easy to host ASP.NET on a server. Where's the benefit in merging the client and server here?



回答4:

You might consider factoring out the HttpListener code and developing a Windows Service that is up while your machine is up. This would allow you to serve HTML in a long-lived fashion without having to have your desktop application always running and without the need to acquire a web server like IIS.

If you want to allow communication with a broader heterogeneity of clients than just browsers, look into WCF. Using WCF, a service can be hosted by any .NET application; beit Winforms, WPF, Windows Service, ASP.NET, Console, etc., allowing communication over various mediums HTTP, TCP, named pipes, etc.



回答5:

I'd recommend going a different route: change your desktop app to talk to a commercial database server like sql server express, and then build your web site to talk to that database as well.



标签: .net http