public void ReadWriteTest ()
		{
			string directory = Path.Combine (Path.GetTempPath (), Path.GetRandomFileName ());
			Directory.CreateDirectory (directory);
			try {
				string url = "http://www.w3schools.com/WebServices/TempConvert.asmx";
				var p1 = new DiscoveryClientProtocol ();
				p1.DiscoverAny (url);
				p1.ResolveAll ();

				p1.WriteAll (directory, "Reference.map");

				var p2 = new DiscoveryClientProtocol ();
				var results = p2.ReadAll (Path.Combine (directory, "Reference.map"));

				Assert.AreEqual (2, results.Count);
				Assert.AreEqual ("TempConvert.disco", results [0].Filename);
				Assert.AreEqual ("TempConvert.wsdl", results [1].Filename);
			} finally {
				Directory.Delete (directory, true);
			}
		}
		ReferenceGroup ConvertMapFile (string mapFile)
		{
			DiscoveryClientResultCollection files;
			using (var prot = new DiscoveryClientProtocol ())
				files = prot.ReadAll (mapFile);
			
			var map = new ReferenceGroup ();
			
			if (refGroup != null) {
				map.ClientOptions = refGroup.ClientOptions;
				map.ID = refGroup.ID;
			} else {
				map.ClientOptions = defaultOptions;
				map.ID = Guid.NewGuid ().ToString ();
			}
			
			var sources = new Dictionary<string, int> ();
			foreach (DiscoveryClientResult res in files) {
				string url = res.Url;
				var uri = new Uri (url);
				if (!string.IsNullOrEmpty (uri.Query))
					url = url.Substring (0, url.Length - uri.Query.Length);
				int nSource;
				if (!sources.TryGetValue (url, out nSource)) {
					nSource = sources.Count + 1;
					sources [url] = nSource;
					var ms = new MetadataSource ();
					ms.Address = url;
					ms.Protocol = uri.Scheme;
					ms.SourceId = nSource.ToString ();
					map.MetadataSources.Add (ms);
				}
				var file = new MetadataFile ();
				file.FileName = res.Filename;
				file.ID = Guid.NewGuid ().ToString ();
				file.SourceId = nSource.ToString ();
				file.SourceUrl = res.Url;
				switch (Path.GetExtension (file.FileName).ToLower ()) {
					case ".disco": file.MetadataType = "Disco"; break;
					case ".wsdl": file.MetadataType = "Wsdl"; break;
					case ".xsd": file.MetadataType = "Schema"; break;
				}
				map.Metadata.Add (file);
			}
			map.Save (mapFile);
			return map;
		}
    public override void GenerateCode(AssemblyBuilder assemblyBuilder)  {

        // Only attempt to get the Indigo provider once
        if (!s_triedToGetWebRefType) {
            s_indigoWebRefProviderType = BuildManager.GetType(IndigoWebRefProviderTypeName, false /*throwOnError*/);
            s_triedToGetWebRefType = true;
        }

        // If we have an Indigo provider, instantiate it and forward the GenerateCode call to it
        if (s_indigoWebRefProviderType != null) {
            BuildProvider buildProvider = (BuildProvider)HttpRuntime.CreateNonPublicInstance(s_indigoWebRefProviderType);
            buildProvider.SetVirtualPath(VirtualPathObject);
            buildProvider.GenerateCode(assemblyBuilder);
        }

        // e.g "/MyApp/Application_WebReferences"
        VirtualPath rootWebRefDirVirtualPath = HttpRuntime.WebRefDirectoryVirtualPath;

        // e.g "/MyApp/Application_WebReferences/Foo/Bar"
        string currentWebRefDirVirtualPath = _vdir.VirtualPath;

        Debug.Assert(StringUtil.StringStartsWithIgnoreCase(
            currentWebRefDirVirtualPath, rootWebRefDirVirtualPath.VirtualPathString));

        string ns;

        if (rootWebRefDirVirtualPath.VirtualPathString.Length == currentWebRefDirVirtualPath.Length) {
            // If it's the root WebReferences dir, use the empty namespace
            ns = String.Empty;
        }
        else {
            // e.g. "Foo/Bar"
            Debug.Assert(rootWebRefDirVirtualPath.HasTrailingSlash);
            currentWebRefDirVirtualPath = UrlPath.RemoveSlashFromPathIfNeeded(currentWebRefDirVirtualPath);
            currentWebRefDirVirtualPath = currentWebRefDirVirtualPath.Substring(
                rootWebRefDirVirtualPath.VirtualPathString.Length);

            // Split it into chunks separated by '/'
            string[] chunks = currentWebRefDirVirtualPath.Split('/');

            // Turn all the relevant chunks into valid namespace chunks
            for (int i=0; i<chunks.Length; i++) {
                chunks[i] = Util.MakeValidTypeNameFromString(chunks[i]);
            }

            // Put the relevant chunks back together to form the namespace
            ns = String.Join(".", chunks);
        }
#if !FEATURE_PAL // FEATURE_PAL does not support System.Web.Services

        CodeNamespace codeNamespace = new CodeNamespace(ns);

        // for each discomap file, read all references and add them to the WebReferenceCollection
        WebReferenceCollection webs = new WebReferenceCollection();

        bool hasDiscomap = false;

        // Go through all the discomap in the directory
        foreach (VirtualFile child in _vdir.Files) {

            string extension = UrlPath.GetExtension(child.VirtualPath);
            extension = extension.ToLower(CultureInfo.InvariantCulture);

            if (extension == ".discomap") {
                // NOTE: the WebReferences code requires physical path, so this feature
                // cannot work with a non-file based VirtualPathProvider
                string physicalPath = HostingEnvironment.MapPath(child.VirtualPath);

                DiscoveryClientProtocol client = new DiscoveryClientProtocol();
                client.AllowAutoRedirect = true;
                client.Credentials = CredentialCache.DefaultCredentials;

                client.ReadAll(physicalPath);

                WebReference webRefTemp = new WebReference(client.Documents, codeNamespace);

                // 

                string fileName = System.IO.Path.ChangeExtension(UrlPath.GetFileName(child.VirtualPath), null);
                string appSetttingUrlKey = ns + "." + fileName;

                WebReference web = new WebReference(client.Documents, codeNamespace, webRefTemp.ProtocolName, appSetttingUrlKey, null);

                webs.Add(web);

                hasDiscomap = true;
            }
        }

        // If we didn't find any discomap files, we have nothing to generate
        if (!hasDiscomap)
            return;

        CodeCompileUnit codeCompileUnit = new CodeCompileUnit();
        codeCompileUnit.Namespaces.Add(codeNamespace);

        //public static StringCollection GenerateWebReferences(WebReferenceCollection webReferences, CodeDomProvider codeProvider, CodeCompileUnit codeCompileUnit, WebReferenceOptions options) {
        WebReferenceOptions options = new WebReferenceOptions();
        options.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties | CodeGenerationOptions.GenerateNewAsync | CodeGenerationOptions.GenerateOldAsync;
        options.Style = ServiceDescriptionImportStyle.Client;
        options.Verbose = true;
        StringCollection shareWarnings = ServiceDescriptionImporter.GenerateWebReferences(webs, assemblyBuilder.CodeDomProvider, codeCompileUnit, options);
        // Add the CodeCompileUnit to the compilation
        assemblyBuilder.AddCodeCompileUnit(this, codeCompileUnit);
#else // !FEATURE_PAL
        return;
#endif // !FEATURE_PAL
    }
		private MetadataSet DiscoverMetadataFromMapFile(string file)
		{
			string mapFile = Path.ChangeExtension(file, MapFileExtension);

			using (DiscoveryClientProtocol discovery = new DiscoveryClientProtocol())
			{
				discovery.ReadAll(mapFile);
				MetadataSet metadataSet = new MetadataSet();
				foreach (object document in discovery.Documents.Values)
				{
					metadataSet.MetadataSections.Add(CreateMetadataSection(document));
				}
				return metadataSet;
			}
		}
        /// <summary>
        /// Process local paths
        /// </summary>
        /// <param name="path"></param>
        /// <param name="discoveryClientProtocol"></param>
        private void ProcessLocal( string path, DiscoveryClientProtocol discoveryClientProtocol)
        {
            string extension = Path.GetExtension(path);

            if (string.Compare(extension, ".discomap", true) == 0)
            {
                discoveryClientProtocol.ReadAll(path);
            }
            else
            {
                object obj = null;
                if (string.Compare(extension, ".wsdl", true) == 0)
                {
                    obj = this.ReadLocalDocument(false, path);
                }
                else
                {
                    if (string.Compare(extension, ".xsd", true) != 0)
                        throw new InvalidOperationException("Unknown file type " + path);

                    obj = this.ReadLocalDocument(true, path);
                }

                if (obj != null)
                    this.AddDocument(path, obj);

            }
        }
