my windows service starts before the sql server service - c#

I have a windows service written in c#, which when running connects to a sql server database.
When the service first starts it connects to the database to obtain various information for the service to run.
The service is set to automatically start if the server it is installed on is restarted.
If the service starts before the sql server service has started. My service when started obviously has an initial execption error when trying to connect to the database.
To combat this issue, if the exception occurs I have added a short delay before try to connect to the database again, I continue this n amount of times until it connects
My question is, is there any alternative solutions to what I am doing already
thanks

Make your service dependent on SQL Server, done using ‘sc’.
Using a poll, as you're doing right now is fie, and you'll still need it if your service starts after SQL Server because you can never be sure SQL Server has fully started then.

Related

Website can't connect to SQL Server, but console app can

I have a SQL Server with two databases, a production database and a development database. The .net 2.0 website hitting the production database with manual SqlConnection code is working fine. The other database is being hit from a newer ASP.NET MVC app using Entity Framework 6.2 and is getting timeout issues. The timeout takes 30 seconds the first time, but the page comes back almost instantaneously on subsequent refreshes. Both websites are on the same box as the database, so are only using "localhost" to connect. They are using SQL Server user logins, not Windows authentication.
I copied the .edmx and .tt files into a .net console app and that app has no problem hitting the database with the exact same linq query and pulling the same data that is failing.
I then created a new web site and copied just that same code into an aspx page. It fails the first time with a timeout, and then works on subsequent attempts (and a week ago, the main dev site was doing the same thing).
I separated the dev database from the SQL Server 2008 R2 server and attached it to a newly installed instance of SQL Server Express on a different port, and get the same results.
The web server is windows server 2008 standard 32-bit. I copied both websites and the console application to a new box (I thought was 2016, but it turns out it is 2008 standard 64-bit) and get the same results.
The dev site was working up until a couple of months ago. The client was using local user accounts for everything, but had a domain and wanted to do testing with windows authentication for an old vb app that hits the same database, and I had started migrating testing accounts to the domain. When the client tried to later, for an unrelated reason, change his password, we discovered that he was already using a domain account, but that his laptop could not connect to the domain. We found several other computers that could not connect, even though the machines I had connected to the domain during my testing were working fine. An outside network "friend" was brought in to figure out what was going on. At that point, I lost all track of what was actually done. I know that different network and domain configurations were tried and didn't fix the domain issues, but I don't know what. However, the production site was never rendered inoperative.
I have no idea what is going on. Does anyone else?
Oh, and in case it was a provider issue, I've also tried manual connection using OleDbconnection from the web app, and it also fails with the Timeout issue.
Update:
I spun up a new DataCenter 2016 box, installed IIS and .net on it and copied the website to that box. It has no problems hitting the database and pulling the data from the other server.
I know patches and such were updated on the original box while the domain and network were being manipulated, but I don't know how far behind they were. I suspect that some patch changed some default or inherited .net configuration options or something. I did do a "repair" on the .net installation, and that didn't make a difference. However, with the production site working fine, I'm not currently willing to uninstall .net or anything else. I'm afraid I would risk pushing this same error into the production site and the client would be screwed.
It seems that for some reason, the timeout period elapsed while attempting to consume the pre-login handshake acknowledgement.
Try increasing the connect timeout property in your connection string to 60 or more. Default is 15 (in seconds).
Example: Data Source=(LocalDB)\v11.0;Integrated Security=True;Connect Timeout=30

Windows programming service is unable to connect to Mysql database

I am using a windows programming in c#,the windows service which I am using as to connect mysql database to fetch the data and it has to upload the data to server.But when the services is being started in my local system, the windows service is getting started first and then Mysql database service is getting started.
In this case, it is unable to connect to mysql database, so that I have to create a dependency that mysql should start first and then windows service has to start next.
How to implement the dependency code in my program,such that the clients who are using this windows service should not effect with this problem,I have do it through code that it has to be done automatically.
You can set DelayedAutoStart = true for you System.ServiceProcess.ServiceInstaller in your Installer class.

Checking "connectivity" between remote server A and remote server B programmatically

