Search code examples
c#wcf

Background thread with HostingEnvironment.QueueBackgroundWorkItem in WCF


I have WCF service exposed which validates the parameters and returns true/false accordingly and run a background thread to process results. For that, I have tried to do it with the HostingEnvironment.QueueBackgroundWorkItem but it gives me the following error:

Operation is not valid due to the current state of the object.

Code is given below:

public class SearchService : ISearchService
{
    public async Task<bool> SearchAsync(UserSearch search, string email)
    {
        //Some operations
        var searchManager = new SearchManager();
        HostingEnvironment.QueueBackgroundWorkItem(ct => searchManager.PerformSearch(search, email));
        return true;
    }
}

Solution

  • I got it working with the following :

    var thread = new Thread(
                    async () =>
                    {
                        await searchManager.PerformSearch(search, email);
                    }) {IsBackground = true};
    
                thread.Start();
    

    Any better option please do let me know.