Exemple #6
0
 private void ProcessLocalPaths(DiscoveryClientProtocol client, StringCollection localPaths, XmlSchemas schemas, ServiceDescriptionCollection descriptions)
 {
     StringEnumerator enumerator = localPaths.GetEnumerator();
     while (enumerator.MoveNext())
     {
         string current = enumerator.Current;
         string extension = Path.GetExtension(current);
         if (string.Compare(extension, ".discomap", true) == 0)
         {
             client.ReadAll(current);
         }
         else
         {
             object document = null;
             if (string.Compare(extension, ".wsdl", true) == 0)
             {
                 document = this.ReadLocalDocument(false, current);
             }
             else
             {
                 if (string.Compare(extension, ".xsd", true) != 0)
                 {
                     throw new InvalidOperationException("Unknown file type " + current);
                 }
                 document = this.ReadLocalDocument(true, current);
             }
             if (document != null)
             {
                 this.AddDocument(current, document, schemas, descriptions);
             }
         }
     }
 }
 private void ProcessLocalPaths(DiscoveryClientProtocol client, StringCollection localPaths, XmlSchemas schemas,
                                ServiceDescriptionCollection descriptions)
 {
     foreach (string text1 in localPaths)
     {
         string text2 = Path.GetExtension(text1);
         if (string.Compare(text2, ".discomap", true) == 0)
         {
             client.ReadAll(text1);
         }
         else
         {
             object obj1 = null;
             if (string.Compare(text2, ".wsdl", true) == 0)
             {
                 obj1 = ReadLocalDocument(false, text1);
             }
             else
             {
                 if (string.Compare(text2, ".xsd", true) != 0)
                 {
                     throw new InvalidOperationException("Unknown file type " + text1);
                 }
                 obj1 = ReadLocalDocument(true, text1);
             }
             if (obj1 != null)
             {
                 AddDocument(text1, obj1, schemas, descriptions);
             }
         }
     }
 }
 public override void GenerateCode(AssemblyBuilder assemblyBuilder)
 {
     string str2;
     if (!s_triedToGetWebRefType)
     {
         s_indigoWebRefProviderType = BuildManager.GetType("System.Web.Compilation.WCFBuildProvider", false);
         s_triedToGetWebRefType = true;
     }
     if (s_indigoWebRefProviderType != null)
     {
         BuildProvider provider = (BuildProvider) HttpRuntime.CreateNonPublicInstance(s_indigoWebRefProviderType);
         provider.SetVirtualPath(base.VirtualPathObject);
         provider.GenerateCode(assemblyBuilder);
     }
     VirtualPath webRefDirectoryVirtualPath = HttpRuntime.WebRefDirectoryVirtualPath;
     string virtualPath = this._vdir.VirtualPath;
     if (webRefDirectoryVirtualPath.VirtualPathString.Length == virtualPath.Length)
     {
         str2 = string.Empty;
     }
     else
     {
         string[] strArray = UrlPath.RemoveSlashFromPathIfNeeded(virtualPath).Substring(webRefDirectoryVirtualPath.VirtualPathString.Length).Split(new char[] { '/' });
         for (int i = 0; i < strArray.Length; i++)
         {
             strArray[i] = Util.MakeValidTypeNameFromString(strArray[i]);
         }
         str2 = string.Join(".", strArray);
     }
     CodeNamespace proxyCode = new CodeNamespace(str2);
     WebReferenceCollection webReferences = new WebReferenceCollection();
     bool flag = false;
     foreach (VirtualFile file in this._vdir.Files)
     {
         if (UrlPath.GetExtension(file.VirtualPath).ToLower(CultureInfo.InvariantCulture) == ".discomap")
         {
             string topLevelFilename = HostingEnvironment.MapPath(file.VirtualPath);
             DiscoveryClientProtocol protocol = new DiscoveryClientProtocol {
                 AllowAutoRedirect = true,
                 Credentials = CredentialCache.DefaultCredentials
             };
             protocol.ReadAll(topLevelFilename);
             WebReference reference = new WebReference(protocol.Documents, proxyCode);
             string str5 = Path.ChangeExtension(UrlPath.GetFileName(file.VirtualPath), null);
             string appSettingUrlKey = str2 + "." + str5;
             WebReference webReference = new WebReference(protocol.Documents, proxyCode, reference.ProtocolName, appSettingUrlKey, null);
             webReferences.Add(webReference);
             flag = true;
         }
     }
     if (flag)
     {
         CodeCompileUnit codeCompileUnit = new CodeCompileUnit();
         codeCompileUnit.Namespaces.Add(proxyCode);
         WebReferenceOptions options = new WebReferenceOptions {
             CodeGenerationOptions = CodeGenerationOptions.GenerateOldAsync | CodeGenerationOptions.GenerateNewAsync | CodeGenerationOptions.GenerateProperties,
             Style = ServiceDescriptionImportStyle.Client,
             Verbose = true
         };
         ServiceDescriptionImporter.GenerateWebReferences(webReferences, assemblyBuilder.CodeDomProvider, codeCompileUnit, options);
         assemblyBuilder.AddCodeCompileUnit(this, codeCompileUnit);
     }
 }