Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to pass a service reference delegate into a generic method with error handling
    primarykey
    data
    text
    <p>I'm not sure how to best describe what I want so I'm going to start off high-level and then go over my thought on the implementation.</p> <p>Using c# I'm trying to make a method that has a generic return type, and takes as a parameter a method from a service reference.</p> <p>This generic method is going to new up the service reference, call the service reference's method that I passed in, do all the error handling and checking needed for a service reference, and then either Close or Abort it and return the result of the call.</p> <p>The sort of pseudo code of this:</p> <pre><code>public T CallServiceReference&lt;T&gt;(method serviceRefMethod) { T result = null; ServiceReference svcClient = new ServiceReference.Client(); try { result = svcClient.serviceRefMethod; svcClient.Close(); } catch (ExceptionType ex) { // log error message svcClient.Abort(); throw; } return result; } </code></pre> <p>Is this possible in c#? I'm looking in generics and delegates. One of my major issues is making a delegate of one the service reference's methods without the service ref being instantiated. If I have to instantiate the service ref I think I might as well put all of the Close, Abort and error handling for each method call.</p> <p>I'm researching different design patterns although it's a little difficult since I don't know the name of the one I'm looking for or if it even exists.</p> <p>Let me know if I can provide any additional information or clarification.</p> <p>Update (part 2): Now I'm trying to make a delegate that encapsulates variables with the method it is calling.</p> <pre><code>public delegate T Del&lt;T&gt;(); public static IEnumerable&lt;String&gt; GetDataFromService(String username) { ServiceReference.ServiceClient client = new ServiceReference.ServiceClient(); // the method I'm going to call returns a string array Del&lt;String[]&gt; safeClientCall = new Del&lt;String[]&gt;(client.DataCall); // the above call is what I need to use so the C# compiles, but I want to do this // the below code throws an error... Del&lt;String[]&gt; safeClientCall = new Del&lt;String[]&gt;(client.DataCall(username)); var result = DataCallHandlerMethod(ref client, safeClientCall); return result; } </code></pre> <p>Basically pass the username parameter from my calling method and that username parameter is already defined. I don't want to define it when the delegate is invoked. Is there any way to do this using c#?</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.
 

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