Friday 29 January 2010

Canonical address format for phone numbers

The canonical address format is a universal (international) phone number format.

Pattern:

+ Country/RegionCode (AreaCode)SubscriberNumber

Example:

+1 (425) 555-0100

Regular Expression:

const string canonicalRegx = "(?:\+\d{1,4} ?(?:\(\d{0,5}\))?(?:\d+[-. ])*\d{2,})"

More:


Thursday 21 January 2010

WCF RESTful Services AJAX-enabled

http://www.west-wind.com/WebLog/posts/310747.aspx



UriTemplate:


Create a RESTful WCF Service:

1) Set WebGet or WebInvoke

[OperationContract]
[WebGet(UriTemplate = "Add?a={a}&b={b}", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
int TestAdd(int a, int b);

2) Add <webHttp/> endpoint behavior

<endpointBehaviors> <behavior name="VMServiceEndpointBehavior"> <webHttp/> </behavior> </endpointBehaviors>

3) Access the service

http://localhost:4082/MyApp.WebUI/Services/VMService.svc/Add?a=1&b=2

Example2:

1) Define your service

[OperationContract]
[WebInvoke(Method = "POST" , UriTemplate = "Add", RequestFormat = WebMessageFormat.Json ,
ResponseFormat = WebMessageFormat.Json)]
int TestAdd(int a, int b);

2) Create a proxy class
function serviceProxy(serviceUrl) {
var _I = this;
this.serviceUrl = serviceUrl;
this.invokeJsonMethod = function(method, dataInJson, succeededCallback, failedCallback) {
var url = _I.serviceUrl + method;
$.ajax({
type: "POST",
url: url,
data: dataInJson,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(result) {
if (succeededCallback != null)
succeededCallback(result);
},
error: function(result) {
if (failedCallback != null)
failedCallback(result);
}
});
}
}

3) Call the service

function btnTestAdd_click() {
var url = "http://localhost/AppName/Services/VMService.svc/";
var proxy = new serviceProxy(url);
var methodName = "Add";
var params = '{"a":1,"b":2}';
proxy.invokeJsonMethod(methodName, params, succeeded, failed);
}

function succeeded(e) {
//debugger;
alert(e.TestAddResult);
}

function failed(e) {
// debugger;
alert(e);
}

General Tips:
  • Parameter's Location: If you're using GET verb, it means that the parameters of the method must appear in the url using a UriTemplate, whereas when using other verbs (POST, DELETE, PUT), the parameters can appear in the body section of the message not url.
  • GET Parameter's Type: If you're using GET verb, it means that the parameters of the method must be of type 'string'. Which means that if you need other types of parameters such as int you must use POST verb. However the output parameter can be of any type as long as it is serializable.
  • WebMessageBodyStyle: The default option for WebMessageBodyStyle is Bare which means both Responses and Requests are not wrapped.
  • WebMessageFormat: The default data format for the webHttp behavior is XML, while the default data format for the enableWebScript behavior is JSON.
  • <webHttp>: This element specifies the WebHttpBehavior on an endpoint through configuration. This behavior, when used in conjunction with the <webHttpBinding> standard binding, enables the Web programming model (REST) for a WCF service.
  • <enableWebScript />: This element enables the endpoint behavior that makes it possible to consume the service from ASP.NET AJAX web pages.

Tuesday 19 January 2010

ORCA MSI Editor

In case you need to see or edit the properties of an .msi package, ORCA is there to help.

"You can edit the properties of any MSI. Change the title, and text within the installer. Look at how and where the files are delivered."

Friday 15 January 2010

WiX Windows Installer - MSI Package

WiX stands for Windows Installer Xml and is a tool to build msi packages but it has some more advanced features than Visual Studio Setup Project Templates.

Thursday 14 January 2010

Free Converters

Switch Audio Converter Software
To convert audio files such as wav, mp3, etc.

to be updated...


Monday 4 January 2010

Covariance, Contravariance, Invariance

Generally if an operator changes the type in some kind, it’s called variant.

If it retains the type (the type is fix), then it’s called invariant.

If an operator orders types in the way from general to more specific for any type, then it is called to be covariant.

If it orders types reversely from specific to more general, then it’s contravariant.
public class Vehicle
{

}

public class Car: Vehicle
{

}

public class Honda: Car
{

}





1. Covariance:

Covariance is the ability to assign an expression of a more specific type to a variable of a less specific type.

Methods in C# are ‘covariant’ in their return types

   // Valid since Honda inherits from Car
public Car GetCar1()
{

return new Honda();
}

// Valid
public Car GetCar2()
{

return new Car();
}

// Compile-time error
public Car GetCar3()
{

return new Vehicle();
}

Referenced type arrays are 'covariant'

2. Contravariance

Method arguments in C# are 'contravariant'

3. Invariance

'int[] arr;' is invariant.

Generic delegate types are always invariant in C# 3.0.

To be continued…