Friday, February 25, 2011

Difference between String.Empty and ""

Yesterday someone asked whether there is difference between String.Empty and "". I answered promptly "there is no difference".

What I thought at the time is that we can write a test:

Assert.AreEqual(String.Empty, "");

this will pass the test.

But today, when I think about this question more carefully. I will say there is difference between them.

String.Empty is intialed at run time.


so we can say:
const strA="";
static readonly strB=String.Empty;

but we cannot write const strA=String.Empty;Just take a quick notes.

JavaScript Cross Domain Call

Let's create a Asp.net Empty Web application called JavaScriptCrossDomainTopic. Let's add a new HTML page called "MyTestPage". Let's include the Jquery library, and write some code like:

< script type="text/javascript">
$(document).ready(function () {
$.get("http://google.com");
});
< /script>

in the body tags.

see the following picture:



By defaut internet browser security setting, when we browse our test page, there will pop up a window. saying :
"The page is accessing information that is not under its control. This poses a security risk. Do you want to continue?"



We can get rid of this pop up by do the browser security setting. But what causes this pop up.

Let's write some code to create XMLHttpRequest object and use it to request the url.

function createXMLHttpRequest() {
try { return new XMLHttpRequest(); } catch (e) { }
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { }
alert("XMLHttpRequest not supported");
return null;
}

< script type="text/javascript">
$(document).ready(function ()
var xhReq = createXMLHttpRequest();
xhReq.open("get", "http://google.com", true);
xhReq.send(null); });
< /script>

This also has the security pop up. This is so called "Same Origin Policy". Please find materials about this ourselves.

We can use Jquery, $.get(), $.post() or ajax call ($.ajax({url:...}) , all will have the cross domain issure. The reason is all these methods are encentially use the XmlHttpObject to call the url.

As I want my application don't have this anoying pop up, we need to by pass the client-side cross domain restriction.

Here is an important princeple:

The same-origin policy doesn't prevent adding scripts/Images/or other Elements dynamically in the page from a different domain as long as the script doesn't try to load document from a different domain.

so we wrote code like the following to call a cross domain url in client side:

var script=document.createElement('script'); //or var myImage=new Image();

script.src="http://...."; //or myImage.src="";
//or var url="";script.setAttribute('src', url);

document.body.appendChild(script); //or document.body.append(myImage);

See the following picture:



and We browse our test page, we will find there is no pop up again. We bypass the cross domain restriction!

In many situations we need to use javaScript to call a Web/WCF service, for example we create a REST WCF service in the previous article. If a Web/WCF service has Json response support.

There is a technique called JsonP.

$.getJSON("http://www.anotherdomain.svc/MyRESTService/GetProduct?id=3&name=apple&price=1.25&callback=?", function(data) {
//do something here.
});

There will not have cross domain issue as well. By my understanding, JsonP also use the our mechnism above to load to the same domain an Element which has src attribute set to the Json service.

Create a REST WCF Service

Let's create a WCF Service Application project, called DemoRESTService.

using System.ServiceModel;
using System.Runtime.Serialization;

namespace DemoRESTService
{
[DataContract]
public class Product
{
[DataMember]
public int ID { get; set; }

[DataMember]
public string Name { get; set; }

[DataMember]
public double Price { get; set; }

}

[ServiceContract]
public interface IGetProductService
{
[OperationContract]
Product GetProduct(int id, string name, double price);
}

public Product GetProduct(int id, string name, double price)
{
return new Product() { ID = id, Name = name, Price = price };
}
}

Compile, this is our namal WCF service. We can use Service Client tool to test:



These is our old knowledge, so we are not go too much into details. To create a REST WCF service, the most important thing is the WebGet attribute. In the WebGet Attribute we can set the UriTemplate, if we don't set this value, the REST service will have the Url like: http://.... svc/GetProduct?id=5&name=Apple&price=1.25 to call the REST service. Here we use the default uri template. We can also specify the Service Response data format as Json or XML in WebGet attribute. In my next series of blog, I am going to write something on javaScript/Jquery cross domain call, JsonP et al, so I get the respose format to be Json. The WebGet attribute is like [WebGet(ResponseFormat = WebMessageFormat.Json)] for my furture use.

The hard thing is web.config. We can use WCF Service Configuration Editor to edit the web.config file. But it is still need careful writing for this part.



I create the web.config file as picture below (we can use this as template for furture use).


Let's compile and now we create a REST WCF service below.


About the hosting,(Address, Binding, Contract), there are too much materials on this, I am not going to talk in my blog.

Let's in IE, we type the address:

http://..../GetProductService.svc/GetProduct?id=1&name=apple&price=1.25

We will get a Json results return back. (notice for url querystring the querystring's parameters should also be able to be seperated by semicolumn ";", http://..../GetProductService.svc/GetProduct?id=1;name=apple;price=1.25 should also work, but it doesn't. This is a Microsoft bug).

We can see the result is Json format:


There should aslo have materials to talk about how to build a REST Web Service has Json Response format. If anyone is interested in that, he can search the related materials. You can download my Demo Code Here.

JavaScript check whether a function is defined

if (typeof myFun == "undefined") {
alert('undefined');
}
else {
alert('defined');
}
function myFun()
{ }

Wednesday, February 16, 2011

Optional Paramter in JavaScript function

I have a function myFun(param1, param2) {
//area1,...;}

When we call this function, we can call:

myFun(1,2);
myFun(1); --param1=1, param2 is undefined
myFun();--param1, param2 undefined

In the area1 place, we can add the sample code like

if(param1===undefined)
param1=0;

if(param2===undefined)
param2=1;

Thursday, February 10, 2011

Could not load file or assembly 'App_Web...'

Here is a good article talking about why this error coming out and how to solve this:

http://yetanotherdeveloper.com/post/2008/08/10/Could-not-load-file-or-assembly-App_Web.aspx

we can set the compilation batch="false" to solve this issue.