title: WSDL with linked xsds are not compiled dynamically using c#
body: <p>I have a following wsdl from a WCF service.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;wsdl:definitions name="Service" targetNamespace="http://tempuri.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:tns="http://tempuri.org/" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:wsa10="http://www.w3.org/2005/08/addressing" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex"&gt;
    &lt;wsp:Policy wsu:Id="WSHttpBinding_IService_policy"&gt;
      &lt;wsp:ExactlyOne&gt;
        &lt;wsp:All&gt;
          &lt;sp:SymmetricBinding xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy"&gt;
            &lt;wsp:Policy&gt;
              &lt;sp:ProtectionToken&gt;
                &lt;wsp:Policy&gt;
                  &lt;sp:SecureConversationToken sp:IncludeToken="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy/IncludeToken/AlwaysToRecipient"&gt;
                    &lt;wsp:Policy&gt;
                      &lt;sp:RequireDerivedKeys /&gt;
                      &lt;sp:BootstrapPolicy&gt;
                        &lt;wsp:Policy&gt;
                          &lt;sp:SignedParts&gt;
                            &lt;sp:Body /&gt;
                            &lt;sp:Header Name="To" Namespace="http://www.w3.org/2005/08/addressing" /&gt;
                            &lt;sp:Header Name="From" Namespace="http://www.w3.org/2005/08/addressing" /&gt;
                            &lt;sp:Header Name="FaultTo" Namespace="http://www.w3.org/2005/08/addressing" /&gt;
                            &lt;sp:Header Name="ReplyTo" Namespace="http://www.w3.org/2005/08/addressing" /&gt;
                            &lt;sp:Header Name="MessageID" Namespace="http://www.w3.org/2005/08/addressing" /&gt;
                            &lt;sp:Header Name="RelatesTo" Namespace="http://www.w3.org/2005/08/addressing" /&gt;
                            &lt;sp:Header Name="Action" Namespace="http://www.w3.org/2005/08/addressing" /&gt;
                          &lt;/sp:SignedParts&gt;
                          &lt;sp:EncryptedParts&gt;
                            &lt;sp:Body /&gt;
                          &lt;/sp:EncryptedParts&gt;
                          &lt;sp:SymmetricBinding&gt;
                            &lt;wsp:Policy&gt;
                              &lt;sp:ProtectionToken&gt;
                                &lt;wsp:Policy&gt;
                                  &lt;sp:SpnegoContextToken sp:IncludeToken="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy/IncludeToken/AlwaysToRecipient"&gt;
                                    &lt;wsp:Policy&gt;
                                      &lt;sp:RequireDerivedKeys /&gt;
                                    &lt;/wsp:Policy&gt;
                                  &lt;/sp:SpnegoContextToken&gt;
                                &lt;/wsp:Policy&gt;
                              &lt;/sp:ProtectionToken&gt;
                              &lt;sp:AlgorithmSuite&gt;
                                &lt;wsp:Policy&gt;
                                  &lt;sp:Basic256 /&gt;
                                &lt;/wsp:Policy&gt;
                              &lt;/sp:AlgorithmSuite&gt;
                              &lt;sp:Layout&gt;
                                &lt;wsp:Policy&gt;
                                  &lt;sp:Strict /&gt;
                                &lt;/wsp:Policy&gt;
                              &lt;/sp:Layout&gt;
                              &lt;sp:IncludeTimestamp /&gt;
                              &lt;sp:EncryptSignature /&gt;
                              &lt;sp:OnlySignEntireHeadersAndBody /&gt;
                            &lt;/wsp:Policy&gt;
                          &lt;/sp:SymmetricBinding&gt;
                          &lt;sp:Wss11&gt;
                            &lt;wsp:Policy&gt;
                              &lt;sp:MustSupportRefKeyIdentifier /&gt;
                              &lt;sp:MustSupportRefIssuerSerial /&gt;
                              &lt;sp:MustSupportRefThumbprint /&gt;
                              &lt;sp:MustSupportRefEncryptedKey /&gt;
                            &lt;/wsp:Policy&gt;
                          &lt;/sp:Wss11&gt;
                          &lt;sp:Trust10&gt;
                            &lt;wsp:Policy&gt;
                              &lt;sp:MustSupportIssuedTokens /&gt;
                              &lt;sp:RequireClientEntropy /&gt;
                              &lt;sp:RequireServerEntropy /&gt;
                            &lt;/wsp:Policy&gt;
                          &lt;/sp:Trust10&gt;
                        &lt;/wsp:Policy&gt;
                      &lt;/sp:BootstrapPolicy&gt;
                    &lt;/wsp:Policy&gt;
                  &lt;/sp:SecureConversationToken&gt;
                &lt;/wsp:Policy&gt;
              &lt;/sp:ProtectionToken&gt;
              &lt;sp:AlgorithmSuite&gt;
                &lt;wsp:Policy&gt;
                  &lt;sp:Basic256 /&gt;
                &lt;/wsp:Policy&gt;
              &lt;/sp:AlgorithmSuite&gt;
              &lt;sp:Layout&gt;
                &lt;wsp:Policy&gt;
                  &lt;sp:Strict /&gt;
                &lt;/wsp:Policy&gt;
              &lt;/sp:Layout&gt;
              &lt;sp:IncludeTimestamp /&gt;
              &lt;sp:EncryptSignature /&gt;
              &lt;sp:OnlySignEntireHeadersAndBody /&gt;
            &lt;/wsp:Policy&gt;
          &lt;/sp:SymmetricBinding&gt;
          &lt;sp:Wss11 xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy"&gt;
            &lt;wsp:Policy&gt;
              &lt;sp:MustSupportRefKeyIdentifier /&gt;
              &lt;sp:MustSupportRefIssuerSerial /&gt;
              &lt;sp:MustSupportRefThumbprint /&gt;
              &lt;sp:MustSupportRefEncryptedKey /&gt;
            &lt;/wsp:Policy&gt;
          &lt;/sp:Wss11&gt;
          &lt;sp:Trust10 xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy"&gt;
            &lt;wsp:Policy&gt;
              &lt;sp:MustSupportIssuedTokens /&gt;
              &lt;sp:RequireClientEntropy /&gt;
              &lt;sp:RequireServerEntropy /&gt;
            &lt;/wsp:Policy&gt;
          &lt;/sp:Trust10&gt;
          &lt;wsaw:UsingAddressing /&gt;
        &lt;/wsp:All&gt;
      &lt;/wsp:ExactlyOne&gt;
    &lt;/wsp:Policy&gt;
    &lt;wsp:Policy wsu:Id="WSHttpBinding_IService_GetSimpleType_Input_policy"&gt;
      &lt;wsp:ExactlyOne&gt;
        &lt;wsp:All&gt;
          &lt;sp:SignedParts xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy"&gt;
            &lt;sp:Body /&gt;
            &lt;sp:Header Name="To" Namespace="http://www.w3.org/2005/08/addressing" /&gt;
            &lt;sp:Header Name="From" Namespace="http://www.w3.org/2005/08/addressing" /&gt;
            &lt;sp:Header Name="FaultTo" Namespace="http://www.w3.org/2005/08/addressing" /&gt;
            &lt;sp:Header Name="ReplyTo" Namespace="http://www.w3.org/2005/08/addressing" /&gt;
            &lt;sp:Header Name="MessageID" Namespace="http://www.w3.org/2005/08/addressing" /&gt;
            &lt;sp:Header Name="RelatesTo" Namespace="http://www.w3.org/2005/08/addressing" /&gt;
            &lt;sp:Header Name="Action" Namespace="http://www.w3.org/2005/08/addressing" /&gt;
          &lt;/sp:SignedParts&gt;
          &lt;sp:EncryptedParts xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy"&gt;
            &lt;sp:Body /&gt;
          &lt;/sp:EncryptedParts&gt;
        &lt;/wsp:All&gt;
      &lt;/wsp:ExactlyOne&gt;
    &lt;/wsp:Policy&gt;
    &lt;wsp:Policy wsu:Id="WSHttpBinding_IService_GetSimpleType_output_policy"&gt;
      &lt;wsp:ExactlyOne&gt;
        &lt;wsp:All&gt;
          &lt;sp:SignedParts xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy"&gt;
            &lt;sp:Body /&gt;
            &lt;sp:Header Name="To" Namespace="http://www.w3.org/2005/08/addressing" /&gt;
            &lt;sp:Header Name="From" Namespace="http://www.w3.org/2005/08/addressing" /&gt;
            &lt;sp:Header Name="FaultTo" Namespace="http://www.w3.org/2005/08/addressing" /&gt;
            &lt;sp:Header Name="ReplyTo" Namespace="http://www.w3.org/2005/08/addressing" /&gt;
            &lt;sp:Header Name="MessageID" Namespace="http://www.w3.org/2005/08/addressing" /&gt;
            &lt;sp:Header Name="RelatesTo" Namespace="http://www.w3.org/2005/08/addressing" /&gt;
            &lt;sp:Header Name="Action" Namespace="http://www.w3.org/2005/08/addressing" /&gt;
          &lt;/sp:SignedParts&gt;
          &lt;sp:EncryptedParts xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy"&gt;
            &lt;sp:Body /&gt;
          &lt;/sp:EncryptedParts&gt;
        &lt;/wsp:All&gt;
      &lt;/wsp:ExactlyOne&gt;
    &lt;/wsp:Policy&gt;
    &lt;wsp:Policy wsu:Id="WSHttpBinding_IService_GetCashFlowData_Input_policy"&gt;
      &lt;wsp:ExactlyOne&gt;
        &lt;wsp:All&gt;
          &lt;sp:SignedParts xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy"&gt;
            &lt;sp:Body /&gt;
            &lt;sp:Header Name="To" Namespace="http://www.w3.org/2005/08/addressing" /&gt;
            &lt;sp:Header Name="From" Namespace="http://www.w3.org/2005/08/addressing" /&gt;
            &lt;sp:Header Name="FaultTo" Namespace="http://www.w3.org/2005/08/addressing" /&gt;
            &lt;sp:Header Name="ReplyTo" Namespace="http://www.w3.org/2005/08/addressing" /&gt;
            &lt;sp:Header Name="MessageID" Namespace="http://www.w3.org/2005/08/addressing" /&gt;
            &lt;sp:Header Name="RelatesTo" Namespace="http://www.w3.org/2005/08/addressing" /&gt;
            &lt;sp:Header Name="Action" Namespace="http://www.w3.org/2005/08/addressing" /&gt;
          &lt;/sp:SignedParts&gt;
          &lt;sp:EncryptedParts xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy"&gt;
            &lt;sp:Body /&gt;
          &lt;/sp:EncryptedParts&gt;
        &lt;/wsp:All&gt;
      &lt;/wsp:ExactlyOne&gt;
    &lt;/wsp:Policy&gt;
    &lt;wsp:Policy wsu:Id="WSHttpBinding_IService_GetCashFlowData_output_policy"&gt;
      &lt;wsp:ExactlyOne&gt;
        &lt;wsp:All&gt;
          &lt;sp:SignedParts xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy"&gt;
            &lt;sp:Body /&gt;
            &lt;sp:Header Name="To" Namespace="http://www.w3.org/2005/08/addressing" /&gt;
            &lt;sp:Header Name="From" Namespace="http://www.w3.org/2005/08/addressing" /&gt;
            &lt;sp:Header Name="FaultTo" Namespace="http://www.w3.org/2005/08/addressing" /&gt;
            &lt;sp:Header Name="ReplyTo" Namespace="http://www.w3.org/2005/08/addressing" /&gt;
            &lt;sp:Header Name="MessageID" Namespace="http://www.w3.org/2005/08/addressing" /&gt;
            &lt;sp:Header Name="RelatesTo" Namespace="http://www.w3.org/2005/08/addressing" /&gt;
            &lt;sp:Header Name="Action" Namespace="http://www.w3.org/2005/08/addressing" /&gt;
          &lt;/sp:SignedParts&gt;
          &lt;sp:EncryptedParts xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy"&gt;
            &lt;sp:Body /&gt;
          &lt;/sp:EncryptedParts&gt;
        &lt;/wsp:All&gt;
      &lt;/wsp:ExactlyOne&gt;
    &lt;/wsp:Policy&gt;
    &lt;wsdl:types&gt;
      &lt;xsd:schema targetNamespace="http://tempuri.org/Imports"&gt;
        &lt;xsd:import schemaLocation="http://mrecserver.nj.win.com//PIData/Service.svc?xsd=xsd0" namespace="http://tempuri.org/" /&gt;
        &lt;xsd:import schemaLocation="http://mrecserver.nj.win.com//PIData/Service.svc?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/" /&gt;
        &lt;xsd:import schemaLocation="http://mrecserver.nj.win.com//PIData/Service.svc?xsd=xsd2" namespace="http://schemas.datacontract.org/2004/07/" /&gt;
      &lt;/xsd:schema&gt;
    &lt;/wsdl:types&gt;
    &lt;wsdl:message name="IService_GetSimpleType_InputMessage"&gt;
      &lt;wsdl:part name="parameters" element="tns:GetSimpleType" /&gt;
    &lt;/wsdl:message&gt;
    &lt;wsdl:message name="IService_GetSimpleType_OutputMessage"&gt;
      &lt;wsdl:part name="parameters" element="tns:GetSimpleTypeResponse" /&gt;
    &lt;/wsdl:message&gt;
    &lt;wsdl:message name="IService_GetCashFlowData_InputMessage"&gt;
      &lt;wsdl:part name="parameters" element="tns:GetCashFlowData" /&gt;
    &lt;/wsdl:message&gt;
    &lt;wsdl:message name="IService_GetCashFlowData_OutputMessage"&gt;
      &lt;wsdl:part name="parameters" element="tns:GetCashFlowDataResponse" /&gt;
    &lt;/wsdl:message&gt;
    &lt;wsdl:portType name="IService"&gt;
      &lt;wsdl:operation name="GetSimpleType"&gt;
        &lt;wsdl:input wsaw:Action="http://tempuri.org/IService/GetSimpleType" message="tns:IService_GetSimpleType_InputMessage" /&gt;
        &lt;wsdl:output wsaw:Action="http://tempuri.org/IService/GetSimpleTypeResponse" message="tns:IService_GetSimpleType_OutputMessage" /&gt;
      &lt;/wsdl:operation&gt;
      &lt;wsdl:operation name="GetCashFlowData"&gt;
        &lt;wsdl:input wsaw:Action="http://tempuri.org/IService/GetCashFlowData" message="tns:IService_GetCashFlowData_InputMessage" /&gt;
        &lt;wsdl:output wsaw:Action="http://tempuri.org/IService/GetCashFlowDataResponse" message="tns:IService_GetCashFlowData_OutputMessage" /&gt;
      &lt;/wsdl:operation&gt;
    &lt;/wsdl:portType&gt;
    &lt;wsdl:binding name="WSHttpBinding_IService" type="tns:IService"&gt;
      &lt;wsp:PolicyReference URI="#WSHttpBinding_IService_policy" /&gt;
      &lt;soap12:binding transport="http://schemas.xmlsoap.org/soap/http" /&gt;
      &lt;wsdl:operation name="GetSimpleType"&gt;
        &lt;soap12:operation soapAction="http://tempuri.org/IService/GetSimpleType" style="document" /&gt;
        &lt;wsdl:input&gt;
          &lt;wsp:PolicyReference URI="#WSHttpBinding_IService_GetSimpleType_Input_policy" /&gt;
          &lt;soap12:body use="literal" /&gt;
        &lt;/wsdl:input&gt;
        &lt;wsdl:output&gt;
          &lt;wsp:PolicyReference URI="#WSHttpBinding_IService_GetSimpleType_output_policy" /&gt;
          &lt;soap12:body use="literal" /&gt;
        &lt;/wsdl:output&gt;
      &lt;/wsdl:operation&gt;
      &lt;wsdl:operation name="GetCashFlowData"&gt;
        &lt;soap12:operation soapAction="http://tempuri.org/IService/GetCashFlowData" style="document" /&gt;
        &lt;wsdl:input&gt;
          &lt;wsp:PolicyReference URI="#WSHttpBinding_IService_GetCashFlowData_Input_policy" /&gt;
          &lt;soap12:body use="literal" /&gt;
        &lt;/wsdl:input&gt;
        &lt;wsdl:output&gt;
          &lt;wsp:PolicyReference URI="#WSHttpBinding_IService_GetCashFlowData_output_policy" /&gt;
          &lt;soap12:body use="literal" /&gt;
        &lt;/wsdl:output&gt;
      &lt;/wsdl:operation&gt;
    &lt;/wsdl:binding&gt;
    &lt;wsdl:service name="Service"&gt;
      &lt;wsdl:port name="WSHttpBinding_IService" binding="tns:WSHttpBinding_IService"&gt;
        &lt;soap12:address location="http://mrecserver.nj.win.com//PIData/Service.svc" /&gt;
        &lt;wsa10:EndpointReference&gt;
          &lt;wsa10:Address&gt;http://mrecserver.nj.win.com//PIData/Service.svc&lt;/wsa10:Address&gt;
          &lt;Identity xmlns="http://schemas.xmlsoap.org/ws/2006/02/addressingidentity"&gt;
            &lt;Dns&gt;localhost&lt;/Dns&gt;
          &lt;/Identity&gt;
        &lt;/wsa10:EndpointReference&gt;
      &lt;/wsdl:port&gt;
    &lt;/wsdl:service&gt;
  &lt;/wsdl:definitions&gt;
  </code></pre>
  
  <p>..and the following code to return a compiled assembly to use to save the methods. </p>
  
  <pre><code>/// &lt;summary&gt;
  /// Compiles an assembly from the proxy class provided by the ServiceDescriptionImporter.
  /// &lt;/summary&gt;
  /// &lt;param name="descriptionImporter"&gt;&lt;/param&gt;
  /// &lt;returns&gt;An assembly that can be used to execute the web service methods.&lt;/returns&gt;
  private Assembly CompileAssembly(ServiceDescriptionImporter descriptionImporter)
  {
      // a namespace and compile unit are needed by importer
      CodeNamespace codeNamespace = new CodeNamespace();
      CodeCompileUnit codeUnit = new CodeCompileUnit();
  
      codeUnit.Namespaces.Add(codeNamespace);
  
      ServiceDescriptionImportWarnings importWarnings = descriptionImporter.Import(codeNamespace, codeUnit);
  
      if (importWarnings == 0) // no warnings
      {
          // create a c# compiler
          CodeDomProvider compiler = CodeDomProvider.CreateProvider("CSharp");
  
          // include the assembly references needed to compile
          string[] references = new string[2] { "System.Web.Services.dll", "System.Xml.dll" };
  
          CompilerParameters parameters = new CompilerParameters(references);
  
          // compile into assembly
          CompilerResults results = compiler.CompileAssemblyFromDom(parameters, codeUnit);
  
          foreach (CompilerError oops in results.Errors)
          {
              // trap these errors and make them available to exception object
              throw new Exception("Compilation Error Creating Assembly");
          }
  
          // all done....
          return results.CompiledAssembly;
      }
      else
      {
          // warnings issued from importers, something wrong with WSDL
          throw new Exception("Invalid WSDL");
      }
  }
  </code></pre>
  
  <p>The wsdl passed always go to else part and throw an exception "Invalid WSDL". I noticed that the same piece of code works for those wsdl that doesn't have any linked xsd (i.e. xsd0, xsd1 or xsd2). Can anyone think what could be the problem? and, why does some wcf services have linked xsds and some don't?</p>
  
  <p>Here I am trying to read the wsdl to get all the methods and the properties. I know we can do this easily by adding service reference but I want to do this dynammically as user will pass the service url and I will generate the list of methods with properties.</p>