title: Importing ASMX Web Service metadata to WCF Endpoint
body: <p>I am interested in impersonating well-known Web Services and Wcf Services for integration test purposes. To this end, I would like to capture service metadata, auto-generate service stubs, and host service stubs in a self-hosted environment.</p>
  
  <p>Following <a href="http://msdn.microsoft.com/en-us/library/ms733780.aspx" rel="nofollow noreferrer">this article here</a>, I am able to obtain remote Wcf Service metadata and generate contracts. However, I am having some difficulty doing the same for remote Asmx Web Services.</p>
  
  <p>I have a set of mickey-mouse solutions for vetting this out.</p>
  
  <p>My Asmx solution contains a default "Hello World" web service, found below </p>
  
  <pre><code>[WebService(Namespace = "http://tempuri.org/")]
  [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  [System.ComponentModel.ToolboxItem(false)]
  public class SimpleAsmxService : System.Web.Services.WebService
  {
      [WebMethod]
      public string HelloWorld () { return "Hello World"; }
  }
  </code></pre>
  
  <p>My Wcf solution contains a default "Hello World" service, also found below</p>
  
  <pre><code>[ServiceContract]
  public interface ISimpleWcfService
  {
      [OperationContract]
      string GetData(int value);
  
      [OperationContract]
      CompositeType GetDataUsingDataContract(CompositeType composite);
  }
  
  [DataContract]
  public class CompositeType
  {
      [DataMember]
      public bool BoolValue { get; set; }
  
      [DataMember]
      public string StringValue { get; set; }
  }
  
  public class SimpleWcfService : ISimpleWcfService
  {
      public string GetData(int value)
      {
          return string.Format("You entered: {0}", value);
      }
  
      public CompositeType GetDataUsingDataContract(CompositeType composite)
      {
          if (composite.BoolValue)
          {
              composite.StringValue += "Suffix";
          }
          return composite;
      }
  }
  </code></pre>
  
  <p>Finally, the little console-that-could looks like</p>
  
  <pre><code>class Program
  {
      public const string UrlWcf = 
          "http://localhost:8731/Design_Time_Addresses/SimpleWcfService/mex";
      public const string UrlAsmx = 
          "http://localhost:1803/SimpleAsmxService.asmx?WSDL";
  
      static void Main(string[] args)
      {
          EndpointAddress mexAddress = new EndpointAddress (UrlWcf);
          MetadataExchangeClient mexClient = 
              new MetadataExchangeClient (mexAddress);
          mexClient.ResolveMetadataReferences = true;
  
          // NOTE: blows up if we use UrlAsmx
          MetadataSet metaSet = mexClient.GetMetadata ();
  
          WsdlImporter importer = new WsdlImporter (metaSet);
          Collection&lt;ContractDescription&gt; contracts = 
              importer.ImportAllContracts();
      }
  }
  </code></pre>
  
  <p>It seems to me that I should be able to pull Wsdl from a well-known Asmx Web Service and generate contracts [and from contracts to code], but cannot seem to contort the preceding sample to do so. Any help would be much appreciated,</p>
  
  <p>Thanks!</p>
  
  <hr>
  
  <p><strong>NOTE:</strong> the error generated when invoking <code>MetadataSet metaSet = mexClient.GetMetadata();</code> above is a <code>System.InvalidOperationException</code> with message of </p>
  
  <blockquote>
    <p>Metadata contains a reference that cannot be resolved : '<a href="http://localhost:1803/SimpleAsmxService.asmx?WSDL" rel="nofollow noreferrer">http://localhost:1803/SimpleAsmxService.asmx?WSDL</a>'</p>
  </blockquote>
  
  <p>With a <code>System.InvalidOperationException</code> inner exception with message of</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-16"?&gt;
  &lt;Fault xmlns="http://www.w3.org/2003/05/soap-envelope"&gt;
      &lt;Code&gt;
          &lt;Value&gt;Sender&lt;/Value&gt;
      &lt;/Code&gt;
      &lt;Reason&gt;
          &lt;Text xml:lang="en"&gt;
  System.Web.Services.Protocols.SoapException: Unable to handle request without a valid action parameter. Please supply a valid soap action.
     at System.Web.Services.Protocols.Soap12ServerProtocolHelper.RouteRequest()
     at System.Web.Services.Protocols.SoapServerProtocol.RouteRequest(SoapServerMessage message)
     at System.Web.Services.Protocols.SoapServerProtocol.Initialize()
     at System.Web.Services.Protocols.ServerProtocol.SetContext(Type type, HttpContext context, HttpRequest request, HttpResponse response)
     at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean&amp;amp; abortProcessing)
          &lt;/Text&gt;
      &lt;/Reason&gt;
  &lt;/Fault&gt;
  </code></pre>