Monday, September 6, 2010

Dependency Injection – Unity IoC Container

Had been looking around on the web to pick one of the dependency injection container and came across video from David Hayden. Its a great video for quick start and shows a lot of features of Unity.

You can watch the video at: http://www.pnpguidance.net/Screencast/UnityDependencyInjectionIoCScreencast.aspx

Wednesday, September 1, 2010

Using Fiddler to test WCF Post methods

By default, WCF only allows JSON and XML for post data. It'll not allow callers to post data using html forms unless mime type application/x-www-form-urlencoded is supported.

In order to post data, json or xml, view the service /help page which gives you the exact format. This message template can be used as fiddler Request Body under Request Builder Tab.

When mime type application/x-www-form-urlencoded is enabled from wcf service. past the query string in Request Body

For e.g. if your html link was like http://www.mysite.com/index.html?param1=1&param2=2&param3=3

In Fiddler Request Builder
- Set Method to Post
- url as http://www.mysite.com/index.html
- Request Header
    User-Agent: Fiddler
    content-type: application/x-www-form-urlencoded
- Request Body
    param1=1&param2=2&param3=3

Also if an object was excepted on wcf side, for instance following service contract
getData(Person p)

Where p is

class Person
{
    [DataMember]
    public string name;
    [DataMember]
    public string age;
}

Request Header will be the same as above but Request Body will be like:
Person.name=John Smith&Person.age=25

As a best practice, keep your Request Body case sensitive to avoid any issues.

Tuesday, August 31, 2010

Efficient SQL Profiling

 

When you have to run SQL Profiler to view activity, if database server is used by multiple people or accounts which you want to view, there could be a lot of activity going on the server. It gets very confusing or near impossible to find out which sql statements or profiler activity you should look at.

The best way to view activity initiated by you is to use a Hostname filter.

You can even create a template so you don't have to create a filter every time you want to run sql profiler.

1. Open SQL Server Profiler
2. Create New Template (Base it on Standard template)
  SQL Profile - New Template
3. Click on Column Filter in Event Selection Tab and Define Hostname Like as your machine name. Make sure to check show all columns
 SQL Profiler - Step2
4. Save Template
Once template is saved. You can create a new trace using the template you just created with Hostname filter.
SQL Profiler - Step3

Upon running the trace, you'll see traces originated only from your machine.

Happy tracing!

Friday, August 20, 2010

DateAdd : reusable Javascript function

 

While looking for a Javascript equivalent of DataAdd VB or SQL method, I came across this function which is working flawlessly. Please visit the forum post to view it directly and also as follow:

   1: function DateAdd(timeU,byMany,dateObj) {
   2:     var millisecond=1;
   3:     var second=millisecond*1000;
   4:     var minute=second*60;
   5:     var hour=minute*60;
   6:     var day=hour*24;
   7:     var year=day*365;
   8:  
   9:     var newDate;
  10:     var dVal=dateObj.valueOf();
  11:     switch(timeU) {
  12:         case "ms": newDate=new Date(dVal+millisecond*byMany); break;
  13:         case "s": newDate=new Date(dVal+second*byMany); break;
  14:         case "mi": newDate=new Date(dVal+minute*byMany); break;
  15:         case "h": newDate=new Date(dVal+hour*byMany); break;
  16:         case "d": newDate=new Date(dVal+day*byMany); break;
  17:         case "y": newDate=new Date(dVal+year*byMany); break;
  18:     }
  19:     return newDate;
  20: }

Tuesday, August 10, 2010

Using a Stored Procedure in Entity Framework 4

If you have worked with LINQ and added stored procedures to it, it would seem obvious to be able to it the same way in Entity framework.

In LINQ all you had to do is to drag the stored procedure from your Server Explorer respective data connection on your model diagram.

In entity framework 4, there are few steps and they are different.

1. On your entity diagram in visual studio, right click and choose Update Model from database.

2. Add your stored procedure from the wizard. What it does, it adds the proc to your diagram. Its not visible in .Net code yet.
If you go to Model Browser > {EntityModel}.Store > Stored Procedures, it should be visible in there now

3. Right click, Add > Function Import. Complete this according to your requirement, name of the proc you want to see in .Net, which stored proc it maps to and what is the collection it returns etc.

After step 3 you can access access the proc using an instance of context.

For more visual representation of the steps, please visit WebLog of Ken Cox

Wednesday, August 4, 2010

Extracting tags from xhtml content

There are two ways of doing it. One is the dirty way, using Mid function in VB.Net or IndexOf string method but the more appropriate way would be to use regular expressions.

Following code will get you title using regular expression
Regex regex = new Regex("<title>(?<title>.*?)</title>", RegexOptions.IgnoreCase);
Match titleMatch = regex.Match(html);
string title = titleMatch.Groups["title"].Value;
Following code will get you meta tag My_Meta

Regex regex = new Regex("<META +NAME=\"(?<name>My_Meta*?)\" +CONTENT=\"(?<content>.*?)\" */?>", RegexOptions.IgnoreCase);
Match metaMatch = regex.Match(html);

title = metaMatch.Groups["content"].Value;

Thursday, July 15, 2010

Saving objects using Entity framework

TableClass tableObject = new TableClass();
tableObject.property1 = "Hello";
tableObject.proeprty2 = "World";

// NOTE: make sure to use the same instance of entityContextObject which was used to populate the tableObject (e.g. updating back to database)
entityContextObject.AddToTableClass(tableObject);
entityContextObject.SaveChanges();