Exemple #1
0
        /// <summary>
        /// Determines if a project is a WCF Client Project based on the following :-
        ///     If it has a web.config or App.config based configuaration, and has a client tag
        ///     in the nested configuration/system.serviceModel tag.
        /// </summary>
        /// <param name="analyzerResult"></param>
        /// <returns>Whether a project is a WCF Client</returns>
        public override bool IsPresent(AnalyzerResult analyzerResult)
        {
            string projectDir = analyzerResult.ProjectResult.ProjectRootPath;

            string webConfigFile = Path.Combine(projectDir, Rules.Config.Constants.WebConfig);
            string appConfigFile = Path.Combine(projectDir, Rules.Config.Constants.AppConfig);

            if (File.Exists(webConfigFile))
            {
                var config = WebConfigManager.LoadWebConfigAsXDocument(projectDir);
                if (config.ContainsElement(Constants.WCFClientElementPath))
                {
                    return(true);
                }
            }
            if (File.Exists(appConfigFile))
            {
                var config = WebConfigManager.LoadAppConfigAsXDocument(projectDir);
                if (config.ContainsElement(Constants.WCFClientElementPath))
                {
                    return(true);
                }
            }

            return(false);
        }
Exemple #2
0
        public bool IsWCFConfigService(AnalyzerResult analyzerResult)
        {
            string projectDir = analyzerResult.ProjectResult.ProjectRootPath;

            string webConfigFile = Path.Combine(projectDir, Rules.Config.Constants.WebConfig);
            string appConfigFile = Path.Combine(projectDir, Rules.Config.Constants.AppConfig);

            // For Config based look for <services> element.
            if (File.Exists(webConfigFile))
            {
                var config = WebConfigManager.LoadWebConfigAsXDocument(projectDir);
                if (config.ContainsElement(Constants.WCFServiceElementPath))
                {
                    return(true);
                }
            }
            if (File.Exists(appConfigFile))
            {
                var config = WebConfigManager.LoadAppConfigAsXDocument(projectDir);
                if (config.ContainsElement(Constants.WCFServiceElementPath))
                {
                    return(true);
                }
            }

            return(false);
        }
Exemple #3
0
        /// <summary>
        /// Get Config File Path for the Project.
        /// </summary>
        /// <returns>Config File Path</returns>
        public string GetConfigFilePath()
        {
            var webConfig = WebConfigManager.LoadWebConfigAsXDocument(_projectPath);
            var appConfig = WebConfigManager.LoadAppConfigAsXDocument(_projectPath);

            if (webConfig.ContainsElement(Constants.SystemServiceModelElementPath))
            {
                return(Path.Combine(_projectPath, Constants.WebConfig));
            }
            else if (appConfig.ContainsElement(Constants.SystemServiceModelElementPath))
            {
                return(Path.Combine(_projectPath, Constants.AppConfig));
            }
            else
            {
                return(null);
            }
        }
