Jun 01 2009

BING is live, the msn search is gone.

Category: BINGRobert @ 5:08 pm

Watched the live video of the BING, I really liked the idea about the categorized search result. Also found that http://search.msn.com is already redirected to the http://www.bing.com

 

Also noticed a unofficial definition of the BING  — :-)  "BING = But its not Google"

It’s interesting that Microsoft is taking the social media like Twitter to do the release announcement as well.


Apr 12 2009

WCF RESTFul Service

Category: Asp.Net, RESTRobert @ 12:15 am

Creating a RESTFul web service in WCF can be easier than a normal web service!? Some of my friends did not believe and they even argued that REST is not considered as a web service.So I figured it out by using the http://en.wikipedia.org/wiki/Web_service . Thanks wikipedia!

With the installation of the WCF REST Service Start Kit , the RESTFul web service can be created by using a wizard and you can create multi types of service as well.Before you install this start kit, you have to install the visual studio 2008 sp1 and .net framework 3.5 sp1.

If you had the experience with the WCF web service, you have to live with the service.svc. I HATE the .svc extension. It’s OK if we create the normal web service, but if it’s for a RESTFul web service, only this name convention will kill the idea. This can be resolved easier under IIS 7 by creating a URLrewrite module,YES, it’s easy (OK, not hard) and cool. But if you have to live under IIS 6, what you need to do?!

Thanks God and please Hate MS, there is one great solution for it.You can go to the Ionic’s Isapi Rewrite Filter and start to use it .It’s a great open source project and hosted at codeplex.com. It support the regular expression to define the rewriting rules. I used it in my projects and did not find any problems.

That’s it and hope you will like the new RESTFul web service world.


Apr 04 2009

LINQ to Flickr support for Photo search

Category: LINQRobert @ 11:01 pm

 

Long time ago, I had one post said that the NFlickr is going to support the LINQ , because my new baby was born at last Dec and I could not get any chance until yesterday. So NOW I will announce that the first version of the LINQ to Flickr is ready for the Alpha test.

To search your photos can be as simple as this

 

   1:   PhotoList list = new PhotoList(service);
   2:              var photos = from photo in list
   3:                                where photo.TagName == "Yang" && photo.Family == true 
   4:                                select photo;

 

Of course, you need to initialize the service and you have to follow the Flickr development document. But it’s already done in NFlickr :)

In the next few posts, I will share the experience with the LINQ to Flickr development. Hopefully baby will agree and give me more time :)


Nov 18 2008

The problem of JSON and how to fix it(Update with code sample)

Category: AJAX, Asp.NetRobert @ 2:13 pm

In AJAX world, JSON is one of the most popular implementation, Asp.Net AJAX is using this concept, and it really works great.

But in some very special situation, JSON could cause some unexpected problems.For example, if the user want to submit a huge document on the local to the server side, you will find it takes too much time before the server-side gets requested.Why?

We knew that one task for the so-call AJAX framework is to serialize the data and on the server-side to de-serialize the data. But if the data is huge, then the serialization itself will take a while.

If the server only needs the body of the huge document, why do we need to serialize the client -side data? In this case, we don’t need the serialization at all and we can use the XmlHttp post to implement the AJAX call ourselves.

(I read some comments and feedbacks said it I can post some simple example code, that will be helpful, I am sorry for that I did not do that at beginning)

The Javascript AJAX call will be like:

   1:  var xmlpost;
   2:  function OnXmlPostRequestComplete(args){}
   3:  function OnXmlPostRequestTimeout(){alert('Timeout not implemented.');}
   4:  function OnXmlPostRequestError(){alert('Exception not implemented');}
   5:  function AjaxSaveDocument(documentText,OnPostRequestComplete,OnPostRequestError,OnWebRequestTimeout)
   6:  {
   7:      xmlpost=new ActiveXObject("Microsoft.XMLHTTP")
   8:      OnXmlPostWebRequestComplete=OnPostRequestComplete;
   9:      OnXmlPostWebRequestError=OnPostRequestError;
  10:      OnXmlPostWebRequestTimeout=OnWebRequestTimeout;
  11:      
  12:      xmlpost.open("POST", "AjaxSaveDocument.aspx");
  13:      xmlpost.onreadystatechange=OnPostRequestCompleted;
  14:      xmlpost.setRequestHeader("Content-Type", "text/html; charset=utf-8")
  15:      xmlpost.send(documentText)
  16:  }
  17:   
  18:  function OnPostRequestCompleted() 
  19:  { 
  20:      if (xmlhttp.readyState==4) {
  21:      
  22:          if(xmlhttp.statusCode>299)
  23:          {
  24:              OnXmlPostRequestTimeout();
  25:          }
  26:          else
  27:          {
  28:             try
  29:             {
  30:                OnXmlPostRequestComplete(xmlhttp.responseText);               
  31:             }
  32:             catch(error)
  33:             {
  34:                OnXmlPostRequestError();
  35:             }
  36:          }
  37:       }
  38:  }

