What is the best workaround for the WCF client `using` block issue?

前端 未结 26 2234
余生分开走
余生分开走 2020-11-22 00:03

I like instantiating my WCF service clients within a using block as it\'s pretty much the standard way to use resources that implement IDisposable:

26条回答
  •  深忆病人
    2020-11-22 00:16

    Actually, although I blogged (see Luke's answer), I think this is better than my IDisposable wrapper. Typical code:

    Service.Use(orderService=>
    {
      orderService.PlaceOrder(request);
    }); 
    

    (edit per comments)

    Since Use returns void, the easiest way to handle return values is via a captured variable:

    int newOrderId = 0; // need a value for definite assignment
    Service.Use(orderService=>
      {
        newOrderId = orderService.PlaceOrder(request);
      });
    Console.WriteLine(newOrderId); // should be updated
    

提交回复
热议问题