title: .Net WebServices and out/ref WebMethod arguments
body: <p>I've received some documentation from one of our suppliers for a webservice they're publishing and they're very specific that on one of their WebMethods that an argument has the out modifier(? not sure if that's the right descriptor) for instance consider the following WebMethod signature:</p>
  
  <pre><code>[WebMethod]
  public void HelloWorld(out string strVal) 
  { 
      strVal = "Hello World";
  }
  </code></pre>
  
  <p>[Obviously the actual method isn't a Hello World method]  </p>
  
  <p>Now, I'd never considered designing a WebMethod with an out/ref argument and it got me wondering why they would've used it.</p>
  
  <p>Trying to understand an application for this design decision I threw a prototype together with a few basic Hello World style webmethods...one with a single out string argument, one with two out string arguments and one that doesn't receive any arguments but returns a string.</p>
  
  <p>Upon trying to reference my webmethods from a separate application I notice that I have to access the method with the single out string argument exactly as if I'd defined the method to output the string so that in effect as far as the client is concerned:</p>
  
  <pre><code>public string HelloWorld1()
  {
    return "Hello World";
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>public void HelloWorld2(out string strVal)
  {
    strVal = "Hello World";
  }
  </code></pre>
  
  <p>are exactly the same...in that I have to reference them both as such [where x is substituted for the correct method]:</p>
  
  <pre><code>string val = HelloWorldX();
  </code></pre>
  
  <p>Having attempted to reference the methods in the way I would access them if they weren't web methods [like so]:</p>
  
  <pre><code>string val = string.Empty;
  MyService1.HelloWorld(out val);
  Console.WriteLine(val);
  </code></pre>
  
  <p>which causes a compilation error stating that no method arguments accept 1 input.  Why is that?  There's obviously a web method that accepts one argument - I'm looking at it [HelloWorld2].</p>
  
  <p>Upon examining the SOAP responses, I notice that the content of the response for HelloWorld1 is:</p>
  
  <pre><code>&lt;HelloWorld1Response xmlns="http://tempuri.org/"&gt;
    &lt;HelloWorld1Result&gt;string&lt;/HelloWorld1Result&gt;
  &lt;/HelloWorld1Response&gt;
  </code></pre>
  
  <p>And HelloWorld2 is</p>
  
  <pre><code>&lt;HelloWorld2Response xmlns="http://tempuri.org/"&gt;
    &lt;strVal&gt;string&lt;/strVal&gt;
  &lt;/HelloWorld2Response&gt;
  </code></pre>
  
  <p>Going a step further I thought, what if I have 2 ref arguments...</p>
  
  <pre><code>public void HelloWorld3(out string strVal1, out string strVal2)
  {
      strVal1 = "Hello World";
      strVal2 = "Hello World Again!";
  }
  </code></pre>
  
  <p>This generates the SOAP content:</p>
  
  <pre><code>&lt;HelloWorld3Response xmlns="http://tempuri.org/"&gt;
    &lt;strVal1&gt;string&lt;/strVal1&gt;
    &lt;strVal2&gt;string&lt;/strVal2&gt;
  &lt;/HelloWorld3Response&gt;
  </code></pre>
  
  <p>I thought fair enough, so theoretically [providing I can figure out a way to pass out/ref arguments to WebMethods] that means I can just pass in two arguments that can be set by the method, but when I do this:</p>
  
  <pre><code>string val1 = string.Empty;
  string val2 = string.Empty;
  MyService1.HelloWorld3(out val1,out val2);
  Console.WriteLine(val1);
  Console.WriteLine(val2);
  </code></pre>
  
  <p>I should get the same compilation error I saw when I tried to reference the HelloWorld2 this way.  With the obvious exception that it's complaining about 2 arguments instead of 1 [and in fact I do get the same exception, I tested it].</p>
  
  <ul>
  <li>What gives?</li>
  <li>Is there a reason or a way to use out/ref arguments in WebMethods that I'm missing?   </li>
  <li>If there is, how do I reference WebMethods with multiple out/ref arguments?</li>
  </ul>