The Javascript consumer call will be like :

   1:      var documentText = document.getElementById("txtBody").value;
   2:      AjaxSaveDocument(documentText,OnCustomerizedAjaxCallComplete);
   3:      function OnCustomerizedAjaxCallComplete(args)
   4:      {
   5:         alert("Save Result is " + args);
   6:      }
 
On the server side code-behind, the C# code will get the http request stream and parse the stream to the string, then put the business logic
   1:  public partial class AjaxSaveDocument : System.Web.UI.Page
   2:  {
   3:      private string RequestInputStreamToString()
   4:      {
   5:          using (StreamReader reader = new StreamReader(Request.InputStream))
   6:          {
   7:              string contents = reader.ReadToEnd();
   8:              return contents;
   9:          }
  10:      }
  11:      protected void Page_Load(object sender, EventArgs e)
  12:      {
  13:          if (Request.HttpMethod == "POST")
  14:          {
  15:              string strmContents = RequestInputStreamToString();
  16:             
  17:              Response.Clear();
  18:              Response.Write(string.Format("Content length is {0}",strmContents.Length));
  19:              Response.End();
  20:          }
  21:      }
  22:  }
 
I will try Casey Barton’s suggestion to see will that work and how it will work. I tried that solution before but I did not try very hard on it since I like to everything from scratch :(
Here is the feedback from Casey Barton:

Tuesday, November 18, 2008 10:03 AM by Casey Barton

The *real* fix would be to implement asynchronous (de)serialization for AJAX requests and responses, that operates on the HTTP stream as it transfers data.

 
Thanks for you guys great feedback.

Tags: ,


Nov 18 2008

The problem of JSON and how to fix it

Category: AJAX, Asp.NetRobert @ 12:38 am

In AJAX world, JSON is one of the most popular implementation, Asp.Net AJAX is using this concept, and it really works great.

But in some very special situation, JSON could cause some unexpected problems.For example, if the user want to submit a huge document on the local to the server side, you will find it takes too much time before the server-side gets requested.Why?

We knew that one task for the so-call AJAX framework is to serialize the data and on the server-side to de-serialize the data. But if the data is huge, then the serialization itself will take a while.

If the server only needs the body of the huge document, why do we need to serialize the client -side data? In this case, we don’t need the serialization at all and we can use the XmlHttp post to implement the AJAX call ourselves.


Nov 16 2008

Multi culture programming in Asp.Net

Category: Asp.NetRobert @ 2:08 am

Asp.Net provide a very powerful multi culture programming pattern.In this post , we will discuss specially under the user control level.

Last weekend, one of my friends asked the question about the user control multi culture programming problem. He could not make it work at that time and I was in the middle of the NFlickr Release , so I promised to write a post about this problem.

First, I did not know what exactly the problem was, but I will try to use the standard way to implement it.

The user control is “Login.ascx” and the html markup is

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Login.ascx.cs"   Inherits="UserControl_Login"  %>
<asp:Label ID="lblUser" runat="server" Text="UserId"
    meta:resourcekey="lblUserResource1" /> <asp:TextBox ID="txtUser"
    runat="server"  />
<br />
<asp:Label ID="lblPassword" runat="server" Text="UserId"
    meta:resourcekey="lblPasswordResource1" /> <asp:TextBox ID="txtPassword"
    TextMode="Password" runat="server"  />
<br />
<asp:Button ID="cmdLogin" runat="server" Text="Login"
    />

Change to the design mode and select the menu tools->Generate the resource file. This tools will create the App_LocalResources folder and Login.ascx.resx file automatically. After this operation , the file content will be changed to

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Login.ascx.cs"   Inherits="UserControl_Login"  %>
<asp:Label ID="lblUser" runat="server" Text="UserId"
    meta:resourcekey="lblUserResource1" /> <asp:TextBox ID="txtUser"
    runat="server" meta:resourcekey="txtUserResource1" />
<br />
<asp:Label ID="lblPassword" runat="server" Text="UserId"
    meta:resourcekey="lblPasswordResource1" /> <asp:TextBox ID="txtPassword"
    TextMode="Password" runat="server" meta:resourcekey="txtPasswordResource1" />
<br />
<asp:Button ID="cmdLogin" runat="server" Text="Login"
    meta:resourcekey="cmdLoginResource1" />

The trick is the meta:resourceKey , in the Login.ascx.resx file, there will be a list of keys which  will like cmdLoginResource1.Text,txtPasswordResource1.Text, etc.

If we want to support the Canada French, what we need to do is just create a french version of the resource, the name convention will be Login.ascx.fr-CA.resx

Now, we can go back to the default.aspx file which is the place holder for the user control

<%@ Page Language="C#" AutoEventWireup="true"   CodeFile="Default.aspx.cs"  Inherits="_Default" %>

<%@ Register src="UserControl/Login.ascx" tagname="Login" tagprefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <uc1:Login ID="Login1" runat="server" />
    <asp:Label ID="lblMessage" runat="server" Text="Message"
            />
    </div>
    </form>
</body>
</html>

When we run this file , we will find that the multi-culture does not work!! Why?

By default, if we don’t write any code and want to have the multi-culture feature, we need to enable the multi-culture for this page.We need to change the page register statement.

 

 

After we had this line of change, we will have the full set of multi-culture support without writing a line of code.

The source code can be downloaded at http://technetguy.com/Download/blog/UserControlCulture.rar

Tags:


Nov 15 2008

The first version of the NFlickr released

Category: NFlickrRobert @ 10:48 pm

Yesterday, the first Alpha  version of the NFlickr was released.This version was fully written by the .Net 3.5 and using the provider concept.For example, if you don’t like the built-in encryption or you don’t like the implementation of the http communication. you can use the same interface of the provider.

From the functionality part, this version will have the below support:

1. Login to the Flickr Service
2. Create the PhotoSet.
3. Upload the photo
4. Add the photo to a photoSet.
5. Search the photo based on the privacy, tags.

 

In the next version of the release, the NFlickr will have those feature.

1.LINQ to Flickr support for Photo search (This will be the asset :))
2. Get the photo Information.
3. Update the photo information, privacy, Title,etc

 

