windows service application interacting with SQL server database - sql-server

I have a windows service application that is meant to interact with SQL server database (INSERT, UPDATE, ETC). The windows service application is also multi-threaded.
I created an "App_Data" folder to keep my database and used app.config file for connection information, etc.
After installing and starting the service, nothing happens, the database doesnt get updated, etc.
Has anyone ever written a windows service application that interacts with a database? Kindly advice me on how to overcome this problem..
Thanks

From you've described you don't necessarily have a database problem. What you need is a way to debug your windows service. Particularly the OnStart.
Here's what I often put in the OnStart in a Windows Service written in C#
protected override void OnStart(string[] args)
{
foreach (string arg in args)
{
if (arg == "DEBUG_SERVICE")
DebugMode();
}
#if DEBUG
DebugMode();
#endif
timer.Interval = 1;
timer.Start();
}
private static void DebugMode()
{
Debugger.Break();
}
Now when you want to Debug the OnStart you can add the "DEBUG_SERVICE" command argument from the Service Control panel. Otherwise you'll have to try and attach the debugger manually which might not be in time.
Also note how the I start a timer. This allows a separate thread to do the actual work. This is important because you want the OnStart to finish in a timely fashion. A timer isn't required because some windows services respond to an event like a file watcher but more often then not, it seems polling at intervals is what people do in Windows Services.

As far as I know, the App_Data folder and therefore connection strings pointing to it are only available in ASP.NET web apps and web sites - not in other types of Windows apps.
My recommendation: put your SQL Server database on a database server - can be your local machine and a SQL Server Express database - and connect to that server instance!

Related

Can I use SignalR with SQL Server Backplace on an existing entity code first database?

According to Scaleout with SQL Server you can use SignalR.SqlServer to keep SignalR synced on a load balancing setup. I have an existing MVC 5 website with its own database created using Entity Code First. The article seems to use a dedicated database with service broker enabled and says not to modify the database.
So do I need to have a separate database for this? I know Entity can be picky if the database schema doesn't match and I worry that if I try to use the SignalR Sql Server package with the existing database that the tables it creates will cause a context changed error.
Also can someone provide me with more information about using the Microsoft.AspNet.SignalR.SqlServer package. The article I linked doesn't give a ton of detail and I don't know if I need to change anything in my hub and groups or if it is all handled automatically.
You should be able to, though you'd likely want to separate your entity framework definitions from signalR. You can either put SignalR in a separate database, or give the two a separate schema.
In terms of configuration, you'll need to make an addition to the Startup class of your web project:
public class Startup
{
public void Configuration(IAppBuilder app)
{
var sqlConnectionString = "connection string here";
GlobalHost.DependencyResolver.UseSqlServer(sqlConnectionString);
this.ConfigureAuth(app);
app.MapSignalR();
}
}

Send TCP Message in Update/Insert/Delete Trigger

