Tanzim Saqib on .NET discovery

January 25, 2008

HttpRequestFactory vs. XMLHttpRequest in Volta

Filed under: .NET, C#, Volta — tanzimsaqib @ 11:09 pm

HttpRequestFactory was designed for use by tiersplitting internally and was not supposed to be exposed as part of the Volta API as Danny van Velzen from Microsoft Volta team told me today. So, its better if you use XMLHttpRequest instead because this factory class might not show up in the later releases. You will find this class in Microsoft.LiveLabs.Volta.Xml namespace.  As like as JavaScript’s one, in this .NET version you can also Open URL, specify method name, and of course pass credentials. You can track response text, xml, status code, status text and also you can abort.

To retrieve your content, you must subscribe to ReadyStateChange event with a HtmlEventHandler which you can find in Microsoft.LiveLabs.Volta.Html namespace and check the status code. If it is 200 that means “HTTP OK”, you can take the ResponseText or ResponseXML. See this example:

string content = string.Empty;
var request = new XMLHttpRequest();

request.ReadyStateChange += delegate()
{
    if (request.Status == 200)
        content = request1.ResponseText;
};

request.Open("POST", "http://tanzimsaqib.com/feed/", true);

However, you cannot fetch cross domain content by XMLHttpRequest. The Volta compiler creates client side JavaScript XMLHttpRequest and lets developers write code in .NET friendly way. So, I do not think there is any way to retrieve cross domain content in Volta, and leaving us on the same old HttpRequest class.

January 18, 2008

[New Article] Building a Volta Control : A Flickr Widget

Filed under: .NET, C#, Volta — tanzimsaqib @ 9:36 pm

This is my first article which is based on the first CTP of Volta considering its current limitations. You will see how you can create a Volta control that the compiler can convert into an AJAX Widget without requiring us writing a single line of JavaScript code: http://dotnetslackers.com/articles/aspnet/BuildingAVoltaControlAFlickrWidget.aspx

January 15, 2008

How to solve: Server Controls can’t be accessed in View’s code-behind in ASP.NET MVC

Filed under: .NET, MVC — tanzimsaqib @ 12:19 am

It’s still long way to go for a final release of ASP.NET MVC, the one I’ve been using right now is just a December CTP. But, like me you might be experiencing this confusing problem. The server controls that you put in a View (ViewContentPage) can not be found in code-behind page. The reason behind it is – the Views don’t have a back-end designer code file. I believe it’s just a bug or they could not find time to fix/look into it. I’m sure it will be fixed in any of the upcoming versions.

To enable this, switch to Solution Explorer, right click on the View you are interested in, and choose Convert to Web Application. Now, you will find the server controls in code-behind file.

January 3, 2008

Make HTML controls discoverable in Volta Control

Filed under: .NET, C#, Volta — tanzimsaqib @ 2:12 pm

 

When a Volta control is rendered, the ID attribute of the generated HTML is changed to something like _vcId_1_DivName which is inconvenient to find from code. But the ID attribute stays the same in case of Volta Page, so it is discoverable by ID like this:

Div divContent = Document.GetById<Div>("divContent");

However, if you add HTML controls to the control like the following, the ID is not changed during the rendering:

public VoltaControl1() : base("VoltaControl1.html")
{
    InitializeComponent();

    Button btnClick = new Button();
    btnClick.InnerText = "Click!";
    btnClick.Id = "btnClick";
    this.Add(btnClick);
}

If you don’t prefer this way and seriously want to write your own HTML in the control’s html page, you might find the following snippet useful. But, remember in this case you will use name attribute of the html element instead of ID.

// Usage: var element = GetElementByName(Document.GetElementsByTagName("div"), "divWidget");
private HtmlElement GetElementByName(HtmlElementCollection elements, string name)
{
    foreach (var element in elements)
    {
        DomAttribute nameAttribute = element.Attributes.GetNamedItem("name");
        if (nameAttribute != null)
            if (nameAttribute.Value == name)
                return element;
    }

    return null;
}

January 2, 2008

Making cross domain AJAX call using Volta

Filed under: .NET, C#, Volta — tanzimsaqib @ 12:56 pm

 

