title: Consume SOAP Web Service in Silverlight
body: <p>I'm attempting to consume a SOAP service in a Silverlight 5 application and I'm completely lost. This is my first Silverlight app and only my second time using web services in a .NET application.</p>
  
  <p>In a separate .NET application, the only way I was able to get it to work was by adding the WSDL as a Web Reference; the application would not build when I added it as a Service Reference. In talking to the WSDL provider, I discovered that the WSDL was compiled using the .NET 2.0 framework...hence the need to add it as a Web Reference.</p>
  
  <p>From the research I've done thus far, I see that Silverlight doesn't support adding a Web Reference. So I tried adding it to the hosting ASP.NET application as a Web Reference then started the server.</p>
  
  <p>Back in my Silverlight app, I selected the option to add a Service Reference and pointed to the WSDL file now at <code>http://localhost:55265/Web%20References/THINKWebService/SLWebSvc_734_Upgrade.wsdl</code>. Visual Studio seemed to pick it up just fine and generate the proxies.</p>
  
  <p>Here's where I start to get stuck. If my research is correct, a WCF reference was created and should be used in that manner. I've never used WCF so I did some reading on how to send/receive requests and this is the best code I've come up with, based on examples in the MSDN library (I inserted it into a button click event so I would know exactly when the code was executing):</p>
  
  <pre><code>private void Button1Click(object sender, RoutedEventArgs e)
  {
    var client = new ThinkSoapClient();
    var userLoginData = new user_login_data {login = "foo", password = "bar"};
    var customerIdentifier = new customer_identifier {customer_id = 6677070};
    // the debugger halts on this next line and
    // references the "dsn"...it's the 4th argument
    client.CustomerLoginInfoSelectAsync(userLoginData, customerIdentifier, "", "myDSN");
    // I'm not sure if this next line is even needed
    client.CustomerLoginInfoSelectCompleted += CustomerLoginInfoSelectCallback;
    MessageBox.Show(string.Format("CustomerLoginInfoSelectAsync({0},{1})", userLoginData, customerIdentifier));
  }
  
  // here's the callback method
  static void CustomerLoginInfoSelectCallback(object sender, CustomerLoginInfoSelectCompletedEventArgs e)
  {
    MessageBox.Show(string.Format("CustomerLoginInfoSelect Result: {0}", e.Result));
  }
  </code></pre>
  
  <p>As I mentioned in the code above, the debugger halts when executing the <code>client.CustomerLoginInfoSelectAsync</code> method. Here's the error message: <code>XmlSerializer attribute System.Xml.Serialization.XmlAttributeAttribute is not valid in dsn. Only XmlElement, XmlArray, XmlArrayItem and XmlAnyElement attributes are supported when IsWrapped is true.</code></p>
  
  <p>From the research I've done, I think this error is being caused because the the SOAP action element contains an attribute <code>dsn</code> (not sure, though, if I would be getting this error if the sub-elements also had attributes). </p>
  
  <p>I did a find/replace for <code>IsWrapped=true</code> to <code>IsWrapped=false</code> in <code>Reference.cs</code> but I got the same error but the last word was false instead of true.</p>
  
  <p>I'm not sure if I'm making any sense as to what I'm after, so here's what the generated XML should look like in case it helps:</p>
  
  <pre><code>...
    &lt;customer_login_info_select_request dsn="myDSN"&gt;
      &lt;user_login_data&gt;
        &lt;login&gt;foo&lt;/login&gt;
        &lt;password&gt;bar&lt;/password&gt;
      &lt;/user_login_data&gt;
      &lt;customer_identifier&gt;
        &lt;customer_id&gt;6677070&lt;/customer_id&gt;
      &lt;/customer_identifier&gt;
      &lt;login/&gt; &lt;!--corresponds to the empty string in the call to CustomerLoginInfoSelectAsync--&gt;
    &lt;/customer_login_info_select_request&gt;
  ...
  </code></pre>
  
  <p>So at this point, I'm completely lost. Any insights would be greatly appreciated. If there's any other info I can provide, please let me know.</p>