How to read Config file value deployed into Window Azure from Silverlight 5.0 code.
If you are planning to move your Silverlight application into Window Azure and you have some configuration value which will be used on your application at runtime then you have to read config file value in different way.
If you have normal Silverlight application then it is simple to read value from Web.config file but if you moved your Silverlight application into window Azure then Web.config file cannot be used on Cloud.
In that case you have to call window Azure config file (‘.cscfg’) file from SilverlightTest.aspx file and pass into App.xmal so that it can be used in application. see below picture
Solution Structure
Please follow the steps to create demo for this
Steps-1
Create the Project Solution and add Silverlight project and Window Azure Project on it.
Steps-2
Go to Project: SilverlightApplication1.Web.proj and Add reference from
$\Program Files\Windows Azure SDK\v1.6\ref\Microsoft.WindowsAzure.ServiceRuntime.dll
Steps-3
Go to Project: SilverlightApplication1.Web.proj and Open Web.Config file and add following lines into it
<appSettings>
<add key="GetFilePath" value="Get value from On Premises Config file"/>
appSettings>
Steps-4
Go to Project: WindowsAzureProject.ccproj and Open ServiceDefinition.csdef file and add following lines into it.
<ConfigurationSettings>
<Setting name="GetFilePath"/>
ConfigurationSettings>
ConfigurationSettings>
Steps-5
Go to Project: WindowsAzureProject.ccproj and open ServiceConfiguration.Cloud.cscfg and ServiceConfiguration.Local.cscfg and add following lines into it
<ConfigurationSettings>
<Setting name="GetFilePath" value="Get value from Window Azure Config file"/>
ConfigurationSettings>
Steps-6
Go to Project: SilverlightApplication1.Web.proj and SilverlightApplication1TestPage.aspx files and add following lines into it
<%@ Import Namespace="Microsoft.WindowsAzure.ServiceRuntime" %>
<param name="Initparams" value="ConfigKey1=<%=RoleEnvironment.IsAvailable ?RoleEnvironment.GetConfigurationSettingValue("GetFilePath") :ConfigurationManager.AppSettings["GetFilePath"]%>, ConfigKey2="Text to test"" />
Note: IF Project has been deployed into On Premises then read value from Web.Config file, IF Project has been deployed into Window Azure then read value from ServiceConfiguration.Cloud.cscfg and ServiceConfiguration.Local.cscfg files.
Steps-7
Go to Project: SilverlightApplication1.proj and Open App.xaml.cs file and on the Application_Startup() event write following code into it
private void Application_Startup(object sender, StartupEventArgs e)
{
//initialize InitParam value
IDictionary<string, string> initParams = e.InitParams;
//Read value from config
string valueFromConfigKey1 = initParams["ConfigKey1"].ToString();
string valueFromConfigKey2 = initParams["ConfigKey2"].ToString();
}
Steps-8
Set the Startup project as “WindowsAzureProject.ccproj” and put the break point into Application_Startup() event You will get the value into “valueFromConfigKey1” and 2 variable are
valueFromConfigKey1 = “Get value from Window Azure Config file”
valueFromConfigKey2 = “ Text to test ”
Steps-9
Set the Startup project as “SilverlightApplication1.Web.proj” and put the break point into Application_Startup() event You will get the value into “ valueFromConfigKey1” and 2 variable are
valueFromConfigKey1 = “Get value from On Premises Config file”
valueFromConfigKey2 = “ Text to test ”
Note: you can add multiple value into "Initparams" in same way as above.
No comments:
Post a Comment