Exemple #4
0
        /// <summary>
        /// Generate New Config File for CoreWCF.
        /// </summary>
        /// <returns>New Config File for CoreWCF</returns>
        public string GetNewConfigFile()
        {
            var webConfig = WebConfigManager.LoadWebConfigAsXDocument(_projectPath);
            var appConfig = WebConfigManager.LoadAppConfigAsXDocument(_projectPath);

            WebConfigXDocument config;

            if (webConfig.ContainsElement(Constants.SystemServiceModelElementPath))
            {
                config = webConfig;
            }
            else if (appConfig.ContainsElement(Constants.SystemServiceModelElementPath))
            {
                config = appConfig;
            }
            else
            {
                return(null);
            }

            var wcfConfig = config.GetDescendantsAndSelf(Constants.SystemServiceModelElement);

            if (!wcfConfig.IsNullOrEmpty())
            {
                var newXDoc = new XDocument(new XDeclaration(Constants.ConfigXMLVersion, Constants.ConfigXMLEncoding, Constants.ConfigXMLStandalone), wcfConfig.First());

                newXDoc.Descendants().Where(d => d.Name == Constants.HostElement).Remove();
                newXDoc.Descendants().Where(d => d.Name == Constants.EndpointElement && d.Attribute(Constants.BindingAttribute)?.Value == Constants.MexBinding).Remove();

                var serviceModelElement = newXDoc.Element(Constants.SystemServiceModelElement);

                serviceModelElement.ReplaceWith(new XElement(Constants.ConfigurationElement, serviceModelElement));

                var newConfigFile = new StringWriter();
                newXDoc.Save(newConfigFile);

                return(newConfigFile.ToString());
            }

            return(config.GetDocAsString());
        }
        /// <summary>
        /// Check for Binding and Mode in Config based WCF Service
        /// </summary>
        /// <param name="projectDir">Project Directory for Config based WCF Service</param>
        /// <param name="bindingsTransportMap">Dictionary of binding and transport mode</param>
        public static void ConfigBasedCheck(string projectDir, Dictionary <string, BindingConfiguration> bindingsTransportMap)
        {
            var webConfig = WebConfigManager.LoadWebConfigAsXDocument(projectDir);
            var appConfig = WebConfigManager.LoadAppConfigAsXDocument(projectDir);

            if (webConfig != null && webConfig.ContainsElement(Constants.WCFBindingElementPath))
            {
                BindingTagCheck(webConfig, bindingsTransportMap);
            }
            else if (appConfig != null && appConfig.ContainsElement(Constants.WCFBindingElementPath))
            {
                BindingTagCheck(appConfig, bindingsTransportMap);
            }
            else if (webConfig != null && webConfig.ContainsElement(Constants.WCFEndpointElementPath))
            {
                EndpointTagCheck(webConfig, bindingsTransportMap);
            }
            else if (appConfig != null && appConfig.ContainsElement(Constants.WCFEndpointElementPath))
            {
                EndpointTagCheck(appConfig, bindingsTransportMap);
            }
        }
Exemple #6
0
 protected WebConfigXDocument LoadWebConfig(string projectDir)
 {
     return(WebConfigManager.LoadWebConfigAsXDocument(projectDir));
 }
        /// <summary>
        /// Determines if Forms Authentication with Membership is being used in a given project based on
        /// Web.config settings.
        ///
        /// Qualifications:
        /// 1. Web.config uses Forms authentication and Membership:
        ///    <configuration>
        ///      <system.web>
        ///        <authentication mode="Forms">
        ///        </authentication>
        ///        <membership>
        ///          ...
        ///        </membership>
        ///      </system.web>
        ///    </configuration>
        ///
        /// </summary>
        /// <param name="analyzerResult">Source code analysis results</param>
        /// <returns>Whether or not Forms Authentication with Membership is used</returns>
        public override bool IsPresent(AnalyzerResult analyzerResult)
        {
            var config = WebConfigManager.LoadWebConfigAsXDocument(analyzerResult.ProjectResult.ProjectRootPath);

            return(base.IsPresent(analyzerResult) && config.ContainsElement(Constants.MembershipElementPath));
        }
        public bool IsPresentInConfig(AnalyzerResult analyzerResult)
        {
            var config = WebConfigManager.LoadWebConfigAsXDocument(analyzerResult.ProjectResult.ProjectRootPath);

            return(config.ContainsAttributeWithValue(Constants.AuthenticationElementElementPath, Constants.ModeAttribute, Constants.FederatedAuthenticationType));
        }
Exemple #9
0
        /// <summary>
        /// Determines whether project config has <behaviors> tag
        /// </summary>
        /// <returns>Whether <behaviors> tag is present</returns>
        public bool HasBehavioursTag()
        {
            var config = WebConfigManager.LoadWebConfigAsXDocument(_projectPath);

            return(config.ContainsElement(Constants.BehaviorsPath));
        }
        public void LoadWebConfigAsXDocument_Loads_Empty_Config_If_No_Config_Exists()
        {
            var xDocumentWebConfig = WebConfigManager.LoadWebConfigAsXDocument("");

            Assert.AreEqual(0, xDocumentWebConfig.GetConfigProperties().Count());
        }
        public void LoadWebConfigAsXDocument_Loads_WebConfig_As_XDocument()
        {
            var xDocumentWebConfig = WebConfigManager.LoadWebConfigAsXDocument(_configsDir);

            Assert.True(xDocumentWebConfig.ContainsElement("configuration"));
        }
