C# Web Server for displaying Console Output? - c#

I have just finished writing my c# console application, and I am contemplating embedding a web server into it (probably this one http://webserver.codeplex.com). I don't do much in the way over advanced web coding though, so I am not sure if I can do what I need to.
Basically, I would like to allow users to view the console output of my application in realtime just by visiting the site being served by my application. If I understand correctly, to do something like this it would require AJAX, which a simple C# Web Server wouldn't be able to handle.
Is this correct or is there an easy way to do this I am missing?

How to re-route console output
You will need to write your own TextWriter and make Console use it via Console.SetOut. This writer should notify connected web clients, as well as the original Console.Out.
How to host a COMET-like server
You can use HttpListener and some basic async programming to do this. If you wrap the HttpListenerContext.Response.OutputStream in a StreamWriter (with AutoFlush set to true) and set HttpListenerContext.Response.SendChunked to true clients will receive partial results - this means you can even do it in an IFRAME.
You will need to add rights to the URL for yourself if UAC is enabled:
netsh http add urlacl url=http://+:9090/ user=domain\username
Code?
I couldn't resist it; I have written a (poorly tested and mostly incomplete) sample.

That is incorrect.
AJAX is a client side technique relying on JavaScript. As long as the web server can respond to an HTTP request, it can server AJAX content (whatever that might mean:-).

I would use this UtilDev Cassini as me embedded web server of choice, it is based on the code from the embedded Visual Studio dev server and can run pretty much anything that runs in IIS. AJAX is a browser technology not a server technology, the server just sees http requests the same as any other. My last point would that it seems slightly odd to embed a web server in a console application. It would be more usual to do this with a windows service. Have you considered converting the console app into a windows service?

Related

how to serve .net console on Nginx?

So I got it to the stage where the .net (framework) console application could run on localhost, but the next issue is exposing it using Nginx.
I looked at many tutorials, but they all seems to be for websites. How can i make it work for a console app?
Thank you.
What do you gain from using nginx in front of it instead of just
opening the console app on port 80 (or preferably 443 witha
certificate)? You will probably need to set up nginx as a reverse
proxy for your app.
I have the console app running on port 1234 because there is already an app using port 80. To be honest, I am not sure why I am using Nginx, I just assume that's what I need, since I have the app running on localhost but I cannot access it from outside. I have set the firewall to accept that port from the domain provider side.
uhm.... what exactly do you mean by "exposing a console application"?
because what you expose with nginx - or any other webserver - is a
website. also: welcome to stackoverflow. i recommend taking the tour,
as well as reading how to ask a good question and what's on topic.
Thank you. I am probably not using the most accurate term. Basically, I just want people to be able to access my app. In my mind, I imaging there is some additional setting I need to do to have it "exposed" to the public (and that's using nginx). I can see the app running on localhost but not sure why it's not public.
please say what you expect a console app 'exposed to the public' would
look like. How would they access it? What tool on my desktop would I
start, ignore the nginx part of the discussion
So I have a loop serving some web content similar to this one.
https://gist.github.com/define-private-public/d05bc52dd0bed1c4699d49e2737e80e7
When I run it, external user should go to a website and see the page content served up by this.
If your console app is a Console.ReadLine() Console.WriteLine() type
thing the only way to expose it simply is to setup and SSH server on
your host system. Then the users need an SSH client to access it. This
is doable via web browser hosted SSH access but thats v complex
solution
You can ignore all the Console Readline and Writeline stuff.
Ok, so the solution is actually quite simple. All you need to do is forward your incoming request to a localhost using proxy_pass, as suggested by #fredrik. The following should be added to the default config file (under nginx):
server {
listen 80;
listen [::]:80;
server_name example.com;
location / {
proxy_pass http://localhost:8888/;
}
}

Listen to a specific application using C# and FiddlerCore

I'am trying to integrate C# Web Browser control and Fiddler Core. I wan't to do this by letting Fiddler Core to listen to the Web Browser control without affecting any internet explorer or other internet browsers.
The diagram below should show what I want to achieve:
Internet Explorer -> Internet
Chrome -> Internet
Other Browsers -> Internet
Web Browser Control (Custom C# App) -> Fiddler Core -> Internet
The four instance below should run parallel without fiddler core interfering with other browsers. So if I wan't to change the response HTML in my Custom App, this will not affect the other browsers.
I've been searching on Google for a while now but I cannot see any concrete example to get me started.
Thank you in advance guys, any help would be greatly appreciated.
FiddlerCore exposes the calling process information in each session's X-ProcessInfo flag.
By default, FiddlerCore will register as the system proxy and most applications will automatically route their traffic through it.
If you want to capture traffic from only a single application, the steps are:
Don't register as the system proxy when calling .Startup.
The target application's proxy settings must be pointed at FiddlerCore's port.
Now #1 is trivial, but #2 is harder, and depends entirely on what networking code the target application uses. You mentioned that the target application is a custom C# application hosting the .NET Web Browser control (which is just IE). If that's the case, and if the application is also hosting FiddlerCore, then you need only call
Fiddler.URLMonInterop.SetProxyInProcess("127.0.0.1:8889", String.Empty);
...inside of your application.

Can I open client's Windows Explorer from WCF?

Good morning,
I have following scenario: I have solution with two projects - first project is MVC WebClient and second one is WCF Service. I want open Windows explorer on specific location, probably through WCF. I know that it is not possible from pure client so I was thinking if WCF can does it.
Full scenario is: User clicks on button and then windows explorer will be opened.
I have tried do it on my local computer
Process.Start(path);
and it works, but what if I will host it on IIS?
Will it works?
Will it open client's explorer or server's explorer?
If first two steps will work, will I need host WCF service on IIS too or MVC client with service reference is enough?
And if this scenario won't work, can anyone help with some solution for this problem (if is any)?
Many thanks
The only way I could think of doing this purely with a browser would be to require the client to use IE and then embed an ActiveX control. The page would have to run with the highest security privileges so the ActiveX could run unimpeded. The nice thing about this is it would not require much (if any) server-side support. It would all be handled by Javascript.
Otherwise, to make it browser agnostic, you would have to have a listener app running on the client machine, probably as a service or set to start on machine boot (good candidate for the notification area on the taskbar). The listener app on the client machine could establish a connection to the server using WCF, Sockets (whatever you want really) and then when the user presses the button on the WEB application a message is sent from the web server to the client-side listening application. This of course could trigger anything you want on the client-side.
I see several complications with this as well:
1) How do you get the listener installed on the client machine?
If it's really simple you could just send the exe over with instructions where to copy.
You could build an installer or use ClickOnce to deploy it right from the website: http://msdn.microsoft.com/en-us/library/ms973805.aspx
2) Your web application now needs to communicate with a server-side component that is connected to the client-side listener. If you are already using WCF on the server this might not be too bad. If the client-side listener connects to the same web application the user is using it probably wouldn't be too difficult either (use long-polling or web sockets, etc)
3) How do you secure the client-side listener and connect the client-side "session" to the web "session" such that the user clicking on the button in his web browser sends a message to his instance of the client-side listener and not another user's listener.
These are all doable. I wrote an app that worked sort of like this several years ago. There were definitely challenges but we got it working and it has been stable for several years now. We used WCF.

How to develop Smart Client Application

Hi.
I have several questions. I developed an application using Visual Studio 2010. The application is developed using Windows Form and the program was wrote using C#.
This application needs to be run on a server. There are several clients connected to the server that will access and use that application through the browser. I know this can be done using Smart Client technique. But I don't really understand how to do it. My questions are:
Is there any syntax/class/methods/function that I need to include in my application? How to use it?
What settings/configuration do I need to set up so that the client computer can access the application in the server through their browser?
I know this can be done using ClickOnce but I don't know how. Can anybody tell me or show me the steps that I need to do to implement this?
Is there any syntax/class/methods/function that I need to include in my application? How to use it?
There are a few - but this is a large area. I would point you at couple:
WCF
ASP.NET Web API.
I would advise to use the later as it appears to be where the modern development is heading (at least to me).
Put simply, you will write your own web server, host it either in your WinForms application or migrate your WinForms to be a web application and host it on IIS, for example. Your web server will expose some API, which will likely to be based on HTTP protocol. A client application will hit web URLs. This will be a request-response paradigm.
Because this is a large area, I cannot name you exact classes, but have a look at ASP.NET site for samples.
What settings/configuration do I need to set up so that the client computer can access the application in the server through their browser?
It depends on the technology. Usually it's pretty simple - get it from tutorials and samples. In most cases this will be *.config file XML code and some minor bootstrapping in .cs files.
I know this can be done using ClickOnce but I don't know how. Can anybody tell me or show me the steps that I need to do to implement this?
ClickOnce is a deployment tool. You probably don't need that at this point.

Can Console Output in a Web Service Operation be Visible on the Client Console?

I have a console application that's calling a web service method.
In the web service method, I am using System.Console.WriteLine, but its getting ignored.
How can I give feedback to the console application from within my web service method?
You can't because System.Console.WriteLine will write to the server console, whereas you want output on the client console.
You can use a logging framework like the one in the SixPack library, or Log4Net to write logging messages to a file for example.
In order to be able to write on the client console, you need to put your Console.WriteLine statements in the client application.
An alternative is to have the web service 'tell' the console app what it's doing; to keep a running log.
Where your Web Service is hosted?
I suppose Web Service is on IIS, so it is a separate process. It can be installed on the same machine or on the different machine, but in any way it will be different process and it cannot write to the console of the client application. It can write to log files, for example, using system.diagnostics.trace or Log4Net open source logging tool
I suggest you to start with this article: ASP.NET Quickstart Tutorials: Monitoring Your Application to understand what choices you have to log and monitor your ASPX web services
This is a slightly unusual thing to try and do. If you want to log the activity of the server then I'd use the Trace.WriteLine() to write to a log file.
However if you want to view server log information on a client then you could get the server to log to a string variable on the web server and then have a GetLog() web method to retrieve the log information to the client. You can then write it out in the console window. This would require you to call the GetLog() web method quite frequently but you'll get the result you want. There will probably be a whole host of issues with this, such as requesting a large volume of log information in a single call.
You could also consider using the EventLog support in System.Diagnostics to create a log of what the web service has done.
When we are testing our web services on our development server we often use the Visual Studio remote debugger to hook up to the webservice code running remotely, and use Debug.WriteLine calls to keep track of what is happening. Not something that is recommended for a live server, but for development and testing where you often need more detail it is a worthwhile technique.
You could use Asyn Callback to invoke webservice. This way the client could initiate a request for feedback. I do not believe we can achieve the other way round.
You will find more information on Asyn callback on How to: Implement an Asynchronous Web Service Client Using the Callback Technique
The best option is probably to have the Web Method return a string with whatever values you wanted to print to the console. The Console.WriteLine then needs to happen from the Console Applications side. Remember that you can return Objects/Classes/Xml from your Web Method as well, so you are not limited to a single string.
The other option will only really work if you have access to the Web Server running the Web Service. But then you would approach it by letting the Web Service write logs to a text file on the server. The appropriate IUSR_CompName needs write access to the folder on the computer then.

Categories