Welcome to provide the suggestion and feedback.


Nov 15 2008

Is silverlight application the windows or web?

Category: SilverlightRobert @ 12:39 am

Before we started to develop a silverlight application, does anyone ask the question? Is silverlight the windows or web?

The short answer is Windows, but wait….

All the code was executed on the client side, the code was download was downloaded to the client side.It has to be executed under the silverlight ActiveX control or browser plugin(For Firefox, Chrome, etc).

But why we still call the silverlight as a web application? Because it can interact with the HTML Dom, it can access the IIS by using the WCF, REST service. It’s running under the browser. It’s the light version of  WPF and it’s even called WPF/E!

So I will rather consider the silverlight as a web application , but I will be more concerned about the security while I am developing my application because the code was executed at the client side.

Tags:


Nov 13 2008

Using the DOM to control the element(TreeView) inside Silverlight

Category: SilverlightRobert @ 11:55 pm

In our Update the Silverlight toolkit(AutoCompleteBox) With the HTML DOM value post, we talked about the interacting between the Silverlight application and html DOM. It’s about using the Silverlight to access the DOM. Today we are going to discuss  using the DOM to access the Silverlight.

In the post Using the Silverlight toolkit build a online TreeView, we had a demo application to demo a buddy list tree using the Silverlight toolkit.Today we will wrote a more complex application to update the online and offline buddy members.

To make thing more difficult, we limited the require to get the buddy info from the html element which is out side of the Silverlight application.

Before we started, we need to introduce a new concept called Bridge Pattern , and we need to know a trick attribute “ScriptableMember()”.

The Silverlight code will be like

   1:         [ScriptableMember()]
   2:          public void AddOnline(string name)
   3:          {
   4:              TreeViewItem onlineItem = (TreeViewItem)treeview.Items[0];
   5:              TreeViewItem buddy1 = new TreeViewItem();
   6:              buddy1.Header = name;
   7:              onlineItem.Items.Add(buddy1);
   8:              int count = onlineItem.Items.Count;
   9:              onlineItem.Header = string.Format("online({0})", count);
  10:          }
  11:          [ScriptableMember()]
  12:          public void AddOffline(string name)
  13:          {
  14:              TreeViewItem offlineItem = (TreeViewItem)treeview.Items[1];
  15:              TreeViewItem offlineContact = new TreeViewItem();
  16:              offlineContact.Header = name;
  17:              offlineItem.Items.Add(offlineContact);
  18:              int count = offlineItem.Items.Count;
  19:              offlineItem.Header = string.Format("offline({0})", count);
  20:          }
  21:          public Page()
  22:          {
  23:              InitializeComponent();
  24:              TreeViewItem onlineItem = new TreeViewItem();
  25:              onlineItem.Header = "Online(0)";
  26:              treeview.Items.Add(onlineItem);
  27:              AddOnline("Robert1");
  28:              AddOnline("Robert2");
  29:              TreeViewItem offlineItem = new TreeViewItem();
  30:              offlineItem.Header = "Offline(0)";
  31:              treeview.Items.Add(offlineItem);
  32:              AddOffline("Roboo1");
  33:          }

