RSS

Monthly Archives: May 2012

We Can Still Do Anything

“What we have to remember is that we can still do anything. We can change our minds. We can start over. Get a post-bac or try writing for the first time. The notion that it’s too late to do anything is comical. It’s hilarious. We’re graduating college. We’re so young. We can’t, we MUST not lose this sense of possibility because in the end, it’s all we have.”

– Marina Keegan (The Opposite of Loneliness)

 
Leave a comment

Posted by on May 30, 2012 in General

 

Tags: , ,

Why ASP.Net Web API and Not WCF?

Excerpt from Ido Flatow

  • Even with WCF REST starter kit (or new features in WCF 4), we didn’t get any of the client-side libraries.
  • Although we did get most of the service-side features, the new inspectors feature is not included.
  • Although some of the service-side features were well-integrated into WCF, others required the use of ASP.NET by turning on the ASP.NET compatibility mode.
  • To get some of the new features in WCF 4, you needed IIS hosting and ASP.NET.
  • Not all types of requests were supported easily (ever tried posting HTML form data to a WCF HTTP service?).
  • Overuse of CLR attributes to define the POST/GET/PUT/DELETE was tedious.
  • Configuration required to create “Web” HTTP (RESTful) type of services with all of the endpoint behavior.
  • WCF is heavy in configurations.
  • WCF overuses attributes.
  • WCF infrastructure did not support testing well.
 
1 Comment

Posted by on May 11, 2012 in General

 

Tags: , ,

Resolve ‘Cannot Serialize Parameter of Type’ Issue in WCF

This annoying error keep popping up when my WCF method return a POCO object generated by DbContext generator.

Or, if the method return a collection of POCO objects:

And in Fiddler, it identified as HTTP 504 error:

This problem is caused by serializing problem when WCF method try to serialize DbSet<T> (or ObjectSet<T> in case of using ObjectContext generator) into POCO objects.

To solve this problem, try the following:

Add KnownTypeAttribute to POCO object.

In POCO class, add KnownType attribute. Also, add DataContract and DataMember attribute if not present.

Object type to be passed to KnownType attribute should be all collection objects that are member of the POCO class. In this case, Product.

If the POCO class is generated with T4 template, you might want to modify the template to include these attributes.

[DataContract(IsReference = true)]
[KnownType(typeof(Product))]
public partial class Category
{
    [DataMember]
    public int Id { get; set; }
    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public virtual ICollection<Product> Products { get; set; }
}

Context Configuration

Disable ProxyCreationEnabled in Context configuration.

For DbContext:

// Return one object.
public Category GetCategory(string id)
{
    var cId = Convert.ToInt32(id);
    var db = new NorthwindEntities();

    db.Configuration.ProxyCreationEnabled = false;
    return db.Categories.Include("Products").First(x => x.Id == cId);
}

// Return collection of objects
public IEnumerable<Category> GetCategories()
{
    var db = new NorthwindEntities();

    db.Configuration.ProxyCreationEnabled = false;
    return db.Categories.Include("Products").ToList();
}

In the case of using ObjectContext, the configuration is:

var context = new POCEntities();

context.ContextOptions.ProxyCreationEnabled = false;

Why we need to disable POCO proxies?

 
Leave a comment

Posted by on May 9, 2012 in General

 

Tags: , , ,

Exposing IIS Express to External Traffic

By default, web application hosted in IIS express can only be accessed locally (localhost) and it will block all external traffic.

What you need to do in order to expose IIS express web application to external traffic:

Configure HTTP.SYS

Run the following command in Command Prompt with Administrator privilege.

netsh http add urlacl url=http://my-w7-pc-dev:23880/ user=everyone

Where my-w7-pc-dev is name of the machine. You can’t customize this, it must be name of the machine.

This will basically add the mentioned URL to HTTP.SYS, a handler for HTTP requests.

Edit Site Settings

Using Microsoft WebMatrix, edit Settings of the site to use the same URL you register to HTTP.SYS.

 

More about Configuring HTTP and HTTPS.

 
Leave a comment

Posted by on May 9, 2012 in General

 

Tags:

Audit Trail in Entity Framework

When saving changes (ObjectContext.SaveChanges())in Entity Framework, we have access to original and current values. These values can be used for audit trailing / change tracker.

The class DbContext.ChangeTracker provides access to these values. As one might have suspected, the entity context must derived from DbContext.

To access these values, we can loop through ChangeTracker.Entries<T>() or ChangeTracker.Entries(), which is a collection of DbEntityEntry<T>.

More over, DbEntityEntry<T>.Entity.State, type of EntityState, will tell us what operation is being performed (Added, Modified, or Deleted)

Then, to access original or current values, we dive into DbEntityEntry<T>.OriginalValues or DbEntityEntry<T>.CurrentValues, both of which are type of DbPropertyValues.

public override int SaveChanges()
{
    foreach (var entry in ChangeTracker.Entries<Product>())
    {
        if (entry.State == EntityState.Added)
        {
            // Do some actions.
        }
        else if (entry.State == EntityState.Modified)
        {
            // Do some actions.

            // Get current values.
            var currentId = entry.CurrentValues["Id"];
            var currentName = entry.CurrentValues["Name"];

            // Get original values.
            var originalId = entry.OriginalValues["Id"];
            var originalName = entry.OriginalValues["Name"];
        }
        else if (entry.State == EntityState.Deleted)
        {
            // Do some actions.
        }
    }
}
 
1 Comment

Posted by on May 2, 2012 in General

 

Tags: ,

Audit Trail Performance Comparison

Below are audit trail performance testing in different technologies / approaches.

  • Baseline: Standard DML operations with no audit trail.
  • Change Data Capture (CDC): DML operations with CDC to capture audit trail in system tables.
  • Trigger: DML operations with trigger to record audit trail in different table.
  • Entity Framework 1: DML operations with audit trail recording in Entity Framework’s SaveChanges() method by accessing EF’s OriginalValues object for tracking data changes.
  • Entity Framework 2: DML operations with audit trail recording in Entity Framework’s SaveChanges() method by accessing EF’s DbSet object for tracking data changes.
Insert Update Delete All
Baseline 1717.05 2353.95 2650.5 6761.95
Change Data Capture (CDC) 1632 2399 2649 6689
Trigger 1649.6 2347.25 2708.3 6723.65
Entity Framework 1 3517.4 4214.6 3350.3 11106.15
Entity Framework 2 3386.35 5678 4916.95 13993.9
Test results are in millisecond. Operations are performed with 500 records.

 
Leave a comment

Posted by on May 2, 2012 in General

 

Tags: , ,

DocType / Document Type Declaration (DTD)

Strict

Contains all HTML elements and attributes. No deprecated tags are supported. Framesets are not allowed. Code must be written correctly. If XHTML, the markup must also be written as well-formed XML.

Traditional

Contains all HTML elements and attributes. Deprecated tags are allowed. Framesets are not allowed. If XHTML, the markup must also be written as well-formed XML.

Frameset

Contains all HTML elements and attributes. Deprecated tags are allowed. Framesets are allowed. If XHTML, the markup must also be written as well-formed XML.

List of DocType declarations.

Story of DocType.

 
Leave a comment

Posted by on May 1, 2012 in General

 

Tags: ,

 
%d bloggers like this: