示例#1
0
        public HttpResponseMessage Get(HttpRequestMessage request)
        {
            Dictionary <string, string> dictionary = BridgeConfiguration.ToDictionary();

            string configResponse = JsonSerializer.SerializeDictionary(dictionary);

            Trace.WriteLine(String.Format("{0:T} - GET config returning raw content:{1}{2}",
                                          DateTime.Now, Environment.NewLine, configResponse),
                            typeof(ConfigController).Name);

            // Directly return a json string to avoid use of MediaTypeFormatters
            HttpResponseMessage response = request.CreateResponse(HttpStatusCode.OK);

            response.Content = new StringContent(configResponse);
            response.Content.Headers.ContentType = new MediaTypeHeaderValue(JsonSerializer.JsonMediaType);
            return(response);
        }
示例#2
0
文件: Program.cs 项目: michlG/wcf
            private bool Parse(string[] args)
            {
                // Build a dictionary of all command line arguments.
                // This allows us to initialize BridgeConfiguration from it.
                // Precedence of values in the BridgeConfiguration is this:
                //   - Lowest precedence is the BridgeConfiguration ctor defaults
                //   - Next precedence is any value found in a specified configuration file
                //   - Next precedence is environment variables
                //   - Highest precedence is a BridgeConfiguration value explicitly set on the command line

                Dictionary <string, string> argumentDictionary = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

                foreach (string arg in args)
                {
                    if (!arg.StartsWith("/") && !arg.StartsWith("-"))
                    {
                        return(false);
                    }

                    // Cannot use split because some argument values could contain colons
                    int    index    = arg.IndexOf(':');
                    string argName  = (index < 0) ? arg.Substring(1) : arg.Substring(1, index - 1);
                    string argValue = (index < 0) ? String.Empty : arg.Substring(index + 1);

                    if (String.Equals(argName, "?", StringComparison.OrdinalIgnoreCase))
                    {
                        return(false);
                    }

                    argumentDictionary[argName] = argValue;
                }

                BridgeConfiguration = new BridgeConfiguration();

                // If the user specified a configuration file, deserialize it as json
                // and treat each name-value pair as if it had been on the command line.
                // But options explicitly on the command line take precedence over these file options.
                string argumentValue;

                if (argumentDictionary.TryGetValue("bridgeConfig", out argumentValue))
                {
                    if (!File.Exists(argumentValue))
                    {
                        Console.WriteLine("The configuration file '{0}' does not exist.");
                        return(false);
                    }

                    // Read the configuration file as json and deserialize it
                    string configurationAsJson = File.ReadAllText(argumentValue);
                    Dictionary <string, string> deserializedConfig = null;

                    try
                    {
                        deserializedConfig = JsonSerializer.DeserializeDictionary(configurationAsJson);
                    }
                    catch (Exception ex)
                    {
                        // Catch all exceptions because any will cause
                        // this application to terminate.
                        Console.WriteLine("Error deserializing {0} : {1}",
                                          argumentValue, ex.Message);
                        return(false);
                    }

                    // Every name/value pair in the config file not explicitly set on the command line
                    // is treated as if it had been on the command line.
                    foreach (var pair in deserializedConfig)
                    {
                        if (!argumentDictionary.ContainsKey(pair.Key))
                        {
                            argumentDictionary[pair.Key] = pair.Value;
                        }
                    }
                }

                // For every property in the BridgeConfiguration that has not been explicitly
                // specified on the command line or via the config file, check if there is an
                // Environment variable set for it.  If so, use it as if it had been on the command line.
                foreach (string key in BridgeConfiguration.ToDictionary().Keys)
                {
                    // If the property is explicitly on the command line, it has highest precedence
                    if (!argumentDictionary.ContainsKey(key))
                    {
                        // But if it is not explicitly on the command line but
                        // an environment variable exists for it, it has higher precedence
                        // than defaults or the config file.
                        string environmentVariable = Environment.GetEnvironmentVariable(key);
                        if (!String.IsNullOrWhiteSpace(environmentVariable))
                        {
                            argumentDictionary[key] = environmentVariable;
                        }
                    }
                }

                // Finally, apply all our command line arguments to the BridgeConfiguration,
                // overwriting any values that were the default or came from the optional config file
                BridgeConfiguration = new BridgeConfiguration(BridgeConfiguration, argumentDictionary);

                // Finish parsing the command line arguments that are not part of BridgeConfiguration
                if (argumentDictionary.ContainsKey("allowRemote"))
                {
                    AllowRemote = true;
                }

                if (argumentDictionary.ContainsKey("ping"))
                {
                    Ping = true;
                }

                if (argumentDictionary.ContainsKey("stop"))
                {
                    Stop = true;
                }

                if (argumentDictionary.ContainsKey("stopiflocal"))
                {
                    StopIfLocal = true;
                }

                string remoteAddresses;

                if (argumentDictionary.TryGetValue("remoteAddresses", out remoteAddresses))
                {
                    RemoteAddresses = remoteAddresses;
                }

                if (argumentDictionary.ContainsKey("reset"))
                {
                    Reset = true;
                }

                return(true);
            }