locked
Start a new thread in WCF RRS feed

  • Question

  • I am trying to start a new thread in my WCF method, but the thread is aborted when a WCF method ends with execution.

    [OperationContract]
    public int GetWorld(int ID)
    {
    ThreadPool.QueueUserWorkItem(SomeMethod, someParameter);
    }
    I don't want to set InstanceContextMode to Single, since this affects performance. There must be another solution.
    Friday, October 2, 2009 1:08 PM

Answers

  •  try to use a delegate and invoke it.

    First set ConcurrencyMode to ConcurrencyMode.Multiple:
     

    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class MyService : IMyService
    {
       ...
    }
     

    then create a delegate which matches the parameters of the method you like to call:
     
    private delegate int MyDelegate(int parameter);
     

    then invoke the delegate:

     
    MyDelegate delegate = MyMethod;
    
    delegate.BeginInvoke(2); 
     
    Tuesday, October 6, 2009 10:40 AM

All replies

  • You wcf service method says it suspose to return an integer but it returns nothing. I am not sure what you are trying to accomplish here

    Saturday, October 3, 2009 5:47 AM
  •  Well, this is just the demo, there is approx 30 line of codes that are missing, but they are not important.

    I would like to start a new thread. I have some work to do. It could take more then 5 hours, so i can not wait WCF to finish it and then return result. User may logout meanwhile.
    I have some custom logic to view progress and results.

    I need to start a new thread to accomplish that 5 hours work.

    Saturday, October 3, 2009 4:48 PM
  •  try to use a delegate and invoke it.

    First set ConcurrencyMode to ConcurrencyMode.Multiple:
     

    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class MyService : IMyService
    {
       ...
    }
     

    then create a delegate which matches the parameters of the method you like to call:
     
    private delegate int MyDelegate(int parameter);
     

    then invoke the delegate:

     
    MyDelegate delegate = MyMethod;
    
    delegate.BeginInvoke(2); 
     
    Tuesday, October 6, 2009 10:40 AM