コード例 #1
0
        internal static List <ExplorerItem> GetSchemaAndBuildAssembly(string driverLocation, ConnectionProperties props, AssemblyName name, ref string nameSpace, ref string typeName)
        {
            var document = SwaggerDocument.FromUrlAsync(props.Uri).Result;

            // Compile the code into the assembly, using the assembly name provided:
            BuildAssemblySingleClientFromOpId(document, driverLocation, name, nameSpace, ref typeName);

            //// Use the schema to populate the Schema Explorer:
            List <ExplorerItem> schema = GetSchemaSingleClient(document, typeName);

            return(schema);
        }
コード例 #2
0
        internal static List <ExplorerItem> GetSchemaAndBuildAssembly(string driverLocation, ConnectionProperties props, AssemblyName name, ref string nameSpace, ref string typeName)
        {
            List <ExplorerItem> schema = new List <ExplorerItem>();

            var             uri      = new Uri(props.Uri);
            SwaggerDocument document = null;

            if (uri.Scheme == "file")
            {
                document = SwaggerDocument.FromFileAsync(uri.LocalPath).Result;
            }
            else if (uri.Scheme == "http" || uri.Scheme == "https")
            {
                if (props.AuthOption == AuthenticationType.None)
                {
                    document = DownloadDefinition(props.Uri, props);
                }
                else if (props.AuthOption == AuthenticationType.Basic)
                {
                    document = DownloadDefinition(props.Uri, props, new NetworkCredential(props.UserName, props.Password, props.Domain));
                }
                else if (props.AuthOption == AuthenticationType.CurrentUser)
                {
                    document = DownloadDefinition(props.Uri, props, CredentialCache.DefaultNetworkCredentials);
                }
                else
                {
                    throw new NotSupportedException("Authentication method not supported.");
                }

                if (document.BaseUrl.StartsWith("/") && document.BasePath.StartsWith("/"))
                {
                    var t = document.BaseUrl;
                    document.BasePath = uri.Scheme + "://" + uri.Host + ":" + uri.Port + document.BaseUrl;
                    System.Diagnostics.Debug.WriteLine("Changing BaseUrl from '{0}' to '{1}'", t, document.BasePath);
                }

                if (string.IsNullOrEmpty(document.Host))
                {
                    document.Host = uri.Host;
                    System.Diagnostics.Debug.WriteLine("Host was null, setting it to '{0}'", document.Host);
                }
            }

            switch (props.GenOption)
            {
            case GeneratorType.SingleClientFromOperatinoId:

                // Compile the code into the assembly, using the assembly name provided:
                BuildAssemblySingleClientFromOpId(document, driverLocation, name, nameSpace, ref typeName, props);

                // Use the schema to populate the Schema Explorer:
                schema = GetSchemaSingleClient(document, typeName);
                break;

            case GeneratorType.SingleClientFromPathSegment:

                // Compile the code into the assembly, using the assembly name provided:
                BuildAssemblySingleClientFromPathSegOp(document, driverLocation, name, nameSpace, ref typeName, props);

                // Use the schema to populate the Schema Explorer:
                //schema = GetSchemaSingleClientPath(document, typeName);
                schema = GetSchemaViaReflection(name, nameSpace, typeName);
                break;

            case GeneratorType.MultipleClientsFromOperationId:

                // Compile the code into the assembly, using the assembly name provided:
                BuildAssemblyMultiClientFromOpId(document, driverLocation, name, nameSpace, ref typeName, props);

                // Use the schema to populate the Schema Explorer:
                schema = GetSchemaMultiClient(document, typeName);
                break;
            }

            return(schema);
        }
コード例 #3
0
        private static void BuildAssemblySingleClientFromPathSegOp(SwaggerDocument document, string location, AssemblyName name, string nameSpace, ref string typeName, ConnectionProperties props)
        {
            typeName = "Client";
            var settings = new SwaggerToCSharpClientGeneratorSettings
            {
                ClassName = "{controller}Client",

                OperationNameGenerator     = new SingleClientFromPathSegmentsOperationNameGenerator(),
                GenerateOptionalParameters = true,
                CSharpGeneratorSettings    =
                {
                    Namespace = nameSpace
                },
            };

            if (props.InjectHttpClient)
            {
                settings.DisposeHttpClient           = false;
                settings.ClientBaseClass             = nameSpace + ".MyClient";
                settings.UseHttpClientCreationMethod = true;
                settings.DisposeHttpClient           = props.DisposeHttpClient;
            }

            var generator = new SwaggerToCSharpClientGenerator(document, settings);

            var code = generator.GenerateFile();

            code = AddHttpClientInjectCode(code, props, nameSpace);

            CompilerResults results;
            var             assemblyNames = new List <string>()
            {
                "System.dll",
                "System.Core.dll",
                "System.Xml.dll",
                "System.Xml.Linq.dll",
                "System.Runtime.Serialization.dll",
                "System.Net.Http.dll",
                "LINQPad.exe",
                "System.ComponentModel.DataAnnotations.dll",
            };

            assemblyNames.Add(Path.Combine(location, "Newtonsoft.Json.dll"));

            using (var codeProvider = new CSharpCodeProvider(new Dictionary <string, string>()
            {
                { "CompilerVersion", "v4.0" }
            }))
            {
                var options = new CompilerParameters(
                    assemblyNames.ToArray(),
                    name.CodeBase,
                    true);
                results = codeProvider.CompileAssemblyFromSource(options, code);
            }

            if (results.Errors.Count > 0)
            {
                throw new Exception
                          ("Cannot compile typed context: " + results.Errors[0].ErrorText + " (line " + results.Errors[0].Line + ")");
            }
        }
コード例 #4
0
 public ConnectionDialog(IConnectionInfo cxInfo)
 {
     DataContext = _properties = new ConnectionProperties(cxInfo);
     Background  = SystemColors.ControlBrush;
     InitializeComponent();
 }