Esempio n. 1
0
        public static ComplusEndpointConfigContainer Get(string appIdOrName, bool rethrow)
        {
            ComAdminAppInfo appInfo = ComAdminWrapper.GetAppInfo(appIdOrName);

            if (appInfo == null)
            {
                return(null);
            }
            try
            {
                ComplusEndpointConfigContainer container = new ComplusEndpointConfigContainer(appInfo);
                return(container);
            }
            catch (Exception ex)
            {
                if (ex is NullReferenceException || ex is SEHException)
                {
                    throw;
                }

                if (rethrow)
                {
                    throw;
                }
                else
                {
                    ToolConsole.WriteWarning(SR.GetString(SR.FailedToLoadConfigForApplicationIgnoring, appInfo.Name, ex.Message));
                }
            }
            return(null);
        }
        static string GetPartitionIdForApplication(ICatalog2 catalog, string appId, bool notThrow)
        {
            string partitionId = null;

            try
            {
                partitionId = catalog.GetPartitionID(appId);
            }
            catch (COMException e)
            {
                if (!notThrow)
                {
                    throw Tool.CreateException(SR.GetString(SR.CouldNotGetPartition), e);
                }
                else if (e.ErrorCode == HR.COMADMIN_E_OBJECT_DOES_NOT_EXIST)
                {
                    ToolConsole.WriteWarning(SR.GetString(SR.ApplicationNotFound, appId));
                }
                else
                {
                    ToolConsole.WriteWarning(SR.GetString(SR.CouldnotGetPartitionForApplication, appId, e.ErrorCode, e.Message));
                }
                return(null);
            }
            return(partitionId);
        }
Esempio n. 3
0
        public static string[] GetWebServerNames()
        {
            if (!IsIISInstalled())
            {
                return(null);
            }
            try
            {
                List <string> webServerNames = new List <string>();

                DirectoryEntry webServiceEntry = new DirectoryEntry(webService);

                foreach (DirectoryEntry child in webServiceEntry.Children)
                {
                    if (child.SchemaClassName.ToUpperInvariant() == "IISWEBSERVER")
                    {
                        webServerNames.Add(webService + "/" + child.Name);      // Note, child.Name is a number!  the "friendly" name is actually child.Description
                    }
                }
                return(webServerNames.ToArray());
            }
            catch (COMException ex)
            {
                // assume a failure here means that no web servers exist
                ToolConsole.WriteWarning(SR.GetString(SR.CannotGetWebServersIgnoringWas,
                                                      ex.ErrorCode, ex.Message));
                return(null);
            }
        }
