Appservice for UWP Onrequestrecieved - c#

I am wondering when i create an appservice and create the on requestrecieved event handler is this even handler used if i use a timer trigger as well or is it only used when connected to by an appserviceconnection externally
What i am trying to do is create one app service that is run off a trigger(to update live tiles) and also allow an application to send it information to create targeted live tiles by using an appservice connection.
If this is answered somewhere i have been unable to find it

You will have to declare both AppService and BackgroundTask extensions separately in your appxmanifest. If you declare them both to be in-proc they will run in the same process and your scenario can then be seamlessly accomplished.
You should think of it as two different triggers though: one triggers periodically on a timer, and one triggers from an app connecting to the service. Both can trigger the same code/action of course.
Also note that the AppService doesn't need to be running in order for clients to connect to it. The operating system will start it up as needed (and shut it down when no longer needed).

Related

Polling Service in ASP.NET Webservice with Backgroundworker is not continueing

I need to run a polling service in a ASP.NET-WebService. Now, I have implemented it with a BackgroundWorker which I am starting in Application_Start of the HttpApplication. This is working. But suddenly, it will not continue working anymore.
Now, I have the fallowing Question:
What is the Lifetime for a HttpApplication, after it has started and executed Application_Start?
Have I to implement and run my BackgroundWorker at another place?
BackgroundWorker is not designed for your use case. As quoted from MSDN:
The BackgroundWorker class allows you to run an operation on a
separate, dedicated thread.
Normally asp.net app pool is configured (default IIS setting) to shut down after 20 minutes of idle time. Which is what I think is happening in your case.
You should develop a Windows Service. Following is brief about the same:
Windows services enable you to create long-running executable
applications that run in their own Windows sessions. These services
can be automatically started when the computer boots, can be paused
and restarted, and do not show any user interface. These features make
services ideal for use on a server or whenever you need long-running
functionality that does not interfere with other users who are working
on the same computer.
You can reuse all of your existing code. Just call you web service periodically from this windows service as per your requirement. That's it.

Choosing a service to associate a timer job with

I'm writing a custom timer job in C# and need to pick a service to associate my timer job with.
I've tried the local timer service:
: base(String.Format(JOB_NAME, Guid.NewGuid()), SPFarm.Local.TimerService, server, SPJobLockType.None)
This has been pretty spotty, we've had issues getting this job to run reliably enough on all servers in a farm to be satisfied with.
We think this is due to the timer service not updating with the new job sometimes.
Clearing the SharePoint config cache and forcing the timer service to update has "fixed" the issue, but we obviously can't use that as a final solution to the issue.
What other services could I possibly associate this job with? I don't want to associate with a web app instead.

Is there a way to have code run on a time schedule in an ASP.NET web app?

I have an ASP.NET web app providing access to a database on the server. What I need is a way to run code in the background on a given schedule that auto-updates the server database from another source.
I know how to do this in a windows app by adding a timer, linking up a function to the timer tick event and starting the timer. I do not know how to do this in a web app.
Is there a start-up event for a web app or somewhere where I can start this background process regardless of whatever any users are doing on the site?
You should not do this in an ASP.NET website - this is a major no-no. You are correct in thinking to use a timer on a background .exe. You should look into creating either a Windows Task (a console .exe executed by the server task timer), or a Windows Service. I would suggest the Windows Service as that is standard practice.
If you have access to the computer hosting your site I would write a little app that was run from the Task Scheduler.
The web server is not meant to handle long-running background tasks. It's the wrong tool.
If you dont have access to the hosting computer then I would suggest building some kind of interface whereby another computer rebuilt the database and uploaded it. I'm using the terms "interface" and "upload" in the loosest, broadest sense - apply your own definition.
I was searching for a solution myself couple of months ago, and even though I haven't found enough time to try it so far, I guess I can share the link with you. Maybe you'll find it helpful.
If yes, please, let me know here.
http://quartznet.sourceforge.net/
How to use Quartz.net with ASP.NET
you can use Windows Service or use Timer Control (In the Ajax Category)
Or
As other answers have stated, doing this full function - updating a database and scheduling it as an ASP.NET app is using the wrong tool for the job.
ASP.NET can be used to update a database - that's perfectly valid. Where it breaks down is in the timer. ASP.NET apps aren't meant to be long-running, which is necessary for the timer to work.
If you can do it, I'd strongly suggest using the approach others have suggested - a Windows Service or a Scheduled Task.
However, if you have no access to the actual server, other than to post ASP.NET code - you can't install a service and you can't set up a Windows app to run on a scheduled basis, here's an out-of-the box idea.
Set up a web service or ASPX page that does the update, and then call that page from a scheduled task on a machine you DO control.
So if this was at http://www.someserver.net/updatedb.aspx, there's no reason you can't set a scheduled task on your own PC to call that URL.
I'd consider this a last-ditch solution to be used only if you can't do one of the other options.
The global.asax.cs file has a method that is fired when your application starts: Application_Start. You can hook up your timer method in that event. Just beware, depending on how IIS configured, your app pool may shutdown. For example, if no one hits the site in 20 minutes for example. Just make sure if you HAVE to have this run every X minutes that you have IIS configured to ALWAYS be running and start your app. This is harder than it sounds. In the end, you may want to go with a regular windows scheduled task.

