WCF调用后客户端异步方法不继续

时间:2019-06-28 05:48:44

标签: c# wcf asynchronous async-await

我有合同库,该库将同时用于客户端应用程序和WCF服务(重用现有库)。在此合同库中,我有一个异步方法,该方法将在客户端调用。当我调用此方法并尝试在其方法运行时进行WCF调用时,WCF返回新对象,并且我松开了原始对象引用。该方法正在其他对象上工作。有没有办法防止丢失此参考资料?因此,它可以继续在同一对象上进行方法调用。

合同库:

[DataContract]
    public class ComVariable
    {
        [DataMember]
        public int ReadValue { get; set; }

        public async void WriteReadValue()
        {
            Console.WriteLine(this.ReadValue);

            await Task.Delay(5000);

            Console.WriteLine(this.ReadValue);
        }
    }

WCF服务方法:

public class Service1 : IService1
    {
        public ComVariable GetComVariable(ComVariable comVariable)
        {
            //Complex reading algorithm from some device
            comVariable.ReadValue++;
            return comVariable;
        }
    }

客户端:

static void Main(string[] args)
        {
            ComVariable comVariable = new ComVariable();
            comVariable.ReadValue = 1;

            comVariable.WriteReadValue();

            Service1Client service1Client = new Service1Client();
            comVariable = service1Client.GetComVariable(comVariable);

            Console.Read();
        }

我想在控制台屏幕上看到1和2,但是旧的comVariable对象的async方法不受Web服务调用的影响。但是它会在控制台上写入1和1。

在wcf调用之后,我只能均衡对象的属性:

comVariable.ReadValue = service1Client.GetComVariable(comVariable).ReadValue;

这将起作用,但是我不想这样做,因为我将拥有List和ComVariable类将具有太多应被均衡的属性。

0 个答案:

没有答案
相关问题