Tuesday, May 18, 2010

It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level

Error: "It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS"

This error really drived me crazy. Just can't figure out what exactly went wrong. When I double clicked on this error message it took me to web.config and pointed the line "<authentication mode="Windows"/>. What's wrong in that? the syntax is all correct.

Later I found that I have created an application inside the root application and all it was like a folder(root application) and subfolder(my second application). What was happening in the background was the authentication which was already defined in the root applications web.config file was not ready to be overridden in my second applications web.config and that was the problem.

Later I moved out my second application and placed like a root folder and it solved my problem.

Friday, March 26, 2010

CrossDomainPolicyIssue

While running the silverlight application for the first time we tend to face cross-domain policy issue. The error message during the debug mode is show below.
The fix for the above issue is to create two xml files in WWWRoot folder where IIS is hosted. Those two files are 1. clientaccesspolicy.xml and 2. crossdomain.xml

clientaccesspolicy.xml content is shown below


crossdomain.xml content is shown below


Even after adding the above steps, silverlight application fails, then check the Multipurpose Internet Mail Extension (MIME). We have to include the following MIME extenstion.


Friday, October 2, 2009

CallingSilverlightFromASPXPage

While the page load and to populate silverlight control, in Page_Load() of .aspx following has to be done.
multiFileUploaderSilverlightControl.InitParameters = string.Format( "FileName={0},SnapshotImage1={1}", "MultiFileUploader", snapshotImage);

 

To access the values from the above passed method, in the .xaml page’s constructor,


public MultiFileUploader(IDictionary<string, string> param)
{
   InitializeComponent();
   HtmlPage.RegisterScriptableObject(param["FileName"], this);
   if (param.Count > 0)
   {
      _snapShotImage1 = param["SnapshotImage1"];
   }
}
To call a specific usercontrol from the silverlight application, the name has to set in the InitParameters, in this case “MultiFileUploader” and this will be checked in the App.xaml.cs as shown below,
private void Application_Startup(object sender, StartupEventArgs e)
{
  if (e.InitParams != null)
  {
     foreach(var item in e.InitParams)
     {
         Resources.Add(item.Key, item.Value);
     }
  }
   if (Current.Resources.Contains("FileName"))
   {
      fileName = Convert.ToString(Current.Resources["FileName"]);
   }
   if (!string.IsNullOrEmpty(fileName))
   {
      switch (fileName)
      {
         case "MultiFileUploader":
            this.RootVisual = new MultiFileUploader(e.InitParams);
            break;
      }
   }
}

 
To call a method from .aspx page to .xaml(silverlight) page, the following has to be done.
First decorate the class in the xaml page with the attribute [ScriptableType] as
[ScriptableType]
public partial class MultiFileUploader : UserControl
{   }
and decorate the method with the attribute [ScriptableMember]

[ScriptableMember]
public void UploadFiles(string param)
{   }

so that this method can be called from .aspx’s javascript method as shown below
var silverligthControl = document.getElementById('multiFileUploaderSilverlightControl'); silverligthControl.Content.MultiFileUploader.UploadFiles(param);
where, multiFileUploaderSilverlightControl is silverlight control added in .aspx page and in the next line we have called
SilverlightControlName.Content.ClassName.MethodName(parameter)

To call a method from .xaml page to .aspx page, the following has to be done.
HtmlPage.Window.Invoke("ValueFromMultiFileUploaderXaml", param);
In the above line we invoke ValueFromMultiFileUploaderXaml Javascript method which is in .aspx page

To access the values passed from the above javascript method call,

function ValueFromMultiFileUploaderXaml(fileDetails)
{
   if (fileDetails.length > 0)
   {
      var param = fileDetails[0];
   }
}

Thursday, September 17, 2009

AccessingQuerystringFromJavascript

I recently came across a situation to access query string from javascript.

Here is the method to access query string keys and values.
Say for this URL http://www.testsite.co.in/?Mode=ADD
the following method,

var queryString = window.location.search.substring(1);

fetches querystring.

Now the variable 'queryString' will have "Mode=ADD".

Thursday, August 6, 2009

ControlPopulationError

I added UIElements whose value is maintained across the application level, inside the Stack panel container control which is in Page_1.xaml. After leaving from Page_1.xaml to Page_2.xaml and again navigating to Page_1.xaml caused me an error while populating the global content to the stack panel. The error I got is,

"Specified element is already the logical child of another element. Disconnect if first."

The error was due to, at the first time population of stack panel went happily

downloadStackPanel.Children.Add(videoDownloadStatus);

but at the second time when I tried to populate the global content to the stack panel, the previous instance of stack panel still holds the global content and prevented me to populate the same content further and caused an exception.

I tried lot many workarounds for populating the stack panel the second time and later found the solution to be too simple.

In Page_1.xaml I created Closing = "Window_Closing" event. This event will be fired when the current window is closed. In this event I cleared the stack panel

downloadStackPanel.Children.Clear()

so no more global content will be there before I go to new window(Page_2.xaml). Now if I go back to the Page_1.xaml the population of stack panel will happen with no exception as the contents are already cleared.

Friday, March 6, 2009

DebuggerDisplayAttribute

During debugging sometime we fed up to see value of a property if it is deep inside a tree structure. We have to keep on click the “+” sign to expand the tree to reach our deep inside located property. On frequent debug it becomes a hectic task. DebuggerDisplayAttribute comes as a survivor.

DebuggerDisplayAttribute Class determines how a class or field is displayed in the debugger variable windows. This attribute has a single argument: a string to be displayed in the value column for instances of the type. This string can contain braces ({ and }). The text within a pair of braces is evaluated as the name of a field, property, or method. For example, the following C# code causes "MyTechnology = “.NET” " to be displayed when the cursor is placed on family variable.


Here I need to check the value assigned to Technology property frequently, so I declared that property in DebuggerDisplayAttribute. Futher the class containing that property should be decorated by DebuggerDisplayAttribute.

This attribute can be applied to the following:
Classes
Structures
Delegates
Enumerations
Fields
Properties
Assemblies


The picture is shown below.


Tuesday, February 10, 2009

Extension method

Extension method is a new feature available in .NET 3.0 framework. Extension Method enable a variety of useful scenarios, and help make possible the really powerful LINQ query framework that is being introduced with .NET as part of the VS 2008 / .NET 3.5 release. The usage is shown in the below snapshot.



Beware to simply insert the this keyword before the int argument in the Double() helper method, which make the extension method possible, then we can indeed type side.Double(). In fact, the advantage of doing this is that intellisense will help you by showing the method in the dropdown when you type a dot after the variable and also makes something more readable. The extension method should be static and class as well.
However extension method should be avoided in cases where encapsulation breaks.