Exemple #12
0
        /// <summary>
        /// Check for Binding and Mode in Code based WCF Service
        /// </summary>
        /// <param name="project">ProjectWorkspace object of Code based WCF Service</param>
        /// <param name="bindingsTransportMap">Dictionary of binding and transport mode</param>
        public static void CodeBasedCheck(ProjectWorkspace project, Dictionary <string, BindingConfiguration> bindingsTransportMap)
        {
            IEnumerable <InvocationExpression> addEndpointInvocations = project.GetInvocationExpressionsByMethodName(Constants.AddServiceEndpointType);

            foreach (var addEndpointInvocation in addEndpointInvocations)
            {
                var argumentCount = addEndpointInvocation.Arguments.Count();

                if (argumentCount == 1)
                {
                    var endpointIdentifier = addEndpointInvocation.Arguments.First();

                    IEnumerable <ObjectCreationExpression> serviceEndpointObjectExpressions = project.GetObjectCreationExpressionBySemanticClassType(Constants.ServiceEndpointClass);

                    var endpointArgumentObjects = serviceEndpointObjectExpressions.
                                                  SelectMany(s => s.GetObjectCreationExpressionBySemanticNamespace(Constants.SystemServiceModelClass));

                    var bindingArgumentObjects = endpointArgumentObjects.Where(e => e.SemanticClassType != Constants.EndpointAddressType);

                    foreach (var binding in bindingArgumentObjects)
                    {
                        var bindingName          = binding.SemanticClassType;
                        var bindingNameFormatted = bindingName.ToString().ToLower();

                        bindingsTransportMap[bindingName] = new BindingConfiguration
                        {
                            Mode            = GetModeFromObjectArguments(binding.Arguments),
                            EndpointAddress = GetEndpointFromInvocationArguments(addEndpointInvocation.Arguments)
                        };
                    }
                }

                var bindingArgument = addEndpointInvocation.Arguments.Where(a => a.SemanticType.ToLower().Contains("binding"));

                var objectDeclarations = addEndpointInvocation.GetObjectCreationExpressionBySemanticNamespace(Constants.SystemServiceModelClass);

                ObjectCreationExpression objectCreationExpression;

                if (objectDeclarations.IsNullOrEmpty())
                {
                    if (!bindingArgument.IsNullOrEmpty())
                    {
                        var bindingName = bindingArgument.First().SemanticType;

                        var objectCreationExpressionList = project.GetObjectCreationExpressionBySemanticClassType(bindingName);

                        if (!objectCreationExpressionList.IsNullOrEmpty())
                        {
                            objectCreationExpression = objectCreationExpressionList.First();
                        }
                        else
                        {
                            bindingsTransportMap[bindingName] = new BindingConfiguration();
                            return;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    objectCreationExpression = objectDeclarations.First();
                }

                if (objectCreationExpression != null)
                {
                    var bindingName = objectCreationExpression.SemanticClassType.ToLower();

                    bindingsTransportMap[bindingName] = new BindingConfiguration
                    {
                        Mode            = GetModeFromObjectArguments(objectCreationExpression.Arguments),
                        EndpointAddress = GetEndpointFromInvocationArguments(addEndpointInvocation.Arguments)
                    };
                }
            }

            var webConfig = WebConfigManager.LoadWebConfigAsXDocument(project.ProjectRootPath);
            var appConfig = WebConfigManager.LoadAppConfigAsXDocument(project.ProjectRootPath);

            if (webConfig != null && webConfig.ContainsElement(Constants.WCFBindingElementPath))
            {
                BindingTagCheck(webConfig, bindingsTransportMap);
            }
            else if (appConfig != null && appConfig.ContainsElement(Constants.WCFBindingElementPath))
            {
                BindingTagCheck(appConfig, bindingsTransportMap);
            }

            if (webConfig != null && webConfig.ContainsElement(Constants.WCFProtocolMappingElement))
            {
                ProtocolTagCheck(webConfig, bindingsTransportMap);
            }
            else if (appConfig != null && appConfig.ContainsElement(Constants.WCFProtocolMappingElement))
            {
                ProtocolTagCheck(appConfig, bindingsTransportMap);
            }
        }