We have a requirement (I believe as part of a post build/deployment verification test (BVT)) to test that remote server A can "connect" to remote server B.
I want to develop a console application using C# on my LOCAL developer VM. This should then "connect" to remote server A. It then needs to check that remote server A can "connect" to remote server B (via the original connection from LOCAL to remote server A).
Currently I have generated a simple "helper" exe that connects to remote server B (using a prior known connection string point to remote server B and SqlConnection classes). The exe is copied to remote server A as part of the build process (at the end). PSEXEC.exe on the build server then remotely starts the "helper" exe on remote server A and checks the %errorcode%. This then determines if the BVT should pass or fail (Then the "helper" exe is then deleted from remote server A).
FYI all machines run Windows Server 2008 R2 x64 Enterprise and are on the same local domain. I am an administrator on all 3 machines.
Remote server A is a web server and remote server B is a SQL database server. The LOCAL machine will/does represent the build server. This test is simple as saying "can the web server connect to the database server". The difficulty is how to do that remotely.
I know I can just do System/integration/end-to-end testing (using Coded UI etc) and we are doing this but want to consider this requirement/test as well (as the servers and network configuration etc are a "shared responsibility" with an external party and we cant guarantee that anything has/will change without our knowledge (other than reacting via Service Desk processes))
The process I have adopted seems OTT but does appear to work (if I close ports etc). Is there a completely .NET way of doing it using TCPCLient classess or something etc? What about doing webservices and hosting them on the webserver and call them from the build server? Other ideas and things to consider more than welcome.
Why not create a test page on the web server that carries out an innocuous operation against the SQL server.
The page might execute a simple query (select top(1) name from person) for example.
I can see web-services tagged in your question. If you want to verify connectivity to a server hosting web services or between a server hosting web services and an underlying back-end server (database for example), the best way to be sure (in my opinion) would be to call an innocuous service that does a simple read. Retrieving WSDLs would not be enough.
You can use Management objects to query and control computers and servers on your domain.
http://msdn.microsoft.com/en-us/library/system.management.aspx
SQL server includes some extra objects specific to managing SQL Server.
http://msdn.microsoft.com/en-us/library/ms220749.aspx
If you have Linqpad:
ManagedComputer mc = new ManagedComputer( "yourserver" );
mc.Dump();
otherwise create a console app and create an instance of ManagedComputer and use the debugger to query the computer's SQL server, to see the sort of information available.
As you can see its a big subject...

Creating a Database Query Service

I've been asked to create a Service for our Parent Company. They don't care how I do it, as long as the data is sent to them.
We have an SQL 2000 Server that receives machine data via Data Transformation Services (DTS).
Our Parent Company wants me to create a Service that runs every 5 minutes or so to collect new data, summarize it, and forward it to them.
With my background in Windows Forms development, I naturally think that I should poll the database every 5-minutes using some type of Windows Service, then send that data over to our Parent Company.
The machine housing this data is an old Windows 2000 machine, and our Network Administrator has recommended that I write this as a Web Service on our newer Web Server.
I created a Web Service a few months back for the Web Server to pull work order information from our Parent Company, but I do not know how to make this Service execute a process every 5-minutes.
Yesterday, I learned how to create an Insert After Trigger when records were added to the table. Unfortunately, the triggers are not called because this old server uses DTS jobs. I was able to learn about Controlling Trigger Execution When Bulk Importing Data, but there does not seem to be a way to modify our old DTS jobs to enable the BULK INSERT command. It may not work on SQL Server 2000.
So, with this background, should I create a Windows Service or a Web Service?
How should I proceed?
I would not make a web service for a recurring task. Web services are not very comparable to a windows service.
btw: A simpler alternative might be to create a command-line app that runs, periodically via a scheduled task (read about the "AT scheduler in Server 2000"). I is just easier to install and make updates because it wouldn't require a reboot of your server each time you make an update.
If the webservice has a method which you can call that executes the data importing/converting exactly one time, you can use a windows task or cron job to make a request to that method. You can either add this task to the server that is hosting the service, or some other server as long as it can access the webservice.

SQL Server 2005 Browser Service

I've been doing a little work with SQL Server and C# and every time I run the darn thing the connection times out unless I start services.msc and manually restart the service (SQL Browser Service). It's already set to automatic, does anyone know why it would do this? I'm not an advanced programmer, more of an intermediate one. Once I do restart this service it works fine as long as I keep using it. If I stop using it for more than 30 minutes I have to restart it again.
Have you checked the SQl Server Log and the windows event log?
Can you supply some more info., such as the C# code you are using to open/close the connection, the connection string etc.
Is autoclose kicking in?
Defaults to ON for SQL 2005 Express version, but it could have been set for other versions. The database should open automatically though...
Is it a query from the app that times out?
Or simply trying to connect to the server to run, say, SELECT ##SERVERNAME?
Can you connect to the SQL Server by name through SSMS without fiddling with the Browser service (I'm not even sure this really requires the Browser service depending on how you connect)? i.e. does it appear to be running fine except when your program causes it to crash? And once you've restarted it does it work fine repeatedly or do you have to keep restarting it as you use your program?
What version of SQL Server 2005 (and what service pack level) are you using?

Categories