I am building a support ticket system using Sql Server 2014, ASP.Net MVC 5, angular JS etc.
As part of the design I want a way for my system to know when a ticket has been updated, deleted, or created.
That way if a user has a ticket open and it is changed while they have it open I can design the system to force them to refresh the ticket before they themselves can make changes to it, to prevent User B from overriding User A's changes they haven't seen.
Ideally, I'd like to design a TCP Protocol server as a Windows Service and be able to connect to it and send it data from table triggers in Sql Server.
Then the application front end would use Javascript and WebSockets. So the application would be connected to the socket server as well as sql server. When a user opens a ticket I would send a message that user XXY has Ticket 00X open. When a change happens in sql server it tells the server Ticket 00X changed. Then the Socket server tells clients connected to it that are looking at Ticket 00X that it has changed and the javascript prevents a submit until a fresh is done.
But... Can sql server do this at all? Doesn't appear so.
So I'm wondering if it's posisble to build a plugin for SQL Server to enable support for it like PostgreSQL's Notify feature.
Update:
I've discovered User Defined CLR Functions in SQL Server and have managed to get it working. (C#/.Net Framework) I made a static class with some static methods like,
public static int NotifyTicketUpdate(int ticketID)
{
//...
}
Then I registered it in SQL Server,
USE TLCDB;
CREATE ASSEMBLY MyCompanyName_MyDll
FROM 'd:\pathtodll\mydll.dll'
WITH PERMISSION_SET = SAFE;
CREATE FUNCTION XYZ_Notify_Ticket_Updated(#input int) RETURNS int
AS EXTERNAL NAME MyCompanyName_MyDll.UserDefinedFunctions.NotifyTicketUpdated;
Then to call it in SQL, I just do
select dbo.XYZ_Notify_Ticket_Updated(#ticketIDHere);
And it all works. My Static method in c# sends the TCP/IP message to my socket server, the server then checks to see who is looking at that ticket ID and sends them a Ticket_Updated message. The websocket layer running in client javascript sees it, and locks the ticket for updates/saves.
Or you can use Service Broker for handling asynchronous notifications. Not the simplest thing to learn, but lightweight, scalable and already built-in.
You could use CLR, which requires a bit of setup.
You could create an EXE that you can shell with parameters from an SP.
You could implement some standard concurrency. Optimistic vs Pessimistic
So yes, it's possible.

Custom & Proprietary service issue

We have a windows 2008 box with few windows services; and the services start-up type is Automatic. The concern is that, not all services (with automatic start up type) starts on successful server restart. Any idea ?
We noticed the aforementioned issue on service particularly custom windows service, SQL server related services.
Each service will need to be troubleshooted separately. Are any errors reported to the Event Log?
Perhaps your service is starting too soon, before other necessary services have been initialized. You may need to set up a dependency or set the Startup type to Automatic (Delayed Start) from the the Services Control Panel.

Simple scheduler library db for SQL Server in Azure

I would like to schedule to run simple SPs to run in my Azure DB. Options: run once and simple recurring by days, weeks, months. As SQL Server Agent is not available in Azure, I am wondering is there any simple scheduling library, db which has this functionality. I would not like reinvent the wheel.
Please do not recommend to run the SQL Server Agent outside and connect to the Azure DB thats not and option.
In case of there is no such T-SQL library (I have to implement it from scratch) could anyone point me how can I implement simple recurring run SP, which runs in every say 5 seconds in Azure.
Thx in advance
If you are fine to execute from WorkerRole use FluentScheduler. Ideally this kind of background job should be executed from worker role only.
However this library can be scheduled from web application also.But I don't prefer this as we may have multiple web roles, take double care while using in web application.
I am using FluentScheduler for my application on Azure (WorkerRole), It works great. It is also very easy to implement.
Example
Schedule<DailyDigest>().ToRunNow().AndEvery(5).Seconds();
Schedule<DailyDigest>().ToRunOnceAt(18, 0).AndEvery(24).Hours();
Schedule<WeeklyDigest>().ToRunOnceAt(DateTime.Now.StartOfWeek(DayOfWeek.Sunday).AddHours(18)).AndEvery(1).Weeks();
In above example, DailyDigest and WeeklyDigest are the classes inheriting from ITASK
public class DailyDigest : ITask
{
public void Execute()
{
//Get Daily digest
}
}
public class WeeklyDigest : ITask
{
public void Execute()
{
//Weekly Digest
}
}
Get this library from github
https://github.com/jgeurts/FluentScheduler
OR
using Nuget Install-Package FluentScheduler
Not built into Azure SQL - but there are 3rd party services that allow scheduling (Aditi and Cotega are mentioned here http://social.technet.microsoft.com/Forums/exchange/en-US/b2ae724f-4509-4c0f-aef4-58ce8be60f1a/is-there-an-equivalent-job-regular-sql-server?forum=ssdsgetstarted)
If you have Worker Roles (or other VMs running in Azure) available - you can roll your own schedule process. This, obviously, involves paying for that compute time.
If this is an option (i.e. you are paying for spinning up a VM) - you also have the option of running your own instance of SQL Server on your own VM and using "standard" SQL Server (with a standard Agent, etc.) - rather than hosting your DBs in Azure SQL. This would be a very expensive alternative...
Personally, I would look at using a Mobile Service to call the SP and then devise a way to schedule a call to the service. Not robust - but quick and dirty and cheep...
Microsoft have just announced Windows Azure Scheduler, which includes a free tier. See the pricing page

Using nHibernate in a windows service

I want to use nHibernate in a windows service. If the systems boots, it might start my service before the database. In that case, configuration of nHibernate fails and the service crashes. So now I'm wondering how I can check if the database service has already been started. In case it has not yet started, my service should wait a bit and try again later.
If your service always runs on the same machine as SQL Server, You should be using ServiceInstaller.ServicesDependedOn to tell Windows(SCM) that you depend on 'MSSQLSERVER' (the name of service that runs SQL Server).
From MSDN:
A service can require other services to be running before it can
start. The information from this property is written to a key in the
registry. When the user (or the system, in the case of automatic
startup) tries to run the service, the Service Control Manager (SCM)
verifies that each of the services in the array has already been
started.
ServiceInstaller is the class that is used by InstallUtil when it installs your service. Other installation packages including InstallShield also support this windows functionality. Equivalent SC command.
So your service will only start after SQL Server is already running. But even in this case, it might still be a good idea to offload all potentially long running startup procedures to the background thread. Do as little as possible in OnStart method. Ideally you would just spawn a new initialization thread that would take care of NHibernate session factory initialization. If for some reasons you still want to do this in OnStart, then you should consider retrying NHibernate initialization and calling ServiceBase.RequestAdditionalTime to avoid:
Error 1053: The service did not respond to the start or control
request in a timely fashion.
Ideally your service should not depend on the database availability because it might be running on a remote machine. The service is an 'always on' process that should tolerate intermittent database connectivity issues.
No clue if there are better ways, but in your service startup, check for the system uptime. If this is less then let's say 5 minutes, wait for (5 minutes - Uptime) and after that start the rest of the service as you normally would.
See the following for Calculating server uptime gives "The network path was not found"
This is not a solution however for when your service tries to connect to a SQL which is down, however if this happens you want to handle the exception and actually be notified that the SQL is down. Very unlikely you want the service to keep trying without you yourself beeing aware the SQL is down.
You could use ServiceController class and call its static method GetServices() to get the list of services. It will give an array of services, find the right one and check its status.
See ServiceController on MSDN
Currently I am making sure I can establish a connection to the database needed and running a default query (configurable). If this is successful I proceed to start the service.
What I've found in some cases is that even if the MSSQL service is started it doesn't guarantee that you can connect to it and execute queries against it.

Resources