예제 #1
0
파일: Program.cs 프로젝트: bnjMichel/waqs
        private static void WriteWebGlobal(string edmxPath, string edmxName, string edmxProjectPath, string projectDirectoryPath, string toolsPath, string rootNamespace, string assemblyNetVersion, string assemblyName, string assemblyVersion, string wcfServiceNamespace, string templateWebConfigPath, bool addGlobalService, bool isWCF)
        {
            if (wcfServiceNamespace == null)
                wcfServiceNamespace = rootNamespace + ".WCFService";
            string webConfigFilePath = Path.Combine(projectDirectoryPath, "Web.config");
            if (isWCF)
            {
                string globalAsaxFilePath = Path.Combine(projectDirectoryPath, "Global.asax");
                string globalAsaxCsFilePath = Path.Combine(projectDirectoryPath, "Global.asax.cs");
                if (!File.Exists(globalAsaxFilePath))
                    CopyFile(Path.Combine(toolsPath, "Global.asax"), globalAsaxFilePath, new[] { "$RootNamespace$", rootNamespace });

                string globalAsaxCsModelPath = Path.Combine(toolsPath, "Global.asax.cs");
                if (!File.Exists(globalAsaxCsFilePath))
                    CopyFile(globalAsaxCsModelPath, globalAsaxCsFilePath, new[] { "$RootNamespace$", rootNamespace });

                CopyFile(Path.Combine(toolsPath, "server.svc"), Path.Combine(projectDirectoryPath, edmxName + ".svc"), new[] { "$WCFServiceNamespace$", wcfServiceNamespace }, new[] { "$edmxName$", edmxName });

                string globalAsaxCsContent;
                using (var sr = new StreamReader(globalAsaxCsFilePath))
                {
                    globalAsaxCsContent = new ApplicationStartRewriter(edmxName, globalAsaxCsModelPath).Visit(SyntaxFactory.ParseCompilationUnit(sr.ReadToEnd())).NormalizeWhitespace().ToString();
                }
                using (var sw = new StreamWriter(globalAsaxCsFilePath))
                {
                    sw.Write(globalAsaxCsContent);
                }
            }

            if (!File.Exists(webConfigFilePath))
                CopyFile(Path.Combine(toolsPath, "Web.config"), webConfigFilePath, new[] { "$AssemblyNETVersion$", assemblyNetVersion }, new[] { "$AssemblyName$", assemblyName }, new[] { "$AssemblyVersion$", assemblyVersion });

            XElement webConfig = XElement.Load(webConfigFilePath);
            XElement connectionStrings = webConfig.Element("connectionStrings");
            string edmxFolderPath = Path.GetDirectoryName(edmxPath);
            string edmxProjectFolderPath = Path.GetDirectoryName(edmxProjectPath);
            string folderDiff = edmxFolderPath.Substring(edmxProjectFolderPath.Length + (edmxFolderPath.Length > edmxProjectFolderPath.Length ? 1 : 0));
            if (connectionStrings == null)
            {
                if (templateWebConfigPath != null)
                {
                    XElement templateConfig = XElement.Load(templateWebConfigPath);
                    XElement appConnectionString = templateConfig.Element("connectionStrings").Elements("add").FirstOrDefault(add => add.Attribute("connectionString").Value.Contains(edmxName + ".csdl"));
                    appConnectionString.Attribute("connectionString").Value = Regex.Replace(appConnectionString.Attribute("connectionString").Value, string.Concat(@"(?:[^\|\/]+\.)?", edmxName, "(.(?:csdl|ssdl|msl))"), m => string.Concat("WAQS.", edmxName, ".", edmxName, m.Groups[1].Value));
                    webConfig.Add(
                        new XElement("connectionStrings", appConnectionString));
                }
            }
            else
            {
                XElement connectionString = connectionStrings.Elements("add").FirstOrDefault(add => add.Attribute("connectionString").Value.Contains(edmxName + ".csdl"));
                if (connectionString == null)
                {
                    if (templateWebConfigPath != null)
                    {
                        XElement templateConfig = XElement.Load(templateWebConfigPath);
                        XElement appConnectionString = templateConfig.Element("connectionStrings").Elements("add").FirstOrDefault(add => add.Attribute("connectionString").Value.Contains(edmxName + ".csdl"));
                        appConnectionString.Attribute("connectionString").Value = Regex.Replace(appConnectionString.Attribute("connectionString").Value, string.Concat(@"(?:[^\|\/]+\.)?", edmxName, "(.(?:csdl|ssdl|msl))"), m => string.Concat("WAQS.", edmxName, ".", edmxName, m.Groups[1].Value));
                        connectionStrings.Add(appConnectionString);
                    }
                }
                else
                    connectionString.Attribute("connectionString").Value = Regex.Replace(connectionString.Attribute("connectionString").Value, string.Concat(@"(?:[^\|\/]+\.)?", edmxName, "(.(?:csdl|ssdl|msl))"), m => string.Concat("WAQS.", edmxName, ".", edmxName, m.Groups[1].Value));
            }

            if (isWCF)
            {
                XElement serviceModel = webConfig.Element("system.serviceModel");
                if (serviceModel == null)
                {
                    webConfig.Add(serviceModel = XElement.Parse(XElement.Load(Path.Combine(toolsPath, "Web.config")).Element("system.serviceModel").ToString().Replace("$AssemblyName$", assemblyName).Replace("$AssemblyVersion$", assemblyVersion)));
                }

                string service = string.Format("<service behaviorConfiguration=\"WAQSServiceBehavior\" name=\"{0}.{1}WCFService\"><endpoint behaviorConfiguration=\"WAQSEndpointBehavior\" address=\"\" binding=\"customBinding\" bindingConfiguration=\"HttpBinaryBinding\" contract=\"{0}.Contract.I{1}WCFService\"/></service>", wcfServiceNamespace, edmxName);

                var servicesElement = serviceModel.Element("services");
                servicesElement.Add(XElement.Parse(service));

                if (addGlobalService)
                {
                    string globalService = string.Format("<service behaviorConfiguration=\"WAQSServiceBehavior\" name=\"{0}.GlobalWCFService\"><endpoint behaviorConfiguration=\"WAQSEndpointBehavior\" address=\"\" binding=\"customBinding\" bindingConfiguration=\"HttpBinaryBinding\" contract=\"{0}.IGlobalWCFService\"/></service>", rootNamespace);

                    servicesElement.Add(XElement.Parse(globalService));
                }
            }

            webConfig.Save(webConfigFilePath);
        }
