Example #1
0
        private string GenerateCode(CodeCompileUnit codeCompileUnit, InputPackage inputPackage)
        {
            CodeDomProvider provider = new Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider();
            string          result   = string.Empty;

            using (TextWriter w = File.CreateText(outputPath + "\\" + inputPackage.Name + ".cs"))
            {
                provider.GenerateCodeFromCompileUnit(codeCompileUnit, w, new CodeGeneratorOptions());
            }
            return(result);
        }
Example #2
0
        public virtual string GenrateWcfCode(string xsdlUrl, string language)
        {
            //"/develop/code/os/dotnet/WebService/GetWcfGenreatedCode/" + wcfGuid
            Uri mexAddress = new Uri(xsdlUrl);
            MetadataExchangeClientMode mexMode = MetadataExchangeClientMode.HttpGet;

            // Get Metadata file from service
            MetadataExchangeClient mexClient = new MetadataExchangeClient(new EndpointAddress(mexAddress));

            mexClient.ResolveMetadataReferences = true;
            MetadataSet metaSet = mexClient.GetMetadata(mexAddress, mexMode);



            //Import all contracts and endpoints
            WsdlImporter importer = new WsdlImporter(metaSet);

            Collection <ContractDescription> contracts    = importer.ImportAllContracts();
            ServiceEndpointCollection        allEndpoints = importer.ImportAllEndpoints();

            //Generate type information for each contract
            ServiceContractGenerator generator = new ServiceContractGenerator();

            // var endpointsForContracts = new Dictionary<string, IEnumerable<ServiceEndpoint>>();

            foreach (ContractDescription contract in contracts)
            {
                generator.GenerateServiceContractType(contract);
                // Keep a list of each contract's endpoints
                //  endpointsForContracts[contract.Name] = allEndpoints.Where(se => se.Contract.Name == contract.Name).ToList();
            }

            if (generator.Errors.Count != 0)
            {
                throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.WebServiceCodeGenratorError));
            }

            // Generate a code file for the contracts
            CodeGeneratorOptions options = new CodeGeneratorOptions();

            options.BracingStyle = "C";
            //CodeDomProvider codeDomProvider = CodeDomProvider.CreateProvider(language == "csharp" ? "C#": "VisualBasic");



            //      System.CodeDom.Compiler.IndentedTextWriter textWriter
            //= new System.CodeDom.Compiler.IndentedTextWriter(new System.IO.StreamWriter(outputFilePath));
            StringBuilder stringBuilder = new StringBuilder();
            StringWriter  textWriter    = new StringWriter(stringBuilder);



            if (language == "csharp")
            {
                if (Convert.ToBoolean(WebConfigManager.GetSettingByOption(RoslynCompiler).Value))
                {
                    //roslyn compiler
                    var cSharpCodeProvider = new Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider();


                    cSharpCodeProvider.GenerateCodeFromCompileUnit(
                        generator.TargetCompileUnit, textWriter, options
                        );
                }
                else
                {
                    //old compiler
                    var cSharpCodeProvider = CodeDomProvider.CreateProvider(CSharp);


                    cSharpCodeProvider.GenerateCodeFromCompileUnit(
                        generator.TargetCompileUnit, textWriter, options
                        );
                }
            }
            else
            {
                if (Convert.ToBoolean(WebConfigManager.GetSettingByOption(RoslynCompiler).Value))
                {
                    //roslyn compiler
                    var vBCodeProvider = new Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider();
                    vBCodeProvider.GenerateCodeFromCompileUnit(
                        generator.TargetCompileUnit, textWriter, options
                        );
                }
                else
                {
                    //old compiler
                    var vBCodeProvider = CodeDomProvider.CreateProvider(VisualBasic);
                    vBCodeProvider.GenerateCodeFromCompileUnit(
                        generator.TargetCompileUnit, textWriter, options
                        );
                }
            }

            textWriter.Close();

            return(stringBuilder.ToString());



            // File.WriteAllText(outputFilePath, outputFile, Encoding.UTF8);



            //// Compile the code file to an in-memory assembly
            //// Don't forget to add all WCF-related assemblies as references
            //CompilerParameters compilerParameters = new CompilerParameters(
            //    new string[] {
            //        "System.dll", "System.ServiceModel.dll",
            //        "System.Runtime.Serialization.dll" });
            //compilerParameters.GenerateInMemory = true;

            //CompilerResults results = codeDomProvider.CompileAssemblyFromDom(compilerParameters, generator.TargetCompileUnit);

            //if (results.Errors.Count > 0)
            //{
            //    throw new Exception("There were errors during generated code compilation");
            //}

            //// Find the proxy type that was generated for the specified contract
            //// (identified by a class that implements
            //// the contract and ICommunicationbject)
            //Type clientProxyType = results.CompiledAssembly.GetTypes().First(t => t.IsClass && t.GetInterface(contractName)
            //!= null && t.GetInterface(typeof(ICommunicationObject).Name) != null);

            //// Get the first service endpoint for the contract
            //ServiceEndpoint serviceEndPoint = endpointsForContracts[contractName].First();
            //object instance = results.CompiledAssembly.CreateInstance(clientProxyType.Name,
            //    false, System.Reflection.BindingFlags.CreateInstance, null, new object[] { serviceEndPoint.Binding, serviceEndPoint.Address }, CultureInfo.CurrentCulture, null);
        }