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

  • SEARCH
  • Home
  • 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 8941489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:18:53+00:00 2026-06-15T11:18:53+00:00

I am currently working with a WCF service that is supposed to spawn a

  • 0

I am currently working with a WCF service that is supposed to spawn a new task to be run asynchronously on a server (there are db queries etc that need to take place). It is possible (and very likely) that the response is sent back to client before the new task is finished warranting the client to close. At this point the dependencies that were available during creation of the task will no longer be available. I am still in need of some of the dependencies in order to complete the task.

How would I want to go about making sure the dependencies required for the new task are still alive?

I included some very dumbed down code to give a basic example.

public string SubmitData(
            User user, Request request)
        {
            History history = m_history.CreateRequest(user);

            //New task which will do an import of data into the DB.
           Task.Factory.StartNew( () => 
               Import( user, request, history ) ); 

           /*Return some sort of response back to user so they're not waiting for 
           *the long process to complete           
           */
           return "Response";
        }

        private void Import(
            User user,
            Request request,
            History history)
        {
            var response = Import(
                user, request, history);
            m_history.Save(history, response );
        }
  • 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-06-15T11:18:55+00:00Added an answer on June 15, 2026 at 11:18 am

    You will need to take responsibility for disposing of the dependencies yourself. Since you are using autofac, your the simplest solution is likely to used Owned<T>

    See documentation: Owned Instances

    Here is roughly I would factor your code to utilize Owned<T>, though there are lots of possibilities:

        private Func<Owned<Importer>> importerFactory = //Constructor Injected.
    
        public string SubmitData(
            User user, Request request)
        {
            History history = m_history.CreateRequest(user);
    
            //New task which will do an import of data into the DB.
            Task.Factory.StartNew(() =>
                {
                    using (var importer = importerFactory())
                    {
                        importer.Value.Import(user, request, history);
                    }
                });
    
            /*Return some sort of response back to user so they're not waiting for 
           *the long process to complete           
           */
            return "Response";
        }
    
        public class Importer
        {
            //m_history ...
    
            public void Import(
                User user,
                Request request,
                History history)
            {
                var response = Import(user, request, history);
                m_history.Save(history, response);
            }
        }
    

    Judging from your question. You may benefit from some additional reading on Autofac, lifetime, and deterministic disposal.

    Instance Scope

    Lifetime Primer

    Deterministic Disposal

    Once you more firmly understand how autofac works, you will realize your problem is actually more general. Namely, how to ensure proper disposal of shared objects used by in Tasks. In my code above, I solved the issue by not sharing the object, and making the task handle disposal.

    • 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 WCF service that reaches out to another service to
I'm currently working on a wcf service that does some lookups in a database
I am currently working on a WCF application that consumes a Rails REST Service.
I am currently working on a project that contains a WCF service, a Windows
We are currently working on an application that will use a WCF service. The
I am currently working on a project that should be implemented as a WCF
I am currently working on a WCF service with a large number of methods
I have a WCF Service that is transferring large files. Currently I'm using Singleton
I've got a client/server setup that I am implementing using a WCF Service (
I'm currently working in a dispatcher service that processes thousands of messages delivered in

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.