21

通常、.net コンソールまたは Web アプリケーションから呼び出されるクラス ライブラリがあります。さまざまなコンポーネントと統合され、app.config または web.config に依存します。

スクリプト (IronPython など) からクラス ライブラリを利用したい場合、スクリプトで構成ファイルを利用するにはどうすればよいですか? 理想的には、スクリプトを実行するときに構成ファイルを選択できるようにしたい、または慣例 (スクリプト ファイルと一緒にある構成ファイル) を選択できるようにしたいと考えています。

IronPython の複数のコピーがないと複数の構成に対応できないため、可能であれば ipy.exe.config を変更したくありませんか?

代替案はありますか?

4

6 に答える 6

4

コードサンプルを使用した実用的なソリューションがあります。私のブログを参照してください: http://technomosh.blogspot.com/2012/01/using-appconfig-in-ironpython.html

ConfigurationManager に挿入される特別なプロキシ クラスが必要です。

ConfigurationProxy ライブラリのソースは次のとおりです。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Configuration.Internal;
using System.Xml;
using System.Collections.Specialized;
using System.Reflection;
using System.IO;

namespace IronPythonUtilities
{
    /// <summary>
    /// A custom app.config injector for use with IronPython code that needs configuration files.
    /// The code was taken and modified from the great work by Tom E Stephens:
    /// http://tomestephens.com/2011/02/making-ironpython-work-overriding-the-configurationmanager/
    /// </summary>
    public sealed class ConfigurationProxy : IInternalConfigSystem
    {
        Configuration config;
        Dictionary<string, IConfigurationSectionHandler> customSections;

        // this is called filename but really it's the path as needed...
        // it defaults to checking the directory you're running in.
        public ConfigurationProxy(string fileName)
        {
            customSections = new Dictionary<string, IConfigurationSectionHandler>();

            if (!Load(fileName))
                throw new ConfigurationErrorsException(string.Format(
                    "File: {0} could not be found or was not a valid cofiguration file.",
                    config.FilePath));
        }

        private bool Load(string file)
        {
            var map = new ExeConfigurationFileMap { ExeConfigFilename = file };
            config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);

            var xml = new XmlDocument();
            using (var stream = new FileStream(file, FileMode.Open, FileAccess.Read))
                xml.Load(stream);

            //var cfgSections = xml.GetElementsByTagName("configSections");

            //if (cfgSections.Count > 0)
            //{
            //    foreach (XmlNode node in cfgSections[0].ChildNodes)
            //    {
            //        var type = System.Activator.CreateInstance(
            //                             Type.GetType(node.Attributes["type"].Value))
            //                             as IConfigurationSectionHandler;

            //        if (type == null) continue;

            //        customSections.Add(node.Attributes["name"].Value, type);
            //    }
            //}

            return config.HasFile;
        }

        public Configuration Configuration
        {
            get { return config; }
        }

        #region IInternalConfigSystem Members

        public object GetSection(string configKey)
        {
            if (configKey == "appSettings")
                return BuildAppSettings();

            object sect = config.GetSection(configKey);

            if (customSections.ContainsKey(configKey) && sect != null)
            {
                var xml = new XmlDocument();

                xml.LoadXml(((ConfigurationSection)sect).SectionInformation.GetRawXml());
                // I have no idea what I should normally be passing through in the first
                // two params, but I never use them in my confighandlers so I opted not to
                // worry about it and just pass through something...
                sect = customSections[configKey].Create(config,
                                       config.EvaluationContext,
                                       xml.FirstChild);
            }

            return sect;
        }

        public void RefreshConfig(string sectionName)
        {
            // I suppose this will work. Reload the whole file?
            Load(config.FilePath);
        }

        public bool SupportsUserConfig
        {
            get { return false; }
        }

        #endregion

        private NameValueCollection BuildAppSettings()
        {
            var coll = new NameValueCollection();

            foreach (var key in config.AppSettings.Settings.AllKeys)
                coll.Add(key, config.AppSettings.Settings[key].Value);

            return coll;
        }

        public bool InjectToConfigurationManager()
        {
            // inject self into ConfigurationManager
            var configSystem = typeof(ConfigurationManager).GetField("s_configSystem",
                                            BindingFlags.Static | BindingFlags.NonPublic);
            configSystem.SetValue(null, this);

            // lame check, but it's something
            if (ConfigurationManager.AppSettings.Count == config.AppSettings.Settings.Count)
                return true;

            return false;
        }
    }
}

Pythonからロードする方法は次のとおりです。

import clr
clr.AddReferenceToFile('ConfigurationProxy.dll')

from IronPythonUtilities import ConfigurationProxy

def override(filename):
    proxy = ConfigurationProxy(filename)
    return proxy.InjectToConfigurationManager()

最後に、使用例:

import configproxy
import sys

if not configproxy.override('blogsample.config'):
    print "could not load configuration file"
    sys.exit(1)

import clr
clr.AddReference('System.Configuration')
from System.Configuration import *
connstr = ConfigurationManager.ConnectionStrings['TestConnStr']
print "The configuration string is {0}".format(connstr)
于 2012-01-09T09:08:15.517 に答える
2

System.Configuration.ConfigurationManagerあなたはクラスを見ることができます。より具体的には、OpenMappedExeConfigurationメソッドを使用すると、任意の.configファイルをロードできます。これConfigurationにより、標準のAppSettins、ConnectionStrings、SectionGroups、およびSectionsプロパティを公開するオブジェクトが提供されます。

このアプローチでは、構成ファイルの名前をコマンドライン引数としてスクリプトに渡すか、実行時に.configファイルを選択するためのコードロジックを用意する必要があります。

私はPythonを知らないので、サンプルコードを投稿する試みは控えます。:-)

于 2009-02-26T04:15:02.453 に答える
0

このブログ投稿を Python に翻訳すると、次のように動作するはずです。

import clr
import System.AppDomain
System.AppDomain.CurrentDomain.SetData(“APP_CONFIG_FILE”, r”c:\your\app.config”)
于 2009-02-15T11:36:40.937 に答える
0

構成ファイル内に追加のセクションをいつでも含めることができます。ipy.exe.config ファイルにインクルードを追加して、外部構成設定をインポートできます。myApp.config と言います。

バッチ/コマンド ファイルでは、特定の .config セットをいつでも myApp.config にコピーできるため、必要に応じて別の構成ファイルで実行できます。

これを達成する方法については、このブログをのぞいてみてください。 http://weblogs.asp.net/pwilson/archive/2003/04/09/5261.aspx

于 2009-02-16T16:36:52.517 に答える
0

回避策として、ConfigurationManager 静的クラスの AppSettings コレクションを「手動で」入力したため、PY スクリプトを作成し、IronPython でその「インポート」を実行すると、設定がクラス ライブラリで使用できるようになります。ただし、 ConnectionStrings コレクションに値を割り当てることができました:(

私のスクリプトは次のようになります

import clr
clr.AddReferenceToFileAndPath(r'c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.configuration.dll')
from System.Configuration import *
ConfigurationManager.AppSettings["settingA"] = "setting A value here"
ConfigurationManager.AppSettings["settingB"] = "setting B value here"

ただし、カスタム .config ファイルを ConfigurationManager クラスに「ロード」する方法を知っていると便利です。

于 2010-01-08T21:59:10.170 に答える