You should notice that the AddOnline and AddOffline methods were marked by the attribute [ScriptableMember()]

Another trick will be how can we get the Silverlight object in the DOM script which will be the JavaScript in most case.

 <asp:Silverlight ID="Xaml1" runat="server" Source="~/ClientBin/ToolboxTest.xap" MinimumVersion="2.0.31005.0" Width="100%" Height="100%" OnPluginLoaded="silverLightLoaded" />

As you can see, there is one  OnPluginLoaded even and we will register this event by using the JavaScript function

   1:         var silverlight = null;
   2:          
   3:          function silverLightLoaded(sender) {
   4:              silverlight = sender.get_element();
   5:              
   6:          }

The full html source will like

   1:  <head runat="server">
   2:      <title>ToolboxTest</title>
   3:      <script type="text/javascript" language="javascript">
   4:          var silverlight = null;
   5:          
   6:          function silverLightLoaded(sender) {
   7:              silverlight = sender.get_element();
   8:              
   9:          }
  10:          
  11:          function Button1_onclick() {
  12:              
  13:              var value = document.getElementById("DataSource").value;
  14:              silverlight.Content.BuddyList.AddOnline(value);
  15:              
  16:          }    
  17:      </script>
  18:  </head>
  19:  <body style="height:100%;margin:0;">
  20:      <form id="form1" runat="server" style="height:100%;">
  21:          <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
  22:          <div  style="height:100%;">
  23:              <asp:Silverlight ID="Xaml1" runat="server" Source="~/ClientBin/ToolboxTest.xap" MinimumVersion="2.0.31005.0" Width="100%" Height="100%" OnPluginLoaded="silverLightLoaded" />
  24:          </div>
  25:          <input type="text" id="DataSource" name="DataSource" />
  26:          <input type=button value="See"  onclick="Button1_onclick()" />
  27:      </form>
  28:  </body>
  29:  </html>

If you want to run this code, nothing will happen! The reason is the Javascript does not have a way to access the content inside the Silverlight now. So there is one line  of code need to be added.

At the end of the Page load event at Silverlight, we should register a object for the outside to be referenced.

 

   1:   HtmlPage.RegisterScriptableObject("BuddyList", this);
 
That's it.Now we can click the html button and get the access of the silverlight TreeView control.
The source code can be downloaded at http://technetguy.com/Download/blog/SilverLight_BuddyList_Js.rar


Nov 13 2008

Update the Silverlight toolkit(AutoCompleteBox) With the HTML DOM value

Category: SilverlightRobert @ 1:15 am

The silverlight toolkit provided the AutoCompleteBox and We already had one post talking about the usage about it.

This post will focus on the way we can update the content of the AutoCompleteBox by using the value from the HTML DOM value.

The initialized code was not changed and we only added a new button on the silverlight control which is the

<Button Height="24" HorizontalAlignment="Right" Margin="0,20,123,0" VerticalAlignment="Top" Width="76" Click="Button_Click" Content="Refresh"/>

Then we go to the web page ToolboxTestTestPage.aspx to add a new Textarea input box which will provide the content.

<textarea id="DataSource" name="DataSource" cols="10" rows="5"></textarea>

The real problem is about the interacting the silverlight application and the HTML DOM object.

The first code I tried is to use the common sense way which is

   1:             HtmlElement datasource= HtmlPage.Document.GetElementById("DataSource");
   2:              if (datasource != null)
   3:              {
   4:                  string content = datasource.GetProperty("innerHTML") as string;
   5:                  List<string> list=content.Split("\n".ToCharArray()).ToList();
   6:                  txtAutoComplete.ItemsSource = list;
   7:              }

But I find it’s almost no way for me get the user input value, so I changed my way to using the Javascript.

   1:             string content = (string)HtmlPage.Window.Invoke("GetDataSource", null);
   2:              List<string> list = content.Split("\n".ToCharArray()).ToList();
   3:              txtAutoComplete.ItemsSource = list;

when I did this , I realized that how cool and convenient will be when we have the C# dynamic. 

And the Javascript on the page is

   1:  <head runat="server">
   2:      <title>ToolboxTest</title>
   3:      <script type="text/javascript" language="javascript">
   4:          function GetDataSource() {
   5:              return $get("DataSource", document).value;
   6:          }
   7:      </script>
   8:  </head>

Keep in mind , don’t use the  Html.Document.Invoke because the Javascript is an instance of the html window, not the document. In the future post, I will take another way around to getting the silverlight value from the HTML Dom.


Next Page »