I attended the ASP.NET DevConnections in Orlando this week and attended an interesting presentation by Dino Esposito and it was very enlightening.

I have a Web Service that I call to get some data and then I bind it to a Custom DataGrid control via Serialization into a Memory Stream and then doing a DataSet.ReadXml() call.  Then I bind the datagrid to the ds.Tables[0].  Now I found a way that does not incur a cost of the Serialization, but incurs an up front setup of the class that I will use for binding.

I will reiterate from Dino’s talk at the conference, hopefully he won’t mind, so that you get the idea of why you need to do this.

When you use wsdl.exe or the VS way of Add Web Reference to get the Proxy class for the Web Service call, you actually get back the definitions of the class that you will send and receive to the Web Service to do the work.  When wsdl.exe (explicitly) or VS Add Web Reference (under the covers) is used to get the proxy, the proxy generates the classes with public “Fields” and not “Properties”.  The difference being that Fields are defined like this:

public class BenClass
{
      public string Name;
     
public int Age;
}

When using Properties you see this:

public class BenClass
{
      
private string name;
      
private int age;

     public string Name
    {
        
get { return name; }
        
set { name = value; }
    }

    public int Age
   {
           get { return age; }
           set { age = value; }
   }
}

 When you use the Properties and the class implements the IEnumerable interface, it can be bound.  The example that was used in the session was a Collection of classes which is a little different but still carries the same concept.

I am writing this because I know that there is a performance hit when using the Serialization (because of the reflection side) instead of just using the class.  So the up front nature of providing a class that has the Property Accessors and deleting the code out of the generated proxy .cs or .vb file, you can save some performance hits.  Now remember that this is not for the faint of heart, as if you remove the wrong things from the Proxy and do not redefine them correctly elsewhere in the project, it could render your project useless.

So I will be posting the code to what I used to do, and then the alternatives as well next time.

Have a great one and feel free to comment.