Thursday, August 16, 2012

How to use Web Config Files in Silverlight

How to use Web Config Files in Silverlight

Im trying to use my web Config files in Silverlight.

I've put the following in web.config:

<configuration>   <appSettings>     <add key="FileHeader" value="file://***.com/Builds/"/>     <add key="WebHeader" value="http://***.com/dev/builds"/>       </appSettings> 

Im trying to use them like

string temp= System.Configuration!System.Configuration.ConfigurationManager.AppSettings.Get("FileHeader"); 

However it does not work, it gives an error "Only assignment, calls, increment, decrement...can be used as a statement"

Answers & Comments...

Answer: 1

No can do because the configuration namespace does not exist in the SL .NET Framework, but what you can do is this:

public static string GetSomeSetting(string settingName)         {             var valueToGet = string.Empty;             var reader = XmlReader.Create("XMLFileInYourRoot.Config");             reader.MoveToContent();              while (reader.Read())             {                 if (reader.NodeType == XmlNodeType.Element && reader.Name == "add")                 {                     if (reader.HasAttributes)                     {                         valueToGet = reader.GetAttribute("key");                         if (!string.IsNullOrEmpty(valueToGet) && valueToGet == setting)                         {                             valueToGet = reader.GetAttribute("value");                             return valueToGet;                         }                     }                 }             }              return valueToGet;         } 
Answer by Cat Man Do for How to use Web Config Files in Silverlight

Answer: 2

You cannot read web.config from your Silverlight application, because the Silverlight application runs on the client (in the browser) and not on the server.

From your server code you can access app settings with

string temp = Configuration.ConfigurationManager.AppSettings["FileHeader"]; 

but you have to send them to the client. You could do that by using InitParams

<param name="initParams" value="param1=value1,param2=value2" /> 

Within your server code (Page_Load of Default.aspx) you could loop through all AppSettings and create the value for the initParams dynamically.

In the Silverlight Application you can access the parameters in the Application_Startup event:

private void Application_Startup(object sender, StartupEventArgs e)  {               this.RootVisual = new Page();    if (e.InitParams.ContainsKey("param1"))       var p1 = e.InitParams["param1"]; } 

or loop through all parameters and store them in a configuration dictionary. Like this you have your app settings in the Silverlight application on the client.

Answer by slfan for How to use Web Config Files in Silverlight

Answer: 3

Configure Silverlight 3 Applications using the Web.config File from ASP.NET
http://www.codeproject.com/Articles/49490/Configure-Silverlight-3-Applications-using-the-Web

Answer by zclmoon for How to use Web Config Files in Silverlight




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog