How to communicate between ASPX and WinForms - asp.net

How can I send commands to a WinForms application from an ASPX web page? We have already explored executing the WinForms application with different command line parameters but wanted something more smart.
Thanks.

Create a web server within your application that listens for HTTP GET and/or POST commands and acts appropriately. Then use AJAX to send request, i.e., http://localhost/myapp/?command=print&file=teletubies.jpg
Your web server, which is just a program that listens on port 80 and sends responses according to the very simple HTTP protocol, within your application then parses the requested URL and decides that it should print the file teletubies.jpg based on the query string in the URL.

Web pages (whether running asp.net or a competing platform) are always reactive. They receive commands (requests) and respond. They are not proactive, and don't send commands. This is how the core technology on which the internet is built works.
This means is you want an asp.net page to send a message to a client app, the only way to do it is for the client app to frequently poll the page, possibly using System.Net.WebClient.

I don't know of any way this can be done due to security. I know I wouldn't want people to be able to access the running applications on MY machine from their web app.

Related

SignalR: Reply to Web Forms client on same machine as web application originating request

I'm looking for a way to support the following process:
Button is clicked in web application running on machine named PC1234.
Call is made to server (either the web server or an API on another server, it doesn't matter) to Do Something.
The server sends a notification to a Windows Forms client installed on PC1234 that the action is complete.
I've got the easy part working using SignalR. I can call a method on the web server and then send a notification with SignalR to ALL clients that the method has completed. The problem is notifying ONLY the client on the originating machine.
My initial plan was to include some unique identifying attribute of the machine with the call to the server which could then be used to direct the SignalR notification back to just that machine, but that doesn't seem to be possible.
An alternative idea was to have the call to the server include a unique reference and also update a file locally (i.e. a Cookie) with that reference, then have the client app poll the Cookie for new references and filter all SignalR messages received for that unique reference. This would be a bit clunky even if it worked, which it doesn't really, not least because I want this to work cross-browser, and different browsers store cookies in different places.
Ultimately this is to support printing locally and silently from a web application. The user selects a document in the web application, hits a print button, the request is sent to the server which retrieves the document from the database, saves it to a network share and sends a notification to a client app on the machine from which the print request was generated. The client app then prints the document from the network share and deletes it.
I never found a way to do exactly what I described in my question, but I came up with an alternative which worked well enough.
In both my web application and my Windows Forms client, the user was logged in with the same Windows credentials. I was therefore able to have the server respond to the button click in the web application by broadcasting a SignalR message to all SignalR clients where the same user was logged in, using
Clients.User(userId).send(message)
See this article for more detailed examples and instructions.
In my Windows Forms client, I included code to track how many instances of the client were connected to the SignalR Hub with the same user credentials and code to handle the receipt of a SignalR message from the server when multiple client instances were connected with the same user details (in my case, this meant displaying a message saying something like "You've requested a print from the web application but you're logged in at multiple workstations. Do you want the document to print here?").

Sending Data Between Windows Service and Web Application

just a quick question hopefully someone can point me in the right direction. In our .net web application we are trying to implement a third party device that pairs with the user computer. In order to generate this pair in our project we need to be able to either pull the current users computer name, or ip address. In a winforms application this would be easier since it resides on the users network. Since this is a web application, it has been quite hard as the only thing being returned is the ip address of the server or the name of the server. So what we are thinking of doing now is having either a Windows Service, or WinForms application, that our customers can download that will return this information to our application.
Creating the Windows Service or WInForms application is straight forward and I understand that, my question is how would I get my Windows Service or WinForms application to send data to the web application? I have looked into WCF but it seems the end user would have to set up some tcp settings on their end which could be difficult for some and I would like to avoid. If anyone has any suggestions other than WCF I'm open to that as well.
Any suggestions for how this situation should be handled appreciated, thanks.
i guess you are misunderstanding the concept of Web service, or WCF or whatever technology you use.
A Web Service is that, a "function" that will be called from a client but executing on the server, so when you do a WCF and you use it from a different web site, or a desktop app, you are in fact sending the data to that server.
In other case if you are trying to achieve a "real time" monitor, sending data to the server and showing it on your website, then you need to do separate things.
This is what you need to do.
First set up a WCF service that will handle the information on your SERVER side, in there you need to implement a function which will be receiving a IP and computer name (or whatever you need) and returning the hash, key or the info you are using to pair the device, then host it on a IIS server.
`public string PairDevice (string name, string IP, string ....){
//save to database, do something and return the hash.
}`
Second create a windows service, a desktop application or something that will be running on the CLIENT computer, in there you need to CONSUME the WCF, in your main code, you will retrieve all the parameters you need for your WCF function, then adding a "Service Reference", you will be available to use the REMOTE function on your CLIENT, be aware that your SERVICE needs to be reachable by the application.
//retrieve IP, name, etc
string name;
string ip;
myWCFclass WCF= new myWCFclass();
string myHash=WCF.PairDevice (name,ip,etc...);
//thats all.
And that's all you gotta do, if you need more help about how WCF works, i suggest you to read this link
http://www.codeproject.com/Articles/406096/A-beginners-tutorial-for-understanding-Windows

Run background task with console application from asp.net

My requirement is to send emails within the web application from an external server that takes around 4 to 5 seconds on average to process and send the confirmation email. I do not want the user to wait for this, so i trigger a console application with Process().StartInfo.Start() and it does it in background. The question is how much i can rely on this as during normal days the application sends around 10 thousand emails daily and in high traffic days it may surpass 80 thousand. What possible issues/problems the application or server may run into? Is there any better solution for this?
You can use following method to achieve this:
Create a .aspx page and write you business code here (e.g. email
send)
Where required, call this page using Javascript along with parameters in querystring. You can create an image element and set its source to that page.
var img = new Image();
img.scr = "perform-operation.aspx?[parametervalues]";
Thank you
Console application has drawback, if you plan to invoke console application for each email separately, any new process takes long to load and clean up, this is too much overhead on cpu. Instead, the best alternative is to host another website in IIS, this new website will have its own Application Pool, which will create and host process. IIS will shutdown the process if no request is served. You can setup Web Farm by running more worker process per application.
Other alternative is to run console or windows service all the time, let it be in the memory but has a web service host listening for email requests, if a request is received from your web application, this background service will send email on new thread and go back on waiting state. Basically it is a self hosted WCF service. This is quite same as hosting another website on IIS.
I prefer IIS based hosting as it is easy to setup, does not require any extra permissions and in future, to scale horizontally, you can easily move this mail service to other server.

Are webservices exposed to any one?

I'm very new to web services (please note, not WCF but the old fashioned .asmx files).
Now I may be liking this too much to ports, but if I expose a port on my web facing server then it is exposed to attacks as well as my own use; There are tools which can scan to see what ports are open.
Is this true of a web service? Now, don't get me wrong, I know each service should be coded well enough that nothing malicious can happen or that the calling class doesn't know the 'contract' to implement them, but that's not the question (and I guess port flooding could still occur?); If I put up a few web services on a server, is there a tool/program which can detect them (by name)?
Yes, a web service is basically a web page that takes arguments and response with a formatted result that can be read more easily by a program (technically both are a result of a http request and response - there are other mechanisms as well, but the typical one is over the http protocol).
If you type the link to your web service in a browser you will see you are presented with an interface that allows you to "execute" its services.
Therefor you need the same security as with a web page, meaning login or check of credentials, tokens, signing, encryption and so forth (preferably on a ssl-connection).

Integrating Instant Messaging into an ASP.NET application

I was thinking about integrating some instant messaging function into an existing ASP.NET web application, e.g:
the web application can display the online-status of users (are they currently logged in with their IM client)
users can send messages from the web application to the IM client of other users
users can initiate a IM chat from the web application (without having to know the other user's IM identification beforehand)
Does anyone know about some existing libraries, sample applications or other resources that might help implementing such a feature?
Thanks a lot for sharing your knowledge.
You should try Jabber. Demo client avaiable here.
There is an architectural overview, the main concept looks like this:
(source: webta.net)
And some citation from the site:
1. Goal
Create an multi-service instant messaging AJAX-based web application with internal accounting.
2. Main problem
We need to connect to IM servers from HTTP client (browser).
HTTP is a stateless protocol. This means that, theoretically, each HTTP request is being proccessed by separate http daemon proccess.
Once request proccessed (data sent to client), server fogets about client.
All IM services protocols are stateful.
When client connects to IM server, socket connection being created and connection much remain open for succesfull communication.
There's a list on the ASP.net site.
http://www.asp.net/Community/Control-gallery/browse.aspx?category=54
You might want to look at the .net implementation of jabber:
http://code.google.com/p/jabber-net/

Resources