2016-11-18 54 views
0

我已經構建了一個緩存對象的azure服務結構應用程序/服務。現在使用下面的代碼,我在調用服務的控制檯窗口中創建了一個客戶端。 當在c.Add上放置一個斷點然後跨過它時,程序剛剛退出。不會拋出異常,「完成」不會打印,程序僅以代碼0退出。 服務正在運行並處於健康狀態。我能做些什麼來找出什麼是錯的?服務結構客戶端剛剛退出

class Program 
{ 
    static void Main(string[] args) 
    { 
     Test(); 
    } 

    static async void Test() 
    { 
     Uri serviceName = new Uri("fabric:/CacheApp/PreorderCache"); 
     ServicePartitionResolver resolver = new ServicePartitionResolver(() => new System.Fabric.FabricClient()); 
     NetTcpBinding binding = (NetTcpBinding)WcfUtility.CreateTcpClientBinding(); 

     Client c = new Client(new WcfCommunicationClientFactory<IPreorders>(binding, null, resolver), serviceName, 1); 

     try 
     { 
      PreOrder po = await c.Get("50", "11001", OrderNumber = "123456" }); 
      Console.WriteLine("done"); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.ToString()); 
     } 
    } 
} 

經過調查,似乎與該服務中使用的交易有關。

服務一直無法

await tx.CommitAsync(); 

這裏是在服務斷層代碼,沒有什麼特別的

await preOrders.GetAsync(tx, d, s, (k, v) => preOrder); 
ServiceEventSource.Current.ServiceMessage(this, "after update"); 
await tx.CommitAsync(); 
ServiceEventSource.Current.ServiceMessage(this, "after commit"); 

回答

1

Test是不是從Main等待異步方法。所以你的程序不會等待Test完成。

添加GetAwaiter().GetResult()會使您的Test方法像常規方法一樣工作,該方法會阻止,直到c.Get()完成。

所以使用選項2,並刪除'異步'。

(並保留使用async void來異步事件處理程序。)