Esempio n. 4
0
        public override void AbortChanges()
        {
            this.closed = true;
            this.manifestFile.Abort();
            this.configFile.Abort();

            if (this.mustGenerateAppDir)
            {
                // Delete the directory if it exists
                if (Directory.Exists(this.appDir))
                {
                    Directory.Delete(this.appDir);
                }
            }
            if (scope != null)
            {
                try
                {
                    Transaction.Current.Rollback();
                    scope.Complete();
                    scope.Dispose();
                }
                catch (Exception ex)
                {
                    if (ex is NullReferenceException || ex is SEHException)
                    {
                        throw;
                    }
                    ToolConsole.WriteWarning(SR.GetString(SR.FailedToAbortTransactionWithError, ex.Message));
                }
            }
        }
        static void MarkComponentAsPrivate(ICatalog2 catalog, string partitionID, string applicationID, string progid)
        {
            ICatalogCollection partitions   = (ICatalogCollection)(catalog.GetCollection(CollectionName.Partitions));
            ICatalogCollection applications = (ICatalogCollection)(partitions.GetCollection(CollectionName.Applications, partitionID));

            applications.Populate();
            ICatalogCollection components = (ICatalogCollection)applications.GetCollection(CollectionName.Components, applicationID);

            try
            {
                components.Populate();
                for (int j = 0; j < components.Count(); j++)
                {
                    ICatalogObject component = (ICatalogObject)(components.Item(j));
                    if ((string)component.Name() == progid)
                    {
                        component.SetValue(PropertyName.IsPrivateComponent, true);
                        components.SaveChanges();
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is NullReferenceException || ex is SEHException)
                {
                    throw ex;
                }

                ToolConsole.WriteWarning(SR.GetString(SR.FailedToMarkListenerComponentAsPrivateForApplication, progid, applicationID));
            }
        }
        public static WasEndpointConfigContainer Get(string webServer, string webDirectory, string applicationIdOrName)
        {
            string          webDirectoryPath = null;
            RuntimeVersions runtimeVersion   = RuntimeVersions.V40;

            if (!string.IsNullOrEmpty(applicationIdOrName))
            {
                ComAdminAppInfo appInfo = ComAdminWrapper.GetAppInfo(applicationIdOrName);
                runtimeVersion = appInfo.RuntimeVersion;
            }
            if (WasAdminWrapper.GetWebDirectoryPath(webServer, webDirectory, out webDirectoryPath))
            {
                try
                {
                    return(new WasEndpointConfigContainer(webServer, webDirectory, webDirectoryPath, runtimeVersion));
                }
                catch (Exception ex)
                {
                    if (ex is NullReferenceException || ex is SEHException)
                    {
                        throw ex;
                    }
                    ToolConsole.WriteWarning(SR.GetString(SR.FailedToLoadConfigForWebDirectoryOnWebSite, webDirectory, webServer));
                }
                return(null);
            }
            else
            {
                return(null);
            }
        }
Esempio n. 7
0
        public override void PrepareChanges()
        {
            this.closed = true;
            bool workDone           = this.configFile.HasBeenModified() && WasModified;
            TransactionOptions opts = new TransactionOptions();

            opts.Timeout        = TimeSpan.FromMinutes(5);
            opts.IsolationLevel = IsolationLevel.Serializable;
            scope = new TransactionScope(TransactionScopeOption.Required, opts, EnterpriseServicesInteropOption.Full);

            if (workDone)
            {
                // if appDir doesnt exist, we must create it before we prepare the config files below
                if (this.mustGenerateAppDir)
                {
                    // create it
                    Directory.CreateDirectory(this.appDir);

                    ToolConsole.WriteLine(SR.GetString(SR.DirectoryCreated, this.appDir));
                }
                // set the COM+ app property
                ComAdminWrapper.SetAppDir(this.appInfo.ID.ToString("B"), this.appDir);
            }

            this.configFile.Prepare();

            if (workDone)
            {
                ToolConsole.WriteLine(SR.GetString((this.configFile.OriginalFileExists ? SR.FileUpdated : SR.FileCreated), configFile.OriginalFileName));
            }


            if (workDone && !this.manifestFile.CurrentExists() && this.hasServices)
            {
                string fileName = this.manifestFile.GetCurrentFileName(false);  // for update
                CreateManifestFile(this.manifestFile.GetCurrentFileName(false));
                ToolConsole.WriteLine(SR.GetString(SR.FileCreated, this.manifestFile.OriginalFileName));
            }

            this.manifestFile.Prepare();

            if (workDone)
            {
                // Now, install the Listener if it isnt already there
                if (this.hasServices && !this.listenerComponentExists)
                {
                    ComAdminWrapper.InstallListener(this.appInfo.ID, this.appDir, this.appInfo.RuntimeVersion);
                }
                else if (!this.hasServices && this.listenerComponentExists)
                {
                    ComAdminWrapper.RemoveListener(this.appInfo.ID);
                }

                if (this.appInfo.IsServerActivated)
                {
                    ToolConsole.WriteWarning(SR.GetString(SR.ShouldRestartApp, this.appInfo.Name));
                }
            }
        }
        public static void InstallListener(Guid appid, string path, RuntimeVersions runtimeVersion)
        {
            string application = appid.ToString("B");
            string partitionId = null;


            ICatalog2 catalog = GetCatalog();

            partitionId = GetPartitionIdForApplication(catalog, application, false);

            // Search for the listener in this partition..
            bool   is64bit  = GetApplicationBitness(catalog, partitionId, application);
            Guid   clsidVal = Guid.NewGuid();
            string clsid    = clsidVal.ToString("B");
            string tlb      = Path.Combine(path, application + "." + clsid + ".tlb");

            try
            {
                // No other listener in this partition, we're the first - install using RegistrationHelper
                AtomicFile.SafeDeleteFile(tlb);
                string modulePath = GetAppropriateBitnessModuleModulePath(is64bit, runtimeVersion);
                if (string.IsNullOrEmpty(modulePath))
                {
                    throw Tool.CreateException(SR.GetString(SR.CannotFindServiceInitializerModuleInRegistry), null);
                }
                CreateTypeLib(tlb, clsidVal);
                CreateRegistryKey(is64bit, clsidVal, modulePath);
                catalog.InstallComponent(application, modulePath, tlb, null);
                MarkComponentAsPrivate(catalog, partitionId, application, ListenerWSUName);
                if (!SetComponentProperty(application, ListenerWSUName, PropertyName.Description, ListenerComponentDescription))
                {
                    ToolConsole.WriteWarning(SR.GetString(SR.CannotSetComponentDescription, clsid, appid.ToString("B")));
                }
                if (!SetComponentProperty(application, ListenerWSUName, PropertyName.InitializesServerApplication, "1"))
                {
                    ToolConsole.WriteWarning(SR.GetString(SR.CannotSetComponentInitializerProperty, ListenerWSUName, appid.ToString("B")));
                }
                if (!SetComponentProperty(application, ListenerWSUName, PropertyName.ComponentAccessChecksEnabled, "0"))
                {
                    ToolConsole.WriteWarning(SR.GetString(SR.CannotDisableAccessChecksOnInitializer, ListenerWSUName, appid.ToString("B")));
                }
            }
            catch (Exception ex)
            {
                if (ex is NullReferenceException || ex is SEHException)
                {
                    throw ex;
                }

                throw Tool.CreateException(SR.GetString(SR.CouldNotInstallListener), ex);
            }
            finally
            {
                AtomicFile.SafeDeleteFile(tlb);
            }
        }
Esempio n. 9
0
        public static bool GetWebDirectoryPath(string webServer, string webDirectory, out string webDirectoryPath)
        {
            webDirectoryPath = null;

            if (!IsIISInstalled())
            {
                return(false);
            }

            if (!webDirectory.ToUpperInvariant().StartsWith("ROOT", StringComparison.Ordinal))
            {
                webDirectory = "root/" + webDirectory;
            }

            string[] webDirectories = GetWebDirectoryNames(webServer);
            if (webDirectories == null)
            {
                return(false);
            }
            bool found = false;

            foreach (string webDirectoryName in webDirectories)
            {
                if (webDirectoryName.ToUpperInvariant() == webDirectory.ToUpperInvariant())
                {
                    found = true;
                    break;
                }
            }
            if (!found)
            {
                return(false);
            }
            DirectoryEntry webDirectoryEntry = new DirectoryEntry(webServer + "/" + webDirectory);

            try
            {
                if (webDirectoryEntry.Properties.Contains("Path"))
                {
                    webDirectoryPath = (string)webDirectoryEntry.Properties["Path"].Value;
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch (COMException ex)
            {
                // assume a failure here means the dir does not exist
                ToolConsole.WriteWarning(SR.GetString(SR.CannotGetWebDirectoryPathOnWebDirOfWebServIgnoring,
                                                      webServer, webDirectory, ex.ErrorCode, ex.Message));
                return(false);
            }
        }
        public override void Remove(IList <EndpointConfig> endpointConfigs)
        {
            ThrowIfClosed();

            Configuration config = GetConfiguration(false); // not read only

            Debug.Assert(config != null, "config != null");

            bool anyRemoved = false;

            foreach (EndpointConfig endpointConfig in endpointConfigs)
            {
                bool removed = this.BaseRemoveEndpointConfig(config, endpointConfig);
                if (removed)
                {
                    anyRemoved = true;

                    if (NumEndpointsForClsid(config, endpointConfig.Clsid, endpointConfig.Appid) == 0)
                    {
                        // We can only Remove the SVC file when there are no more endpoints for the Clsid
                        this.svcFileManager.Remove(endpointConfig.Appid, endpointConfig.Clsid);
                    }
                    if (!Tool.Options.ShowGuids)
                    {
                        ToolConsole.WriteLine(SR.GetString(SR.InterfaceRemoved, endpointConfig.ComponentProgID, endpointConfig.InterfaceName));
                    }
                    else
                    {
                        ToolConsole.WriteLine(SR.GetString(SR.InterfaceRemoved, endpointConfig.Clsid, endpointConfig.Iid));
                    }
                }
                else if (!endpointConfig.IsMexEndpoint)
                {
                    if (!Tool.Options.ShowGuids)
                    {
                        ToolConsole.WriteWarning(SR.GetString(SR.InterfaceNotExposed, endpointConfig.ComponentProgID, endpointConfig.InterfaceName));
                    }
                    else
                    {
                        ToolConsole.WriteWarning(SR.GetString(SR.InterfaceNotExposed, endpointConfig.Clsid, endpointConfig.Iid));
                    }
                }
            }

            if (anyRemoved)
            {
                WasModified = true;
                config.Save();
            }
        }
        public override List <EndpointConfig> GetEndpointConfigs()
        {
            ThrowIfClosed();

            Configuration config = GetConfiguration(true); // readonly

            if (config == null)
            {
                // null config means there is no config to read, return an empty list
                return(new List <EndpointConfig>());
            }

            Dictionary <string, List <EndpointConfig> > endpointConfigs = BaseGetEndpointsFromConfiguration(config);
            List <EndpointConfig> list = new List <EndpointConfig>();

            // now, fix up the appid for all the endpoints
            foreach (List <EndpointConfig> endpoints in endpointConfigs.Values)
            {
                foreach (EndpointConfig endpoint in endpoints)
                {
                    Guid appid;

                    if (this.svcFileManager.ResolveClsid(endpoint.Clsid, out appid))
                    {
                        endpoint.Appid = appid;
                        list.Add(endpoint);
                    }
                    else
                    {
                        string appName       = endpoint.ApplicationName;
                        string componentID   = endpoint.ComponentProgID;
                        string interfaceName = endpoint.InterfaceName;

                        if (!Tool.Options.ShowGuids && !string.IsNullOrEmpty(appName) && !string.IsNullOrEmpty(componentID) && !string.IsNullOrEmpty(interfaceName))
                        {
                            ToolConsole.WriteWarning(SR.GetString(SR.EndpointNotFoundInSvcFile, appName, componentID, interfaceName, endpoint.BindingName, endpoint.Address));
                        }
                        else
                        {
                            ToolConsole.WriteWarning(SR.GetString(SR.EndpointNotFoundInSvcFile, endpoint.Appid.ToString("B"), endpoint.Clsid.ToString("B"), endpoint.Iid.ToString("B"), endpoint.BindingName, endpoint.Address));
                        }
                    }
                }
            }

            return(list);
        }
Esempio n. 12
0
        public override void Remove(IList <EndpointConfig> endpointConfigs)
        {
            ThrowIfClosed();

            Configuration config = GetConfiguration(false); // not read only

            Debug.Assert(config != null, "config != null");

            bool anyRemoved = false;

            foreach (EndpointConfig endpointConfig in endpointConfigs)
            {
                Debug.Assert(endpointConfig.Appid == this.appInfo.ID, "can't remove endpoint for a different application");

                bool removed = this.BaseRemoveEndpointConfig(config, endpointConfig);
                if (removed)
                {
                    anyRemoved = true;
                    if (!Tool.Options.ShowGuids)
                    {
                        ToolConsole.WriteLine(SR.GetString(SR.InterfaceRemoved, endpointConfig.ComponentProgID, endpointConfig.InterfaceName));
                    }
                    else
                    {
                        ToolConsole.WriteLine(SR.GetString(SR.InterfaceRemoved, endpointConfig.Clsid, endpointConfig.Iid));
                    }
                }
                else if (!endpointConfig.IsMexEndpoint)
                {
                    if (!Tool.Options.ShowGuids)
                    {
                        ToolConsole.WriteWarning(SR.GetString(SR.InterfaceNotExposed, endpointConfig.ComponentProgID, endpointConfig.InterfaceName));
                    }
                    else
                    {
                        ToolConsole.WriteWarning(SR.GetString(SR.InterfaceNotExposed, endpointConfig.Clsid, endpointConfig.Iid));
                    }
                }
            }

            this.hasServices = ServiceModelSectionGroup.GetSectionGroup(config).Services.Services.Count > 0;
            if (anyRemoved)
            {
                WasModified = true;
                config.Save();
            }
        }
        // returns true if deleted
        static bool RemoveComponent(ICatalog2 catalog, string partitionId, string applicationId, string progid)
        {
            int deleteIndex = -1;
            ICatalogCollection partitions = (ICatalogCollection)catalog.GetCollection(CollectionName.Partitions);

            partitions.Populate();
            ICatalogCollection applications = (ICatalogCollection)(partitions.GetCollection(CollectionName.Applications, partitionId));

            applications.Populate();
            ICatalogCollection components = (ICatalogCollection)(applications.GetCollection(CollectionName.Components, applicationId));

            try
            {
                components.Populate();
                bool   is64bit = false;
                string clsid   = null;
                for (int i = 0; i < components.Count(); i++)
                {
                    ICatalogObject comp = (ICatalogObject)components.Item(i);
                    if (((string)comp.Name()).ToLowerInvariant() == progid.ToLowerInvariant())
                    {
                        clsid       = ((string)comp.Key()).ToLowerInvariant();
                        is64bit     = IsBitness64bit(comp);
                        deleteIndex = i;
                        break;
                    }
                }
                if (deleteIndex == -1)
                {
                    return(false);
                }

                components.Remove(deleteIndex);
                components.SaveChanges();
                RemoveClsidFromRegistry(is64bit, clsid);
            }
            catch (Exception e)
            {
                if (e is NullReferenceException || e is SEHException)
                {
                    throw e;
                }
                ToolConsole.WriteWarning(SR.GetString(SR.FailedToRemoveListenerComponentFromApplication, applicationId, progid));
            }
            return(true);
        }
Esempio n. 14
0
        public static List <string> FetchAllMethodsForInterface(ComAdminInterfaceInfo interfaceInfo, bool produceWarning)
        {
            Type typeOfInterfaceResolver = typeof(Message).Assembly.GetType("System.ServiceModel.ComIntegration.TypeCacheManager");

            object resolver = typeOfInterfaceResolver.InvokeMember("Provider", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetProperty, null, null, null, CultureInfo.InvariantCulture);

            object[] args = new object[1] {
                interfaceInfo.Iid
            };
            Type typeOfInterface = null;

            try
            {
                typeOfInterface = typeOfInterfaceResolver.InvokeMember("VerifyType", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, resolver, args, CultureInfo.InvariantCulture) as Type;
            }
            catch (TargetInvocationException e)
            {
                if (e.GetBaseException() is System.IO.FileNotFoundException)
                {
                    throw CreateDescriptiveException((System.IO.FileNotFoundException)e.GetBaseException());
                }

                if (produceWarning)
                {
                    ToolConsole.WriteWarning(SR.GetString(SR.TypeResolutionForInterfaceFailed, Tool.Options.ShowGuids ? interfaceInfo.Iid.ToString("B") : interfaceInfo.Name, e.InnerException.Message));
                }

                return(null);
            }
            MethodInfo[] methods = typeOfInterface.GetMethods();
            if (methods.Length == 0)
            {
                return(null);
            }
            else
            {
                List <string> methodNames = new List <string>();
                foreach (MethodBase method in methods)
                {
                    methodNames.Add(method.Name);
                }
                return(methodNames);
            }
        }
Esempio n. 15
0
        public static string[] GetWebDirectoryNames(string webServer)
        {
            if (!IsIISInstalled())
            {
                return(null);
            }

            try
            {
                List <string> webDirectoryNames = new List <string>();

                DirectoryEntry webServiceEntry = new DirectoryEntry(webServer);

                foreach (DirectoryEntry child in webServiceEntry.Children)
                {
                    if (child.SchemaClassName.ToUpperInvariant() == "IISWEBDIRECTORY" || child.SchemaClassName.ToUpperInvariant() == "IISWEBVIRTUALDIR")
                    {
                        webDirectoryNames.Add(child.Name);

                        // Must special case the "ROOT" vDir, since most actual vDirs are subchildren of the ROOT vdir of a server.
                        if (child.Name.ToUpperInvariant() == "ROOT")
                        {
                            foreach (DirectoryEntry rootChild in child.Children)
                            {
                                if (rootChild.SchemaClassName.ToUpperInvariant() == "IISWEBDIRECTORY" || rootChild.SchemaClassName.ToUpperInvariant() == "IISWEBVIRTUALDIR")
                                {
                                    webDirectoryNames.Add("ROOT" + "/" + rootChild.Name);
                                }
                            }
                        }
                    }
                }
                return(webDirectoryNames.ToArray());
            }
            catch (COMException ex)
            {
                // assume a failure here means that no web directory exist
                ToolConsole.WriteWarning(SR.GetString(SR.CannotGetWebDirectoryForServer,
                                                      webServer, ex.ErrorCode, ex.Message));
                return(null);
            }
        }
        public static ComAdminAppInfo GetAppInfo(string appidOrName)
        {
            ICatalogObject     appObj  = null;
            ICatalogCollection appColl = null;

            if (!FindApplication(appidOrName, out appObj, out appColl))
            {
                return(null);
            }

            ComAdminAppInfo appInfo = null;

            try
            {
                appInfo = new ComAdminAppInfo(appObj, appColl);
            }
            catch (COMException ex)
            {
                ToolConsole.WriteWarning(SR.GetString(SR.FailedToFetchApplicationInformationFromCatalog, appidOrName, ex.ErrorCode, ex.Message));
            }
            return(appInfo);
        }
        public override void Add(IList <EndpointConfig> endpointConfigs)
        {
            ThrowIfClosed();

            Configuration config = GetConfiguration(false); // not read only

            Debug.Assert(config != null, "config != null");

            bool         anyAdded         = false;
            const string systemWebSection = "system.web";

            foreach (EndpointConfig endpointConfig in endpointConfigs)
            {
                Guid appid;
                //verify that we someone did not delete and reinstall the app from underneath us
                if (this.svcFileManager.ResolveClsid(endpointConfig.Clsid, out appid))
                {
                    if (endpointConfig.Appid != appid)
                    {
                        ToolConsole.WriteError(SR.GetString(SR.AppIDsDontMatch), "");
                        return;
                    }
                }
            }

            SystemWebSectionGroup swsg = (SystemWebSectionGroup)config.GetSectionGroup(systemWebSection);
            CompilationSection    compilationSection = swsg.Compilation;

            if (string.IsNullOrEmpty(compilationSection.TargetFramework) && RuntimeVersions.V40 == this.runtimeVersion)
            {
                anyAdded = true;
                compilationSection.TargetFramework = ".NETFramework, Version=v4.0";
            }

            foreach (EndpointConfig endpointConfig in endpointConfigs)
            {
                bool added = this.BaseAddEndpointConfig(config, endpointConfig);
                if (added)
                {
                    this.svcFileManager.Add(endpointConfig.Appid, endpointConfig.Clsid);
                    anyAdded = true;

                    // the metadata exchange endpoint is not displayed as a regular endpoint
                    if (endpointConfig.Iid == typeof(IMetadataExchange).GUID)
                    {
                        ToolConsole.WriteLine(SR.GetString(SR.MexEndpointAdded));
                        continue;
                    }

                    if (!Tool.Options.ShowGuids)
                    {
                        ToolConsole.WriteLine(SR.GetString(SR.InterfaceAdded, endpointConfig.ComponentProgID, endpointConfig.InterfaceName));
                    }
                    else
                    {
                        ToolConsole.WriteLine(SR.GetString(SR.InterfaceAdded, endpointConfig.Clsid, endpointConfig.Iid));
                    }
                }
                else
                {
                    // the metadata exchange endpoint is not displayed as a regular endpoint
                    if (endpointConfig.Iid == typeof(IMetadataExchange).GUID)
                    {
                        if (!Tool.Options.ShowGuids)
                        {
                            ToolConsole.WriteWarning(SR.GetString(SR.MexEndpointAlreadyExposed, endpointConfig.ComponentProgID));
                        }
                        else
                        {
                            ToolConsole.WriteWarning(SR.GetString(SR.MexEndpointAlreadyExposed, endpointConfig.Clsid));
                        }
                    }
                    else
                    {
                        if (!Tool.Options.ShowGuids)
                        {
                            ToolConsole.WriteWarning(SR.GetString(SR.InterfaceAlreadyExposed, endpointConfig.ComponentProgID, endpointConfig.InterfaceName));
                        }
                        else
                        {
                            ToolConsole.WriteWarning(SR.GetString(SR.InterfaceAlreadyExposed, endpointConfig.Clsid, endpointConfig.Iid));
                        }
                    }
                }
            }

            if (anyAdded)
            {
                WasModified = true;
                config.Save();
            }
        }
Esempio n. 18
0
        static internal bool VerifyInterface(ComAdminInterfaceInfo interfaceInfo, bool allowReferences, Guid clsid, bool produceError)
        {
            if (IsInternalInterface(interfaceInfo.Iid))
            {
                if (produceError)
                {
                    ToolConsole.WriteError(SR.GetString(SR.IsInternalInterfaceAndCannotBeExposedOverWebService, Tool.Options.ShowGuids ? interfaceInfo.Iid.ToString("B") : interfaceInfo.Name), "");
                }
                else
                {
                    ToolConsole.WriteWarning(SR.GetString(SR.IsInternalInterfaceAndCannotBeExposedOverWebService, Tool.Options.ShowGuids ? interfaceInfo.Iid.ToString("B") : interfaceInfo.Name));
                }

                return(false);
            }
            Type typeOfInterfaceResolver = typeof(Message).Assembly.GetType("System.ServiceModel.ComIntegration.TypeCacheManager");

            object resolver = typeOfInterfaceResolver.InvokeMember("Provider", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetProperty, null, null, null, CultureInfo.InvariantCulture);

            object[] args = new object[1] {
                interfaceInfo.Iid
            };
            Type typeOfInterface = null;

            try
            {
                typeOfInterface = typeOfInterfaceResolver.InvokeMember("VerifyType", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, resolver, args, CultureInfo.InvariantCulture) as Type;
            }
            catch (TargetInvocationException e)
            {
                if (e.GetBaseException() is System.IO.FileNotFoundException)
                {
                    throw CreateDescriptiveException((System.IO.FileNotFoundException)e.GetBaseException());
                }

                string exceptionMessage = SR.GetString(SR.TypeResolutionForInterfaceFailed, Tool.Options.ShowGuids ? interfaceInfo.Iid.ToString("B") : interfaceInfo.Name, e.InnerException.Message);

                if (DerivesFromServicedComponent(interfaceInfo.Iid, clsid))
                {
                    exceptionMessage += " " + SR.GetString(SR.ClassInterfacesNotSupported);
                }

                if (produceError)
                {
                    ToolConsole.WriteError(exceptionMessage, "");
                }
                else
                {
                    ToolConsole.WriteNonVerboseWarning(exceptionMessage);
                }

                return(false);
            }

            MethodInfo[] methods = typeOfInterface.GetMethods();
            if (methods.Length == 0)
            {
                if (produceError)
                {
                    ToolConsole.WriteError(SR.GetString(SR.InterfaceHasNoMethods, Tool.Options.ShowGuids ? interfaceInfo.Iid.ToString("B") : interfaceInfo.Name), "");
                }
                else
                {
                    ToolConsole.WriteNonVerboseWarning(SR.GetString(SR.InterfaceHasNoMethods, Tool.Options.ShowGuids ? interfaceInfo.Iid.ToString("B") : interfaceInfo.Name));
                }

                return(false);
            }
            string typeMismatchDetails;

            foreach (MethodInfo method in methods)
            {
                foreach (ParameterInfo parameter in method.GetParameters())
                {
                    Type typeOfParam = parameter.ParameterType;
                    if (typeOfParam.IsByRef)
                    {
                        typeOfParam = typeOfParam.GetElementType();
                    }

                    if (!IsValidParameter(typeOfParam, parameter, allowReferences, out typeMismatchDetails))
                    {
                        if (produceError)
                        {
                            ToolConsole.WriteError(SR.GetString(SR.ParameterOfMethodInInterfaceHasANonCompliantType, Tool.Options.ShowGuids ? interfaceInfo.Iid.ToString("B") : interfaceInfo.Name, method.Name, parameter.Name, typeMismatchDetails), "");
                        }
                        else
                        {
                            ToolConsole.WriteNonVerboseWarning(SR.GetString(SR.ParameterOfMethodInInterfaceHasANonCompliantType, Tool.Options.ShowGuids ? interfaceInfo.Iid.ToString("B") : interfaceInfo.Name, method.Name, parameter.Name, typeMismatchDetails));
                        }

                        return(false);
                    }
                }
                if (!IsValidParameter(method.ReturnType, method.ReturnTypeCustomAttributes, allowReferences, out typeMismatchDetails))
                {
                    if (produceError)
                    {
                        ToolConsole.WriteError(SR.GetString(SR.InvalidWebServiceReturnValue, method.ReturnType.Name, method.Name, Tool.Options.ShowGuids ? interfaceInfo.Iid.ToString("B") : interfaceInfo.Name, typeMismatchDetails), "");
                    }
                    else
                    {
                        ToolConsole.WriteNonVerboseWarning(SR.GetString(SR.InvalidWebServiceReturnValue, method.ReturnType.Name, method.Name, Tool.Options.ShowGuids ? interfaceInfo.Iid.ToString("B") : interfaceInfo.Name, typeMismatchDetails));
                    }

                    return(false);
                }
            }
            return(true);
        }
            public static SvcFile OpenExisting(string fileName)
            {
                if (!File.Exists(fileName))
                {
                    return(null);
                }

                string svcFileContents = null;
                Guid   appid;
                Guid   clsid;

                using (StreamReader sr = File.OpenText(fileName))
                {
                    svcFileContents = sr.ReadToEnd();
                }
                IDictionary <string, string> dictionary = null;

                try
                {
                    dictionary = ParseServiceDirective(svcFileContents);
                }
                catch (Exception e)
                {
                    if (e is NullReferenceException || e is SEHException)
                    {
                        throw e;
                    }
                    ToolConsole.WriteWarning(SR.GetString(SR.SvcFileParsingFailedWithError, fileName, e.Message));
                    return(null);
                }

                if (dictionary == null)
                {
                    return(null);
                }

                if (!dictionary.ContainsKey(factoryAttributeName) ||
                    !dictionary.ContainsKey(serviceAttributeName))
                {
                    return(null);
                }

                string typeName     = dictionary[factoryAttributeName];
                Type   factoryType  = typeof(WasHostedComPlusFactory);
                Type   compiledType = factoryType.Assembly.GetType(dictionary[factoryAttributeName], false);

                if (compiledType != factoryType)
                {
                    return(null);
                }

                string comPlusText = dictionary[serviceAttributeName];

                string[] parameters = comPlusText.Split(',');
                if (parameters.Length != 2)
                {
                    ToolConsole.WriteWarning(SR.GetString(SR.BadlyFormattedSvcFile, fileName));
                    return(null);
                }

                try
                {
                    clsid = new Guid(parameters[0]);
                    appid = new Guid(parameters[1]);
                }
                catch (FormatException)
                {
                    ToolConsole.WriteWarning(SR.GetString(SR.BadlyFormattedAppIDOrClsidInSvcFile, fileName));
                    return(null);
                }

                return(new SvcFile(appid, clsid, SvcFileState.Existing, new AtomicFile(fileName)));
            }
Esempio n. 20
0
        // NOTE that all EndpointConfigs returned by this guy have Guid.Empty as the appid, caller needs to fix that up
        protected Dictionary <string, List <EndpointConfig> > BaseGetEndpointsFromConfiguration(Configuration config)
        {
            Dictionary <string, List <EndpointConfig> > endpointConfigs = new Dictionary <string, List <EndpointConfig> >();

            ServiceModelSectionGroup sg = ServiceModelSectionGroup.GetSectionGroup(config);
            ServiceElementCollection serviceColl;

            try
            {
                serviceColl = sg.Services.Services;
            }
            catch (System.Configuration.ConfigurationErrorsException e)
            {
                ToolConsole.WriteWarning(e.Message);
                ToolConsole.WriteWarning(SR.GetString(SR.ConfigFileSkipped, e.Filename));

                return(endpointConfigs);
            }

            foreach (ServiceElement se in serviceColl)
            {
                string serviceType = se.Name;
                Guid   clsid       = Guid.Empty;
                Guid   appId       = Guid.Empty;

                string[] serviceParams = serviceType.Split(',');
                if (serviceParams.Length != 2)
                {
                    continue;
                }

                try
                {
                    appId = new Guid(serviceParams[0]);
                    clsid = new Guid(serviceParams[1]);
                }
                catch (FormatException)
                {
                    // Only Guid serviceTypes are of interest to us - those are the ones our listener picks up
                    continue;
                }


                List <EndpointConfig> list = null;
                if (endpointConfigs.ContainsKey(serviceType))
                {
                    list = endpointConfigs[serviceType];
                }
                else
                {
                    list = new List <EndpointConfig>();
                    endpointConfigs[serviceType] = list;
                }

                foreach (ServiceEndpointElement ee in se.Endpoints)
                {
                    EndpointConfig ec = null;
                    if (!IsMetaDataEndpoint(ee))
                    {
                        Guid contractType;
                        try
                        {
                            contractType = new Guid(ee.Contract);
                        }
                        catch (FormatException)
                        {
                            continue;
                        }
                        ec = new EndpointConfig(Guid.Empty,
                                                clsid,
                                                contractType,
                                                ee.Binding,
                                                ee.BindingConfiguration,
                                                ee.Address,
                                                false,
                                                new List <string>());
                    }
                    else
                    {
                        ec = new EndpointConfig(Guid.Empty,
                                                clsid,
                                                typeof(IMetadataExchange).GUID,
                                                ee.Binding,
                                                ee.BindingConfiguration,
                                                ee.Address,
                                                true, new List <string>());
                    }
                    list.Add(ec);
                }
            }
            return(endpointConfigs);
        }
Esempio n. 21
0
        public override void Add(IList <EndpointConfig> endpointConfigs)
        {
            ThrowIfClosed();

            Configuration config = GetConfiguration(false); // not read only

            Debug.Assert(config != null, "config != null");

            bool anyAdded = false;

            foreach (EndpointConfig endpointConfig in endpointConfigs)
            {
                Debug.Assert(endpointConfig.Appid == this.appInfo.ID, "can't add endpoint for a different application");

                bool added = this.BaseAddEndpointConfig(config, endpointConfig);

                if (added)
                {
                    anyAdded = true;

                    // the metadata exchange endpoint is not displayed as a regular endpoint
                    if (endpointConfig.Iid == typeof(IMetadataExchange).GUID)
                    {
                        ToolConsole.WriteLine(SR.GetString(SR.MexEndpointAdded));
                        continue;
                    }

                    if (!Tool.Options.ShowGuids)
                    {
                        ToolConsole.WriteLine(SR.GetString(SR.InterfaceAdded,
                                                           endpointConfig.ComponentProgID,
                                                           endpointConfig.InterfaceName));
                    }
                    else
                    {
                        ToolConsole.WriteLine(SR.GetString(SR.InterfaceAdded,
                                                           endpointConfig.Clsid,
                                                           endpointConfig.Iid));
                    }
                }
                else
                {
                    // the metadata exchange endpoint is not displayed as a regular endpoint
                    if (endpointConfig.Iid == typeof(IMetadataExchange).GUID)
                    {
                        if (!Tool.Options.ShowGuids)
                        {
                            ToolConsole.WriteWarning(SR.GetString(SR.MexEndpointAlreadyExposed, endpointConfig.ComponentProgID));
                        }
                        else
                        {
                            ToolConsole.WriteWarning(SR.GetString(SR.MexEndpointAlreadyExposed, endpointConfig.Clsid));
                        }
                    }
                    else
                    {
                        if (!Tool.Options.ShowGuids)
                        {
                            ToolConsole.WriteWarning(SR.GetString(SR.InterfaceAlreadyExposed,
                                                                  endpointConfig.ComponentProgID,
                                                                  endpointConfig.InterfaceName));
                        }
                        else
                        {
                            ToolConsole.WriteWarning(SR.GetString(SR.InterfaceAlreadyExposed,
                                                                  endpointConfig.Clsid,
                                                                  endpointConfig.Iid));
                        }
                    }
                }
            }

            if (anyAdded)
            {
                WasModified = true;

                config.Save();
            }

            this.hasServices = true;
        }