tags:

views:

55

answers:

5

Hi, 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.

A: 

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?

Jon Skeet
Jon,Your questions are valid. This a unique scenario where I want the app to look as client/server but I want to keep the option open to migrate to a full web app without too many code changes. The fact that clients can't connect when the app is down can be viewed as a "security feature" this particular app.
@user46703: Hmm. I suspect you'll find it's more of a hassle than a benefit to be honest. I think you'll find there are various downsides to developing it this way - not least in terms of tool support for a relatively unusual situation.
Jon Skeet
Most of the GUI is already written with the help of jQuery and it's running within a web browser control. This is a bit unusual but the content I have to display is very suitable for HTML and CSS.I can control the content my homebrew HTTP server so no bad surprises there. I think this will work.
+2  A: 

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.

itowlson
+3  A: 

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 :)

binarycoder
Thanks! This is cool stuff. Who would have thought that MS is shipping the plumbing for a full ASP.NET within the framework?
A: 

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.

Travis Heseman
A: 

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.

Joel Coehoorn
What if the user has a bunch of .aspx pages containing the logic needed to make sense out of the data? This is probably the reason for the question: to reuse as much of the existing code as possible.
binarycoder
Yes, I want to reuse my code. My local app does most of its GUI in a WebBrowser control with heavy use of jQuery.With the help of System.Web.Hosting I should be able to share not only the database code, but even most GUI code between my desktop app and a full web app.
If most of your business logic is in pages for a webbrowser control, then yes, disregard this. But I saw your winforms tags, which normally means the business logic is in the app, and that normally means re-implementing a lot of it anyway.
Joel Coehoorn