none
Access object/variable being modified by the Task RRS feed

  • Question

  • Hi,

    I am trying to access the variable which is being modified by a function called by Task.Factory.StartNew task. The code is in WCF Service and when I switch the URL to https, it stops working and I don't receive updated value from the Task.

    namespace EDSWcfService
    {
        [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Multiple)]
        public class Service1 : IService1
        {
            TaskHandler objTaskHandler = new TaskHandler();
            public string GetData(int value)
            {
                Task.Factory.StartNew(()=>objTaskHandler.TaskFunction());
                string strTemp = string.Format("You entered: {0}", value) + " and Task returned string = " + objTaskHandler.GetMessage();
                return strTemp;
            }
    
            public string GetMessage()
            {
                return objTaskHandler.GetMessage();
            }

        }
        
        public class TaskHandler
        {
            private string strMessage = string.Empty;
            int i = 0;
            public void TaskFunction()
            {
                strMessage = "Entered Task";
            }
    
            public string GetMessage()
            {
                i++;
                return strMessage + i.ToString();
            }
        }
    }

    It has been troubling a lot. I have a client that need to get the updated status per second from the WCF Service which in turns invoked a task to do the calculation and supply the updated value every time client makes a call.

    Thanks


    • Edited by SachinZen Thursday, June 2, 2016 10:17 PM GetMessage function is called per second by the client
    • Moved by DotNet Wang Friday, June 3, 2016 6:27 AM
    Thursday, June 2, 2016 10:12 PM

All replies

  • Maybe objTaskHandler should be made static?

    Friday, June 3, 2016 5:20 AM
  • I tried that and it worked but it started affecting other client sessions.

    Thanks for your response.

    Friday, June 3, 2016 5:51 AM
  • Hi SachinZen,

    >>”The code is in WCF Service and when I switch the URL to https, it stops working and I don't receive updated value from the Task.”

    This forum is about the C# programming language, IDE, libraries, samples, and tools, as your issue is more related to the WCF Development, we help you move it to the WCF forum for better support.

    Thank you for your understanding.

    Best Regards,

    Albert Zhang

    Friday, June 3, 2016 6:27 AM
  • Thanks. Please let me know how should I move it to WCF Development?
    Friday, June 3, 2016 6:18 PM
  • Hi SachinZen,

    >>The code is in WCF Service and when I switch the URL to https, it stops working and I don't receive updated value from the Task.

    Have you configured your WCF Service to work both in the https and http? If you do not configure your WCF service to work with https, then when you switch the URL to https, it will not work as expected.
    For more information, please try to refer to the following articles about how to create a https WCF service:
    #How to: Configure an IIS-hosted WCF service with SSL:
    https://msdn.microsoft.com/en-us/library/hh556232(v=vs.110).aspx .
    #Seven simple steps to enable HTTPS on WCF:
    http://www.codeproject.com/Articles/36705/simple-steps-to-enable-HTTPS-on-WCF-WsHttp-bindi .

    If you have configure the https for the WCF Service, then you said that it stops working, have you met any exception or error? If so, welcome to post the exception or the error message in here.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, June 6, 2016 6:55 AM
    Moderator
  • Thanks

    There is no exception raised. The same code works when configuring HTTP. What we realized that changing the security mode to "TransportWithMessageCredential" from "Transport" in web.config made the same code working.

    Do you think changing the mode has affects on invoking the activities happening under the raised Task object?

    Thanks!

    Monday, June 6, 2016 10:10 PM
  • Hello,

    >>Do you think changing the mode has affects on invoking the activities happening under the raised Task object?

    Based on your description, it seems that changing the security mode to "TransportWithMessageCredential" from "Transport" make your code work with the Https. If so, in my mind changing the mode from TransportWithMessageCredential" from "Transport" will effect whether the Https WCF Service works or not, if the Https WCF Service does not work, it will have affects on invoking the activities happening under the raised Task object.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, June 8, 2016 5:20 AM
    Moderator
  • Sorry, I didn't quite understand your response. Can you please check and provide some details on why mode matters for invoking the task?

    Appreciate your time!

    Thanks!

    Thursday, June 9, 2016 12:26 AM
  • Hello,

    >> Can you please check and provide some details on why mode matters for invoking the task?

    If the Https WCF Service does not work, then we can not consume the WCF service, in this way the task will not work. If we want to make the task work, we need to make sure that the Https WCF Service works fine.

    As I have said in my first reply, a WCF service works with the Http does not mean that it will work with the Https, we need to do some others configuration to let it work with the Https.

    Changing the mode from TransportWithMessageCredential" from "Transport" will effect whether the Https WCF Service works or not, in this way it will affect whether the task works or not. You can check that by entering the https WCF Service address in the browser of the client side to see if it works when using the "Transport" security mode, if it does not work, of course the task will not work.

    If you still have any questions, please feel free to let me know.

    Thanks for your understanding.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.









    Tuesday, June 14, 2016 4:07 AM
    Moderator
  • Thanks Amy.

    As I indicated once we switch from "transport" to "TransportWithMessageCredential" in HTTPS URL the same service works and the task initiated by WCF function starts executing. But the same code fails to initiate task when switching back to "transport" mode in HTTPS. Other functions which are not part of task still works fine under HTTPS "transport" mode. So, it doesn't looks like problem in setting up the HTTPS. Its the mode that has something to do with the Task initiation.

    Wednesday, June 15, 2016 5:23 PM
  • Hi SachineZen,

    If the WCF Https Web Services work both in the Transport and TransportWithMessageCredential mode, in my mind the Task initiation should work as well in both of the two modes and I do not find any documentation that it is related with your scenario. Could you please try to share your config file in here? I wonder if there is any other configuration that may be related with that.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, June 20, 2016 5:59 AM
    Moderator