Spawning and executing a Worker process in Azure

The google has really failed me on this one. I am new to Azure and am only intermediate at .NET
I have an Azure solution going and I've written some code in a Web Role which runs great. What I would like to do now is move some of this code into an Azure Worker, which will be initialized by a controller function in the Web Role
What on earth do I need to do to get this going locally? I have created the Worker project within the SLN. I just need to know how to fire it up and run it.
I think part of my problem is I am assuming these workers behave like Heroku workers... is this the case? Because what I need is something like a queue system (a bunch of "worker tasks" in one big queue).
A lot of the links I've found for tutorials seem to tap dance around how to actually initialize the process from a Web Role.
Workers in Windows Azure are not tasks; they're entire VMs. To make your life easier, memorize this little detail: Web Role instances are Windows Server 2008 with IIS running, and Worker Roles are the same thing but with IIS disabled.
When you added that worker role to your project, you actually now have a new set of virtual machines running (at least one, depending on the instance count you set). These VMs have their own OnStart() and Run() methods you can put code into, for bootstrapping purposes.
If you grab the Windows Azure training kit, you'll see a few labs that show how to communicate between your various role instances (a common pattern being the use of Windows Azure queues). There's a good example of background processes with the Guestbook hands-on lab (the very first lab).
More info on this, as I've gotten it going now..
If you're coming from a Heroku background, then an Azure Worker is more or less the function in Rails that you'd actually execute with the queue. Unlike Heroku queued operations, an Azure Worker just runs endlessly and keeps polling for new stuff to do... hence the templated sleep(10000) in the Run() function.
The most conventional way I've found to make a Web and Worker talk to each other is by queue messages via Azure ServiceBus which is currently NOT emulated, meaning you need a functioning Azure account to make this work, and it will work even if you are running locally. You just need internet access.
A ServiceBus message can pass an entire object over to the Worker (so long as the Worker proj has the right dependencies in it), so that's kind of nice.
I think you're just having trouble starting the azure emulator along with your worker/web roles? Just set the azure configuration project as the start up project and run that. It'll boot up the emulator along with all your roles.

Can I delegate windows service execution to another process?

I would like to be able to have a Windows service start, launch another executable, and then exit, leaving the launched executable to run in place of itself as the service.
The processes will be able to communicate with each other, so any information needed in either process from the other process is available.
Is this possible to do in .NET?
EDIT: To clarify, here's what the workflow would look like:
Service starts, launching a.exe. Note: a.exe does not call ServiceBase.Run()
a.exe launches b.exe using Process.Start()
a.exe and b.exe accomplish any communication required to exchange information, such as process or service handles (Note: this functionality already exists)
a.exe assigns service control to b.exe
a.exe exits, leaving b.exe running and bound to the service control
No you can't change the the process that is bound to the service control manager. To see why this is impossible you need to see how the unmanaged Windows service API works. (which .NET is wrapping).
You connect to the service control manager by calling StartServiceCtrlDispatcher (which can only be called once). In the lpServiceTable parameter you pass a pointer to a ServiceMain function, which obviously is only valid for the current process and all of the service control notifications are sent to this function.
The call to StartServiceCtrlDispatcher also does not return until all the services have stopped. So the service control managed is also tied to a thread in process that started the service, so that can't exit without the link to the service manager being severed.
So in your example, step 4 cannot occur, and process a.exe needs to stay alive for the duration.
See Process.Start(), this tutorial (old, but should still work), or this one.
If you're trying to start a service that's already registered, this may help.
Once the new process is running, communication with it has the same requirements as any other interprocess communications secnario.
You could use WCF, open a socket, use a database, etc. Without more details about what sort of communication you need, it's difficult to recommend a particular approach.

Categories