Launch windows application from asp.net
If you want to launch the WinForms app from a web page, the best approach is probably to use ClickOnce technology. It allows you to publish your application directly through a web page no separate installer needed. ClickOnce is a deployment technology that enables you to create self-updating Windows-based applications that can be installed and run with minimal user interaction.
Visual Studio provides full support for publishing and updating applications deployed with ClickOnce technology if you have developed your projects with Visual Basic and Visual C. There's an excellent answer on Stack Overflow that reviews some things to be aware of.
Suggest you read through that as well. If you want to launch your application possibly including parameters from a web page, one approach is to have the application register a protocol handler. A protocol handler allows an application to react to a URL with a new protocol that you define, e. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. So, if you agree this is reasonable, please consider accepting this answer formally green button -- thanks.
You can do it by using the Windows Protocol Handler mechanism. Check out the below StackOverflow link: windows - How to launch an application from a browser? Posted Aug pm Member Dave Kreskowiak Aug am.
No, you can't. You cannot launch any arbitrary executable you want from a web page served up from a remote server. Add your solution here. OK Paste as. Treat my content as plain text, not as HTML. Existing Members Sign in to your account. This email is in use. Do you need your password?
Submit your solution! When answering a question please: Read the question carefully. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome. NET empty web application. Then, in the Solution Explorer, right-click on the project name, click Add. You will find a list like web form, web API controller class and so on. Scroll down, click on the Web Service. Name it and click add.
A new window will open for writing the code for the web service. Start coding here. Step 2. I have made three functions here, one for registration, one for login and another for some sort of general purpose.
It's mandatory before creating a new function. I have three functions and I have written it three times before every function. If you don't understand, download the source code that I have provided. Compile it, then if there is no syntax error, this will open. Step 3. The web service has been made. Now we will test it with a C application.
I have made a simple project just for testing it. NET project has a reference to the business logic, flow, and data access projects. Next, to create the RunJob Web service method in the JobRun Web service, the Web service method will need to call the flow layer's function that runs the proper jobs.
This means that the RunJob method can start out as simply as this:. The RunAllActiveJobs of the JobFlow function does all the real work in coordinating the running of the jobs, while the RunJob function merely serves as an entry point into the sequence. Note that this code does not prevent jobs from running on more than one thread at a time, which could happen if the Windows service scheduled tasks too frequently faster than they could be run or if some other application invoked the entry point.
If the method is not thread safe and allows multiple threads through it at the same time, it may cause problems with the results of these jobs.
For example, if job X sent an e-mail to Mary Smith, but hadn't yet updated the database when job Y queried the database to do its e-mails, then Mary could receive two e-mails. To synchronize access to the function, I'll use the Mutex class from the System. Threading namespace:. Mutex provides for cross-process synchronization, so this will prevent multiple runs at the same time even if two different ASP. NET worker processes are involved. Now, let's change the RunJob method to use the Mutex to ensure that no other job is running before starting the jobs.
As you can see in the RunJob function in Figure 4 , you call the WaitOne function of the Mutex to make this thread wait until it is the only one before executing. The ReleaseMutex function is then called to indicate that you are finished with the code that needs to run only in one thread.
Of course, blocking here may not be the correct solution. You might choose to return immediately if another thread is already executing jobs, in which case you could specify a short timeout to the WaitOne method, and immediately return from RunJob if the mutex couldn't be acquired. Put all of the main actions of the function in a try-finally block so that ReleaseMutex is called even if an unexpected exception in the RunAllActiveJobs function causes the RunJob function to exit.
You'll want to secure your Web service using some form of authentication and authorization, possibly using Windows security, to ensure that no one runs the service without proper authorization, but I won't go into the details of that in this article. Now that you have the Web service built so that you can call it from another app, let's build the Windows service that will use it.
Make sure that this service will start properly by adding a Main method as follows:. You should change the created serviceInstaller1's StartType property to Automatic so that the Windows service starts when Windows boots. NET automatically augment the app. The proxy class generated will look to this configuration file for the Web service's URL, thus allowing you to point the Windows service at a different endpoint without recompiling.
Fourth, create a method in the Windows service to run the Web service every time it is called. The method will look like this:.
As you can see, you'll declare the Web service proxy and create it just like any other. NET object. Then, call the Web service's RunJob method in order to run the jobs on the remote Web server. Note that neither step is different from using a local class even though you are using a Web service.
Fifth, you'll need to call the RunCommands function in the Windows service. You should call this method at a set interval of time based on how often you would like to run the jobs on the remote server. Use a System.
Timer object to ensure that the RunCommands function runs at the proper intervals. The Timer's Elapsed event will allow you to trigger any function that you specify after each interval has elapsed.
Note that interval length is specified in the Interval property. You'll use the triggered function to call the RunCommands function so you can automate this feature. By default, this timer class only triggers an event the first time that the timer expires, so you need to ensure that it repeatedly resets itself every time by setting its AutoReset property to true.
You should declare it at the service level, so that any function of the service can reference it:. To allow for the configuration interval to be changed without recompiling the application, I've stored the interval in the app. AppSettings instead of having it hardcoded, as shown in the following:. Finally, you have to install the Windows service using the installutil command. The easiest way is to open the Visual Studio.
NET command prompt, navigate to the service's directory, and run the installutil utility, specifying your assembly as the parameter. It is important to expand the flow layer to handle the needs of running scheduled jobs assuming the jobs differ enough that they need to be coded rather than merely parameterized.
This involves collecting all jobs from the database where the next start time in the database has passed and running them individually. Within the flow layer, you will create a base class called Job to provide all of the functionality that is common between jobs. This includes a mechanism to initialize and retrieve the JobID, a common method RunSingleJob to run the job and set the next time to run in the database after a successful run, and an overridable method PerformRunJob to be customized for each individual job.
The flow layer will also need to have job-specific classes built for each job it performs. These will inherit from the base Job class and will override the PerformRunJob function of the Job class to customize the execution of that particular job.
0コメント