Silverlight - Run Method at Application start on server side - c#

I have a Silverlight-Application which is hosted in a ASP.NET-Site. Now, I need to execute something at the first start of the application (run code for update database). I am searching the right place to do this.
Can anybody help me where I have to put this code? - Thanks.

You can add to the Startup event in your Application class, e.g.
public partial class App : Application
{
private void Application_Startup(object sender, StartupEventArgs e)
{
... startup code here
}
}
See MSDN. Note that this runs on the client side - not the server side. Code in your silverlight application does not run on the server.
If your code has to run on the server, host your silverlight control in an aspx page and override the page's Page_Load event to execute code BEFORFE the silverlight client is sent to the browser.

Related

On asp.net application end page view count data in not updating

I have asp.net application. I am forcibly end this application and on the time of that i want to update my view page count value in database. If anyone known plz help me.
I also written code in global.asax file
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shut`enter code here`down
}
but this is not working when i forcibly end application.
I don't think that this is a very idea to try to save the data upon exiting if it's important to you.
If you still want to try and you use .NET Framework 4.5.1, you can listen to the HostingEnvironment.StopListening event, which fires when an application shut down is pending:
void Application_Start(object sender, EventArgs e)
{
HostingEnvironment.StopListening += (o, args) => SaveTheDataHere();
}
This will save the data if the hosting environment is shut down in an orderly manner, not if it abruptly crashes.
Needless to say, this won't fire if there is a fire in the server room, a BSOD halts the OS, someone trips over a power cable, etc.
A better approach would be to offload the visit event (count) immediately or periodically (depending on what level of potential data loss i acceptable) to some error resilient external process, such as a message queue.
I got my answer,
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shut`enter code here`down
}
this event works only when the following things happens then fire.
Update web.config.
Update dll file in BIN.
Stop OR restart IIS.
Application pool recycle.
So i implemented a web service for this to update my records in database periodically.

Run a Selenium test from a aspx

