Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I wrote a <a href="https://web.archive.org/web/20160315041245/http://www.atrevido.net/blog/2007/08/13/Practical+Functional+C+Part+II.aspx" rel="nofollow noreferrer">higher order function</a> to make it work right. We've used this in several projects and it seems to work great. This is how things should have been done from the start, without the "using" paradigm or so on.</p> <pre><code>TReturn UseService&lt;TChannel, TReturn&gt;(Func&lt;TChannel, TReturn&gt; code) { var chanFactory = GetCachedFactory&lt;TChannel&gt;(); TChannel channel = chanFactory.CreateChannel(); bool error = true; try { TReturn result = code(channel); ((IClientChannel)channel).Close(); error = false; return result; } finally { if (error) { ((IClientChannel)channel).Abort(); } } } </code></pre> <p>You can make calls like this:</p> <pre><code>int a = 1; int b = 2; int sum = UseService((ICalculator calc) =&gt; calc.Add(a, b)); Console.WriteLine(sum); </code></pre> <p>This is pretty much just like you have in your example. In some projects, we write strongly typed helper methods, so we end up writing things like "Wcf.UseFooService(f=>f...)".</p> <p>I find it quite elegant, all things considered. Is there a particular problem you encountered?</p> <p>This allows other nifty features to be plugged in. For instance, on one site, the site authenticates to the service on behalf of the logged in user. (The site has no credentials by itself.) By writing our own "UseService" method helper, we can configure the channel factory the way we want, etc. We're also not bound to using the generated proxies -- any interface will do.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload