Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7547213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:23:45+00:00 2026-05-30T09:23:45+00:00

I am currently working on a Windows Service that does a database call and

  • 0

I am currently working on a Windows Service that does a database call and does some work and then waits for several seconds to run again. I have to connect to a database during the work and I would like to keep the database connection somewhere that I know will be cleaned up when the object is done.

I was thinking of using something like HttpContext but obviously Windows Services do not have anything like that. What is the best way to make sure my connections are disposed properly after each call?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T09:23:46+00:00Added an answer on May 30, 2026 at 9:23 am

    There is always fallback to static properties possible. So you can keep it in a static variable on a class.

    public class ServiceContext
    {
        public static DbConnection Connection {get; set;}
    }
    

    However, this is not good practice. It seems that you need to:

    1. Encapsulate the operation in a class. This will run and then close the connection.
    2. Use Async paradigm to run the operation. You will ensure that you are not bugging the main operations of the service while database call is being made.

    Best is to use TPL: Task<T>.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently working on a C++ program in Windows XP that processes large sets
I'm currently working on databinding some of my existing Windows Forms, and I've ran
I'm working on a PyGTK/glade application that currently has 16 windows/dialogs and is about
Currently working on a VBScript to automate some of the dirty PST ingestion work
I'm currently working on a TimerJob which does some site collections management. When the
I am currently working on a new application (.Net 3.5/Win2008 Server environment) that does
I'm working on a project that uses a Windows Mobile device to call a
I'm currently working on developing a windows service which will be deployed to all
I'm currently working on a Windows service (my first) and I'm wondering how to
I need to write Hebrew text in Latex. I am currently working on Windows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.