I want to start a Selenium test from within my aspx page.
I've installed Selenium with NuGet in my solution.
In the code behind for my webpage I have this:
protected void Page_Load(object sender, EventArgs e)
{
_driver = new FireFoxDriver();
_driver.Manage().Window.Maximize();
_driver.SwitchTo().Window(_driver.CurrentWindowHandle);
_driver.Manage().Cookies.DeleteAllCookies();
_driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(3));
_driver.Navigate().GoToUrl("http://www.google.com/");
_driver.FindElement(By.Id("lst-ib")).SendKeys("ModelTrains");
_driver.FindElement(By.Id("lst-ib")).SendKeys(Keys.Enter);
_driver.Quit();
}
The code runs, I can step through it, but I don't see the new window or browser instance, I also don't get any error.
What I'm I doing wrong?
What you are currently doing is starting up a FireFoxDriver on a web server.
So on the actual server where you are hosting the web site (Server running IIS) you will see selenium in action.
Why don't you use console or winform?
if you run on the console app or winform, you can see that.
but if you run on the webform, you can't see it,
because it is running on the IIS express. like a mmc snapshot.
(if you run app that made by console app(.exe), and Jenkins run that app(not you) you can't see browser too.

C# windows service subscribed to webserver

I'm working on an intranet website.
All users should get desktop popups from the webserver whenever something new is posted on the website.
I was looking to make my own windows service that would subscribe to the server ( Making use of something like SignalR ) and then this service would show a simple popup notifying the user whenever the server sends out a message.
But instead of building this myself i was wondering if something like this isn't already out there. I've been looking around a bit but couldn't find anything.
I'm mainly a web developer and have never built a windows service or C# desktop application so i would prefer using some existing code.
Does anyone know of such a thing ?
For building a Windows Service try Top Shelf: http://docs.topshelf-project.com/en/latest/
In general it is easy as one, two, three...
public class TownCrier
{
readonly Timer _timer;
public TownCrier()
{
_timer = new Timer(1000) {AutoReset = true};
_timer.Elapsed += (sender, eventArgs) => Console.WriteLine("It is {0} and all is well", DateTime.Now);
}
public void Start() { _timer.Start(); }
public void Stop() { _timer.Stop(); }
}
public class Program
{
public static void Main()
{
HostFactory.Run(x =>
{
x.Service<TownCrier>(s =>
{
s.ConstructUsing(name=> new TownCrier());
s.WhenStarted(tc => tc.Start());
s.WhenStopped(tc => tc.Stop());
});
x.RunAsLocalSystem();
x.SetDescription("Sample Topshelf Host");
x.SetDisplayName("Stuff");
x.SetServiceName("Stuff");
});
}
}
I'm working on an intranet website. All users should get desktop
popups from the webserver whenever something new is posted on the
website.
using timer is not a good technique over here as updates are not guaranteed in particular interval or session .but you can take that as an option based on the need.
I was looking to make my own windows service that would subscribe to
the server ( Making use of something like SignalR ) and then this
service would show a simple popup notifying the user whenever the
server sends out a message.
Yes exactly like a chat application that would frequently have messages and users get a pop up.ASP.NET SignalR is a library for ASP.NET developers that simplifies the process of adding real-time web functionality to applications. Real-time web functionality is the ability to have server code push content to connected clients instantly as it becomes available, rather than having the server wait for a client to request new data.
But instead of building this myself i was wondering if something like
this isn't already out there. I've been looking around a bit but
couldn't find anything.
References for SignalR Link1,Link2,Link3
I'm mainly a web developer and have never built a windows service or
C# desktop application so i would prefer using some existing code.
Making C# desktop or windows service is not a big deal as you already are a programmer.Some existing codes for updations pop up is here.
for the signalr Server side, I would suggest you use a C# winform.
for the client side, you can use JavaScript inside any html file to 'receive' the message from the signalr Server, then you can popup an alert message or whatever you want, however, in this case you have to make sure the users are browsing that html file in a browser, otherwise the message won't be received.
there's no ready code since signalr support different types of servers as well as different types of clients, I believe you need to write your own code. Actually Signalr is quite easy to use, write your own code may be faster than using the others.
This question: SignalR Chat App in WinForm With Remote Clients looks like it might point you inthe right direction. Specifically this article:
https://damienbod.wordpress.com/2013/11/01/signalr-messaging-with-console-server-and-client-web-client-wpf-client/
you could probably use DesktopToast: https://github.com/emoacht/DesktopToast
or Growl: http://www.growlforwindows.com/

How to call an executable start a service from a MVC web app

I have a MVC4 app in which I need to init a long running process. Currently, the code for this process is in a console app being installed as a service with topshelf. I have the process checking a database every few seconds to see if it needs to be run, but that's not a solution. I need a way for the MVC4 app to kick off the process and forget about it, but the process NOT be unloaded with the web app when the response is returned to the client.
Can someone point me in the right direction?
If I'm understanding your question, what you can do is, in the service (the class that's derived from ServiceBase), override OnCustomCommand:
private const int MY_CUSTOM_COMMAND = 140;
protected override void OnCustomCommand(int command)
{
if (command == MY_CUSTOM_COMMAND)
{
... Do stuff here ...
}
}
You can then trigger the command in your service, from some external application along these lines:
private const int MY_CUSTOM_COMMAND = 140;
using (ServiceController sc = new ServiceController("MyTaskService", "ServiceMachine"))
{
sc.ExecuteCommand(MY_CUSTOM_COMMAND);
}
That's the basic idea. Custom commands can be any value from 128-256 inclusive.
We use a similar system in our web app, which allows users to submit "jobs" that are then run by a windows service. The web app sends a command to the windows service to let it know a new job has been submitted. The service then goes to the DB to get the information about the job to execute.

Application launched from ASP.NET using Process.Start fails if user credentials are specified

I'm unable to use Process.Start to start a simple console application when I specify user credentials. It appears that the call to Process.Start tries to start the application, but the application immediately fails with an event log entry (see below). I'm running IIS 7.5/.NET 4.0 on a Windows 7 Ultimate (x64) machine. Application pool runs under ApplicationPoolIdentity and uses Integrated Mode. Web site is set to run under Full Trust.
To test, I created a simple console application, ConsoleApplication1, that simply writes "Hello World" to the console and appends the current date and time to a text file, just so I can see that the application executed. When I run the application from a button click in my ASP.NET app using the following code, I see a new value get written into my text file, as expected.
protected void btnStart_Click(object sender, EventArgs e)
{
Process.Start(#"C:\dump\test\ConsoleApplication1.exe");
}
When I try to specify a set of user credentials to run the application using the code below, nothing is written to my text file, and I see a message in the Event Viewer that states:
Application popup: ConsoleApplication1.exe - Application Error : The application was unable to start correctly (0xc0000142). Click OK to close the application.
Here is the code that causes the error:
protected void btnStart_Click(object sender, EventArgs e)
{
Process.Start(#"C:\dump\test\ConsoleApplication1.exe",
"myusername", CreatePassword("mypassword"), string.Empty);
}
private static SecureString CreatePassword(IEnumerable<char> password)
{
var ss = new SecureString();
foreach (var c in password)
ss.AppendChar(c);
return ss;
}
I'm pretty sure the authentication of the user credentials is working properly, because I get a Logon failure: unknown user name or bad password error if I enter incorrect info.
I feel like I must be missing something simple, but can anyone point me in the right direction here?

Categories