Making a cross domain AJAX call in Volta is piece of cake. Volta compiler generates necessary client codes to make it work. Here is a snippet that can make an AJAX call to some Url and fetch data:

public void DownloadPhotos()
{
    IHttpRequest request = HttpRequestFactory.Create();
    request.AsyncSend("POST", URL, string.Empty,
        delegate(string response)
        {
            OnPhotosLoaded(new PhotosLoadedEventArgs(response));
        });
}

Both IHttpRequest and HttpRequestFactory classes can be found in the Microsoft.LiveLabs.Volta.MultiTier namespace. AsyncSend method performs the asynchronous call and calls back the delegate defined where OnPhotosLoaded event is fired to notify the subscriber of this event that the data has just arrived.

December 3, 2007

Anonymous types: var

Filed under: .NET — tanzimsaqib @ 9:19 pm

Anonymous types are excellent addition to .NET “Orcas” release. These are a convenient language feature that allows developers to write code without bothering about what particular type of object they are dealing with. Anonymous types are declared by var keyword which you most of the times will see in different application of LINQ. It’s true it great use with LINQ, but it’s not a LINQ feature at all – it’s a new language feature.

A very simple use of Anonymous types is (starting with a keyword var):

var helo = 123;

You can assign whatever type you like. But, unlike JavaScript once you assigned one type of value, you cannot assign to other types like this:

var helo = 123;
helo = "hello";

Again, unlike JavaScript, you cannot left a declaration unassigned. Do not ever mix up with JavaScript’s var and .NET’s one.

var helo;   // It won't compile

Take a look at variety of usage of Anonymous types. Enjoy the powerful Visual Studio’s great intellisense:

var helo = 123;
Console.WriteLine(helo.Equals(123));

var hello = "hello";
Console.WriteLine(hello.Length);

var helloWorld = new List<string>() { "hello", "world" };
helloWorld.ForEach(delegate(string x) { Console.WriteLine(x); });

If we consider the performance issue, there’s no extra footprint on CLR for Anonymous types. Before it goes to CLR’s hands, vars are replaced with appropriate type so there’s no extra effort for CLR to understand the types:

var hello = "hello";    // exactly same as: string hello = "hello";

Anonymous types are not only appropriate for lazy developers like me, but also have great effective usage in LINQ. I’ll cover that one may be later sometime.

March 12, 2007

Setting up PHP in Windows using Abyss server

Filed under: .NET, Abyss, PHP — tanzimsaqib @ 12:25 pm

Reason behind installing another server to your system
Whichever platform you have been working with must using a server. For .NET developers, IIS eases all the pain. So, what is now if you must install PHP in your system side by side .NET. I suggest you not to install PHP in the same server the .NET is working in. So, install another server which will work with all burdens of PHP stuffs.

Why Abyss?
The reasons behind choosing this server is Abyss is free, easy to configure, very lightweight, and small to download at only 344KB. It also has a preconfigured PHP version for them with all official extensions.

Step by Step
1. Download Abyss server, and install with default settings. Download and install preconfigured PHP version for Abyss
2. Hit this URL in your browser: http://127.0.0.1:9999/console/language
3. Choose your preferred language
4. Enter your chosen user name and password for the server, and click OK.
5. It will then ask your user name and password again, enter the credential and press OK. You will see the following screen:

clip_image002

6. Click on “Configure” from the Hosts table. You will find the following screen:

clip_image002[5]

7. Click on “Scripting Parameters”.
8. You will find the following Interpreters table, click “Add”:

clip_image002[7]

9. Set the Interface drop down to: FastCGI (Local – Pipes).
10. In interpreter field set the path: C:\Program Files\PHP5\php-cgi.exe if you have installed PHP in C:\ Program Files
11. Set Type drop down to PHP Style.
12. Make sure “Use the associated extensions to automatically update the Script Paths” is checked.
13. Click “Add” in Associated Extensions table:

clip_image002[9]

14. Type php in the extension field and pesss OK. Press two more OK.
15. Click on “Restart” to restart the server so that configuration changes can take effect.
16. If you have installed Abyss Server in C:\Program Files, we will have to deploy your web files inside C:\Program Files\Abyss Web Server\htdocs to make it work.
17. Your server address will be http://localhost:8000/

« Newer Posts

Blog at WordPress.com.