【问题标题】:Can I access web.config with an XBAP?我可以使用 XBAP 访问 web.config 吗?
【发布时间】:2011-09-23 15:27:34
【问题描述】:

我正在将 Silverlight 4 应用程序移植到 WPF/XBAP。该应用程序使用 web.config 应用程序设置参数中使用 asp 初始化的 initParams。

与 Silverlight 不同,WPF 在 StartupEventArgs 上缺少 InitParams 属性。

这似乎是BrowserInteropHelper 可以帮助我做的事情,但我什么也没看到。

有没有办法在启动时从 web.config 访问配置参数到应用程序?

【问题讨论】:

    标签: xbap


    【解决方案1】:

    我的解决方法是向托管 xbap 的网站添加一个 Web 服务,并在应用启动时调用它,使用 BrowserInteropHelper.Source 来确定端点地址的 uri。

    public class ConfigService : IConfigService
    {
        public WebConfiguration GetWebConfig()
        {
            var outDict = new Dictionary<string, string>();
            foreach (string key in WebConfigurationManager.AppSettings.AllKeys)
            {
                outDict.Add(key, WebConfigurationManager.AppSettings[key]);
            }
            var webconfig = new WebConfiguration();
            webconfig.AppSettings = outDict;
            return webconfig;
        }
    }
    
    [DataContract]
    public class WebConfiguration
    {
        [DataMember]
        public Dictionary<string, string> AppSettings { get; set; }
    }
    

    客户是这样称呼它的:

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        try
        {
            var b = new System.ServiceModel.BasicHttpBinding();
    
            string url;
    
            // when running xbap directly in browser the port is -1
            if(BrowserInteropHelper.Source.Port != -1)
            {
                url = String.Format("http://{0}:{1}/ConfigService.svc",
                BrowserInteropHelper.Source.Host,
                BrowserInteropHelper.Source.Port);
            }
            else
            {
                url = @"http://localhost.:51007/ConfigService.svc";
            }
            var address = new System.ServiceModel.EndpointAddress(url);
            SDDM3.ConfigServiceReference.ConfigServiceClient c = new ConfigServiceClient(b, address);
            c.GetWebConfigCompleted +=new EventHandler<GetWebConfigCompletedEventArgs>(c_GetWebConfigCompleted);
            c.GetWebConfigAsync(url);
            this.MainWindow.Content = new UserControl1();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    void c_GetWebConfigCompleted(object sender, GetWebConfigCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            // MessageBox.Show(e2.Result.
            m_AppSettings = e.Result.AppSettings;
            MessageBox.Show("got appsettings: " +  e.Result.AppSettings.Count.ToString());                
            this.MainWindow.Content = new Page1();
        }
        else
        {
            string msg;
            if (e.UserState != null)
                msg = String.Format("Unable to get config from: \n{0} \n{1}", e.UserState, e.Error.Message);
            else
                msg = String.Format("Unable to get config: \n{0}", e.Error.Message);
            MessageBox.Show(msg);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-11-26
      • 2011-12-15
      • 2013-02-02
      • 1970-01-01
      • 1970-01-01
      • 2014-02-08
      • 2017-11-11
      • 2016-06-05
      • 1970-01-01
      相关资源
      最近更新 更多