WCF以新的线程行为回收

时间:2018-06-24 12:27:37

标签: c# wcf

我的WCF具有以下三个方法,每个方法基本上都接受一个整数,并使用Thread.Sleep(1000)记录到文件以模拟第二个方法。我试图为每个方法提交一个值(30),导致每秒写入日志文件。在处理请求时,我从IIS手动回收了应用程序池。

结果是前两个方法仍将继续处理,而最后一个方法(NewThreadTest)将停止。如何解释这种行为?

    [OperationContract]
    public void LongProcess(int val)
    {
        WriteToFile("Received LongProcess: " + val);
        for (int i = 0; i < val; i++)
        {
            WriteToFile(i.ToString());
            Thread.Sleep(1000);
        }
        WriteToFile("LongProcess completed");
    }


    [OperationContract(IsOneWay = true)]
    public void OneWayLongProcess(int val)
    {
        WriteToFile("Received OneWayLongProcess: " + val);
        for (int i = 0; i < val; i++)
        {
            WriteToFile(i.ToString());
            Thread.Sleep(1000);
        }
        WriteToFile("OneWayLongProcess completed");
    }

    [OperationContract]
    public bool NewThreadTest(int val)
    {
        Action a = new Action(() =>
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + @"\NewThreadTest.log";
            System.IO.File.AppendAllText(path, "Received val: " + val);
            for (int i = 0; i < val; i++)
            {
                System.IO.File.AppendAllText(path, Environment.NewLine + i);
                Thread.Sleep(1000);
            }
            System.IO.File.AppendAllText(path, Environment.NewLine + "done.");
        });
        Task.Factory.StartNew(a);
        return true;
    }

0 个答案:

没有答案