6

app.config からアプリケーションのサービスの価値を取得しようとしています。URLを表示するアプリケーションに送信する必要があります。このアプリケーションで使用している Web サービスも使用しているため、appSettings に移動できません。

この値 ' http://192.168.4.22:82/Service.asmx ' を C# コードで取得したいと考えています。

<applicationSettings>
    <SDHSServer.Properties.Settings>
      <setting name="DOServer_WebReference1_Service" serializeAs="String">
        <value>http://192.168.4.22:82/Service.asmx</value>
      </setting>
    </SDHSServer.Properties.Settings>
  </applicationSettings>
4

4 に答える 4

8

よくわからない質問ですが、

string s = SDHSServer.Properties.Settings.DOServer_WebReference1_Service;

あなたにそれを手に入れます

于 2012-02-17T14:30:19.443 に答える
4

ASP.Net 4.0サイトでこのコードを使用して、「applicationsetting」セクションからセクションデータを引き出します。

public sealed class SiteSupport {


    /// <summary>
    /// Retrieve specific section value from the web.config
    /// </summary>
    /// <param name="configSection">Main Web.config section</param>
    /// <param name="subSection">Child Section{One layer down}</param>
    /// <param name="innersection">Keyed on Section Name</param>
    /// <param name="propertyName">Element property name</param>
    /// <returns></returns>
    /// <example>string setting = NoordWorld.Common.Utilities.SiteSupport.RetrieveApplicationSetting("applicationSettings", "NoordWorld.ServiceSite.Properties.Settings", "ServiceWS_SrvWebReference_Service", "value")</example>
    public static string RetrieveApplicationSetting(string configSection, string subSection, string innersection, string propertyName) {
        string result = string.Empty;
        HttpWorkerRequest fakeWorkerRequest = null;
        try {
            using (TextWriter textWriter = new StringWriter()) {
                fakeWorkerRequest = new SimpleWorkerRequest("default.aspx", "", textWriter);
                var fakeHTTPContext = new HttpContext(fakeWorkerRequest);
                Configuration config = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap() { ExeConfigFilename = fakeHTTPContext.Server.MapPath(@"~/Web.config") }, ConfigurationUserLevel.None);
                ConfigurationSectionGroup group = config.SectionGroups[configSection];
                if (group != null) {
                    ClientSettingsSection clientSection = group.Sections[subSection] as ClientSettingsSection;
                    if (clientSection != null) {
                        SettingElement settingElement = clientSection.Settings.Get(innersection);
                        if (settingElement != null) {
                            result = (((SettingValueElement)(settingElement.ElementInformation.Properties[propertyName].Value)).ValueXml).InnerText;
                        }
                    }
                }
            }
        } catch (Exception ex) {
            throw ex;
        } finally {
            fakeWorkerRequest.CloseConnection();
        }
        return result;
    }
}

https://www.ServiceWS.com/webservices/Golf

于 2013-03-14T18:35:33.003 に答える
4

私が正しく理解している場合、2 つの Visual Studio C# プロジェクトがあります。1 つ目 (プロジェクト A) には、2 つ目 (プロジェクト B) でアクセスしたい設定があります。そのためには、次の手順を実行する必要があります。

  • プロジェクト B からプロジェクト A への参照を追加する

  • 設定 i プロジェクト A のアクセス修飾子を public に変更します (デフォルトは internal です)

    Visual Studio 設定エディター

  • これで、完全修飾名を使用して、プロジェクト B の設定にアクセスできるようになりました。SDHSServer.Properties.Settings.Default.DOServer_WebReference1_Service

設定エディタで、設定の値を設定できることに注意してください。これは設定のデフォルト値であり、この値はApp.configプロジェクトのファイルにも保存されます。App.configただし、実行中のアプリケーションのファイルに別の値を指定することで、この値をオーバーライドできます。

この例では、App.configプロジェクト A のファイルには、設定の値が含まれますhttp://192.168.4.22:82/Service.asmx。ただし、App.configプロジェクト B のファイルでこれをオーバーライドして、別の値を取得できます。それはおそらくあなたがしたいことではありませんが、これを認識しておく必要があります。

于 2012-02-17T14:32:05.460 に答える
0

このようなものに依存します。

  var s = SDHSServer.Properties.Settings.Default.DOServer_WebReference1_Service;

また

  var s = SDHSServer.Properties.Settings.DOServer_WebReference1_Service;
于 2014-09-16T17:44:24.310 に答える