Note that there are some explanatory texts on larger screens.

plurals
  1. POWCF - How to hold on to the Channel/Proxy between Begin and End call
    text
    copied!<p>I'm attempting to use the ChannelFactory to call an asynchronous service.</p> <p>The bit I'm unsure of is how to best hold on to the reference to the channel - between the call to BeginSave and then EndSave (which is in the callback method). </p> <p>I'm currently doing this by assigning it to an instance variable, but this doesn't seem ideal. I only want the channel to exist for the lifetime of the call (which I think is best practice i.e. Open, use it, close it). I also want to make sure I'm closing the correct channel, there could be situations where there's more than one in existence.</p> <p>Any examples on how best to do this would be great.</p> <p>Here's my code:</p> <pre><code>public partial class MyForm : Form { ICompanyService m_Channel; public MyForm() { InitializeComponent(); // ChannelProvider is a class I've created which returns a channel created by the ChannelFactory ChannelProvider&lt;ICompanyService&gt; channelProvider = new ChannelProvider&lt;ICompanyService&gt;(); m_Channel = channelProvider.GetChannel(); } private void OnSaveButtonClickAsync(object sender, EventArgs e) { Company company = new Company(); company.BranchId = "A1"; company.CompanyName = "A1 LTD"; m_Channel.BeginSave( company, new AsyncCallback(OnSaveCallback), null); StatusLabel.Text = "Saving..."; } private void OnSaveCallback(IAsyncResult ar) { int result = m_Channel.EndSave(ar); Invoke( new MethodInvoker(delegate() { StatusLabel.Text = result.ToString(); })); } } </code></pre> <p>Many thanks.</p>
 

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