예제 #2
0
파일: Program.cs 프로젝트: kouweizhong/waqs
        private static void WriteWebGlobal(string edmxPath, string edmxName, string edmxProjectPath, string projectDirectoryPath, string toolsPath, string rootNamespace, string assemblyNetVersion, string assemblyName, string assemblyVersion, string wcfServiceNamespace, string templateWebConfigPath, bool addGlobalService, bool isWCF)
        {
            if (wcfServiceNamespace == null)
            {
                wcfServiceNamespace = rootNamespace + ".WCFService";
            }
            string webConfigFilePath = Path.Combine(projectDirectoryPath, "Web.config");

            if (isWCF)
            {
                string globalAsaxFilePath   = Path.Combine(projectDirectoryPath, "Global.asax");
                string globalAsaxCsFilePath = Path.Combine(projectDirectoryPath, "Global.asax.cs");
                if (!File.Exists(globalAsaxFilePath))
                {
                    CopyFile(Path.Combine(toolsPath, "Global.asax"), globalAsaxFilePath, new[] { "$RootNamespace$", rootNamespace });
                }

                string globalAsaxCsModelPath = Path.Combine(toolsPath, "Global.asax.cs");
                if (!File.Exists(globalAsaxCsFilePath))
                {
                    CopyFile(globalAsaxCsModelPath, globalAsaxCsFilePath, new[] { "$RootNamespace$", rootNamespace });
                }

                CopyFile(Path.Combine(toolsPath, "server.svc"), Path.Combine(projectDirectoryPath, edmxName + ".svc"), new[] { "$WCFServiceNamespace$", wcfServiceNamespace }, new[] { "$edmxName$", edmxName });

                string globalAsaxCsContent;
                using (var sr = new StreamReader(globalAsaxCsFilePath))
                {
                    globalAsaxCsContent = new ApplicationStartRewriter(edmxName, globalAsaxCsModelPath).Visit(SyntaxFactory.ParseCompilationUnit(sr.ReadToEnd())).NormalizeWhitespace().ToString();
                }
                using (var sw = new StreamWriter(globalAsaxCsFilePath))
                {
                    sw.Write(globalAsaxCsContent);
                }
            }

            if (!File.Exists(webConfigFilePath))
            {
                CopyFile(Path.Combine(toolsPath, "Web.config"), webConfigFilePath, new[] { "$AssemblyNETVersion$", assemblyNetVersion }, new[] { "$AssemblyName$", assemblyName }, new[] { "$AssemblyVersion$", assemblyVersion });
            }

            XElement webConfig             = XElement.Load(webConfigFilePath);
            XElement connectionStrings     = webConfig.Element("connectionStrings");
            string   edmxFolderPath        = Path.GetDirectoryName(edmxPath);
            string   edmxProjectFolderPath = Path.GetDirectoryName(edmxProjectPath);
            string   folderDiff            = edmxFolderPath.Substring(edmxProjectFolderPath.Length + (edmxFolderPath.Length > edmxProjectFolderPath.Length ? 1 : 0));

            if (connectionStrings == null)
            {
                if (templateWebConfigPath != null)
                {
                    XElement templateConfig      = XElement.Load(templateWebConfigPath);
                    XElement appConnectionString = templateConfig.Element("connectionStrings").Elements("add").FirstOrDefault(add => add.Attribute("connectionString").Value.Contains(edmxName + ".csdl"));
                    appConnectionString.Attribute("connectionString").Value = Regex.Replace(appConnectionString.Attribute("connectionString").Value, string.Concat(@"(?:[^\|\/]+\.)?", edmxName, "(.(?:csdl|ssdl|msl))"), m => string.Concat("WAQS.", edmxName, ".", edmxName, m.Groups[1].Value));
                    webConfig.Add(
                        new XElement("connectionStrings", appConnectionString));
                }
            }
            else
            {
                XElement connectionString = connectionStrings.Elements("add").FirstOrDefault(add => add.Attribute("connectionString").Value.Contains(edmxName + ".csdl"));
                if (connectionString == null)
                {
                    if (templateWebConfigPath != null)
                    {
                        XElement templateConfig      = XElement.Load(templateWebConfigPath);
                        XElement appConnectionString = templateConfig.Element("connectionStrings").Elements("add").FirstOrDefault(add => add.Attribute("connectionString").Value.Contains(edmxName + ".csdl"));
                        appConnectionString.Attribute("connectionString").Value = Regex.Replace(appConnectionString.Attribute("connectionString").Value, string.Concat(@"(?:[^\|\/]+\.)?", edmxName, "(.(?:csdl|ssdl|msl))"), m => string.Concat("WAQS.", edmxName, ".", edmxName, m.Groups[1].Value));
                        connectionStrings.Add(appConnectionString);
                    }
                }
                else
                {
                    connectionString.Attribute("connectionString").Value = Regex.Replace(connectionString.Attribute("connectionString").Value, string.Concat(@"(?:[^\|\/]+\.)?", edmxName, "(.(?:csdl|ssdl|msl))"), m => string.Concat("WAQS.", edmxName, ".", edmxName, m.Groups[1].Value));
                }
            }

            if (isWCF)
            {
                XElement serviceModel = webConfig.Element("system.serviceModel");
                if (serviceModel == null)
                {
                    webConfig.Add(serviceModel = XElement.Parse(XElement.Load(Path.Combine(toolsPath, "Web.config")).Element("system.serviceModel").ToString().Replace("$AssemblyName$", assemblyName).Replace("$AssemblyVersion$", assemblyVersion)));
                }

                string service = string.Format("<service behaviorConfiguration=\"WAQSServiceBehavior\" name=\"{0}.{1}WCFService\"><endpoint behaviorConfiguration=\"WAQSEndpointBehavior\" address=\"\" binding=\"customBinding\" bindingConfiguration=\"HttpBinaryBinding\" contract=\"{0}.Contract.I{1}WCFService\"/></service>", wcfServiceNamespace, edmxName);

                var servicesElement = serviceModel.Element("services");
                servicesElement.Add(XElement.Parse(service));

                if (addGlobalService)
                {
                    string globalService = string.Format("<service behaviorConfiguration=\"WAQSServiceBehavior\" name=\"{0}.GlobalWCFService\"><endpoint behaviorConfiguration=\"WAQSEndpointBehavior\" address=\"\" binding=\"customBinding\" bindingConfiguration=\"HttpBinaryBinding\" contract=\"{0}.IGlobalWCFService\"/></service>", rootNamespace);

                    servicesElement.Add(XElement.Parse(globalService));
                }
            }

            webConfig.Save(webConfigFilePath);
        }