Пример #1
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);
        }
Пример #2
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);
        }
Пример #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);
            }
        }
Пример #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());
        }
Пример #5
0
        /// <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);
            }
        }
Пример #6
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);
            }
        }