Thursday, February 09, 2012

Deploy Silverlight UI with all supporting files into Window Azure


Deploy Silverlight UI with all supporting files into Window Azure

Normally when you deploy your Silverlight 5.0 project you might have couple of supporting files like DLL’s, Images, .zip etc.

And when you deploy your project into on Premises (Normal Servers) then you normally deploy all your supporting files into deployment servers and your Silverlight application read that files from there with Relative path option.

But the case will be little different when you deploy same Silverlight 5.0 application into Window Azure (Howto create and deploy Silverlight application into Window Azure), all the supporting files/folders you cannot deploy into the Window Azure as same you normally do it in on premises.
There are two options to do the things
  1. You can utilized Window Azure blob storage to store all your supporting files/folders, Upload all your files into Window Azure Blob and change in your Silverlight code to read that files/folders from blob. You can see couple of links to understand ‘Howto upload/download files into Window Azure Blob’ 
  2. Second option if you don’t want to use Blob storage, you can combine into same package (Window Azure Package) and you are good to go, but for doing this you need to do following changes into your Silverlight1.Web Project  

    • Move all your supporting files/folders from SilverlightUX.proj to Silverlight.Web.proj
    • Change your Silverlight code to read files/folders from new path you changed
    • Go to the file, right click and open the properties window and set properties as below
                      (For XML, .zip, .txt etc. files)

      • Build Action  à Content
      • Copy to Output Directory à Do Not copy
                       (For Ref DLL's)
      • Copy Local à True

Now all Silverlight supporting files will be bind with Window Azure package and no need to do any configuration changes require.

Useful links

Wednesday, February 08, 2012

How to read Config file value deployed into Window Azure from Silverlight 5.0 code.


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> 

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.

Friday, February 03, 2012

Cannot find 'ServiceReferences.ClientConfig' in the .xap application package. This file is used to configure client proxies for web services,......


Error: Cannot find 'ServiceReferences.ClientConfig' in the .xap application package. This file is used to configure client proxies for web services, and allows the application to locate the services it needs. Either include this file in the application package, or modify your code to use a client proxy constructor that specifies the service address and binding explicitly.

I received above error when I tried to write some code in App.XML file, I found link below exactly same I was doing, I solved this error from below link


Thursday, February 02, 2012

Window Azure Error: One of the request inputs is out of range

Window Azure Error: One of the request inputs is out of range


I got his error when I was working with window azure blob Storage code

It's simple code but I got this error and not able to figure why, then I compare the code from MSDN only different I found the input parameter going with one upper case letter. Don't know what is the logic not accepting upper case character but when I change all as lower case then this error resolved.


C# Code

 // create blob container for images

blobStorage = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobStorage.GetContainerReference("Mypics");
container.CreateIfNotExist(); 


Error in above code
  • 'MyPics' string is wrong here changed to "mypics"

Some more observations

  • 'my pics" is wrong, No Space allowed
  • 'my_pics" is wrong, No Special character allowed
  • 'mypics123' is correct, Numbers are allowed

Above rules will be applicable to other methods too like

GetQueueReference();
GetBlockBlobReference();

Monday, January 30, 2012

How to call WCF services methods from Silverlight Application

How to call WCF services methods from Silverlight Application

Here I am giving very basic idea for beginner who wants to know how we can call WCF service from Silverlight application. Calling WCF service from Silverlight uses different types of binding (basicHttpBinding), if you use RIA service then you need not to think binding configuration, it will take care automatically.

<client>
   <endpoint address="http://localhost:13513/Service1.svc" binding="basicHttpBinding"
    bindingConfiguration="BasicHttpBinding_IService1" contract="WcfService1.IService1"
    name="BasicHttpBinding_IService1" />
</client>
  
Follow the steps to create first “HelloWorld” solution  

1.     Create Simple WCF service project “WcfService1” in solution “HelloWorld.sln”
2.     Add “clientaccesspolicy.xml” and “crossdomain.xml” xml file into WCF project “WcfService1”
3.     Add one Silverlight project “SilverlightApplication1” into same solution “HelloWorld.sln”
4.     “SilverlightApplication1.Web”will get added automatically
5.     Set “SilverlightApplication1” project as startup project
6.     Now right click on “SilverlightApplication1” project and “Add service Reference” to “WcfService1”
7.     Now right click on “WcfService1” reference and click on Update Proxy
8.     Run the application

After creating project it will looks like as below screen shot

  
Source Code

C# Code in “IService1.cs”
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfService1
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetData(int value);
    }

C# Code in “Service1.svc.cs” file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfService1
{
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }
    }
 }

C# Code in “MainPage.XMAL”
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightApplication1
{
    public partial class MainPage : UserControl
    {
        //Declare WCF proxy ref variable
        WcfService1.Service1Client webClient;

        public MainPage()
        {
            InitializeComponent();
            //Initialise WCF proxy variables
            webClient = new WcfService1.Service1Client();
            //Registred WCF complatedevent
            webClient.GetDataCompleted += new EventHandler<WcfService1.GetDataCompletedEventArgs>(ServiceCloud_GetDataCompleted);
        }

        //On button click event will call WCF service Async method to get data
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //Call WCF method
            webClient.GetDataAsync(30);
        }

        //once WCF service call completed then "e.Result" will give you return data from WCF call
        //you need to wait until WCF call complated and return value
        void ServiceCloud_GetDataCompleted(object sender, WcfService1.GetDataCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                //if WCF return data successfully then data will return into e.Result
                label1.Content = e.Result.ToString();
            }
            else
            {
                //other wise error message will be returned
                label1.Content = e.Error.Message.ToString();
            }

        }
    }
} 
Troubleshoots
  1. You might get error related to “Crossdomain” policy, if you don’t place “clientaccesspolicy.xml” and “crossdomain.xml” into “WcfService1” project, after changing anything/adding these .xml files you have to update the proxySee here to create these files: http://riteshkk2000.blogspot.com/2011/01/attempting-to-access-service-in-cross.html
  2. Internet explorer may ask to install Silverlight runtime even this already installed into your machine. you need to register local URL on the “Restricted Site”See more here to solve:    http://riteshkk2000.blogspot.com/2011/01/asking-to-install-silverlight-4-when.html
  3. If you need more and more details with pictorial view then good site to go through http://blogs.msdn.com/b/avkashchauhan/archive/2011/11/29/silverlight-front-end-calling-to-wcf-service-all-in-one-windows-azure-web-role-sample.aspx