Welcome

You have reached the blog of Keith Elder. Thank you for visiting! Feel free to click the twitter icon to the right and follow me on twitter.

.Net Developers Guide to Identity

Posted by Keith Elder | Posted in .Net, Programming | Posted on 20-07-2006

0

Have you ever wondered how windows credentials and Kerberos worked under the hood and how to leverage that as a .Net developer?  What about Smart Cards, or the new up and coming addition to Vista called “InfoCard”?  Did you know there was a tool to assist in building roles for your applications called Azman?  Or better yet what is PKI or Federated Services?

I think you get the idea, there are tons of things .Net developers should be aware of when it comes to securing our applications.  Keith Brown from PluralSight.Com has an MSDN article title .Net Developers Guide to Identity that covers all of this and more.  Extremely well written and even has code samples where applicable.

TIP: Managing BindingSource Position in Data Driven WinForms

Posted by Keith Elder | Posted in .Net, Programming, Smart Clients | Posted on 28-06-2006

12

If you are working with a Windows Form where a user can create and edit data in the same form you may run into the problem of trying to set the BindingSource to a given record in a DataSet you want to work with. 

For example let’s say you have a DataGridView in a Smart Client.  When the user double clicks a given row in the DataGridView a form opens so they can edit the data.  More times than not (no matter how many demos you watch) user’s do not edit data values in DataGridViews.  Although this is a supported feature, business objects are too complex.  Imagine having to edit a contact in a single row in Outlook for example, not fun and not very user friendly.  Why every Microsoft demo I watch shows editing in DataGridViews is beyond me.  I digress though.

When the new form is instantiated we need to pass two things:  the dataset we are working with, and the IDof the item we want to edit.  We can pass this information into the constructor of the form without any trouble as seen below. By passing in the DataSet we can add new records, delete records or whatever we need to do.  The other benefit is since the dataset is passed by reference, any changes to it will automatically appear in the row the user just double clicked on.  In order for the form fields to be auto bound to the record the user clicked on we need to set the DataBinding of the controls in the form to a BindingSource.  The BindingSource serves as the go between between the UI and data store.    Here’s a sample “CellDoubleClick” event that would happen in the DataGridView:

    1    int id = Convert.ToInt32(DataGridView.Rows[e.RowIndex].Cells[0].Value);

    2             MyForm myForm = new MyForm(dataSet, id);

    3             myForm.Show();

As the form loads the problem we face is having to tell the BindingSource in the form which record we want to set as the BindingSource.Current item.  At first glance it looks like we would just get the DataRow and then set it as the Current property.  Wrong.  The BindingSource.Current property has only a get accessor.  Instead we need to use the BindingSource.Position property.  Note:  Any change to the position property updates the current property used by the bindingsource.  The problem we face now is the BindingSource.Position property wants to know the index of the item in the underlying DataSource.  

We don’t know the index value used by the BindingSource but we do know the ID we want to use.  This is where the BindingSource.Find() method comes in.  Using the find method we can basically search the underlying datasource and retrieve the index of a given item.  Here’s an example:

contactBindingSource.Position = contactBindingSource.Find(“Id”, 2);

Once the line above runs, it is going to search the column “Id” in the DataSource associated with the BindingSource and return the index of that item. 

What if you use the same form to create a new item?  Simple, call the BindingSource.AddNew() method which will add a new item to the underlying DataSource.  Then move the index to the last item.  Here’s a sample:

      bindingSource.AddNew();
      bindingSource.MoveLast();

Since items are always added at the end of the associated data source the MoveLast() method will set the index to the last item in the underlying datasource.

I hope this makes sense because it is an easy way to have a form bound to a DataSet and use the same form to perform “CRUD” operations with.

Enterprise Library Feedback – Integrate GAT and Workflow

Posted by Keith Elder | Posted in .Net, Programming | Posted on 22-06-2006

0

Tom Hollander, who is on the Enterprise Library team, posted to his blog the Enterprise Library team want everyone’s feedback for the .Net 3.0 framework release.  As I sat and thought about this in relation to .Net 3.0, the more I thought about it and started to write about it, it just all became crystal clear.  Here’s my wishlist for EntLib 3.0:

  1. $20 – Add the EntLib console configuration tool into Visual Studio IDE.  Lot’s of people mentioned this in comments, so it looks like this should definitely be on their list.
  2. $20 – Integrate GAT to assist setting up EntLib based on the best “patterns” and “practice”. 
  3. $60 – Workflow Block

Integrating GAT with EntLib
Number one above is is self-explanatory so let me first start with number two.  If you think about abstracting and best practices and patterns you think of EntLib, however, there is one missing piece of the puzzle.  That missing piece is there is a best practice and pattern for how to setup and use EntLib.  If you are starting out using EntLib you are more than likely not going to use an “established best practice or pattern” as to how you should use EntLib.  I’ve seen this over and over when you want to create a new application to use EntLib you wind up holding onto one configuration file that you are familar with and copying and pasting.  Wouldn’t it be nice to simply install EntLib and using the GAT (guidance automation toolkit) you could create an “Enterprise Library Web Service”.  The GAT would then guide you through a configuration wizard of best practices and patterns on the recommended configuration and setup for EntLib based on the chosen application.  At least this would provide a baseline, and I think, a higher adoption rate of Enterprise Library in the enterprise.  It would also make it easy for anyone to start using thereby eliminating lots of training overhead to get up and running.

Workflow Application Block
Let me explain my reasoning for the Workflow block and why I invested so much into it. First and foremost Tom asked what we would want in regards to the 3.0 framework.  One of the new features of the 3.0 framework will be Windows Workflow Foundation.  For developers, this simply means you have a new set of APIs namespaced as System.Workflow. 

If you look at the existing blocks of Enterprise Library ( data, logging, exceptions, etc) these are all derived from currently existing Namespaces within the framework. The goal of these blocks are to abstract, simplify and tie each block together so it can be used with one another.  This is where the power of Enterprise Library comes in.  Since this would be a 3.0 release of Enterprise Library, Workflow is a major new namespace that will be available to us in the .Net 3.0 framework.  Having an easy way to re-configure workflow through an application config file and/or manage workflows via an application block to me makes perfect sense. If you’ve played with Windows Workflow Foundation you realize a lot of things could be abstracted.  Really this abstraction is no different than how EntLib provides the data block today.

Here are a couple of scenarios how I would want to use the Workflow block.  For example, imagine being able to configure EntLib when an exception is raised to be processed through the Workflow block instead of how we do it today via Categories. So based on the exception type, time of day, user that created the error, what phase the moon is in and how the stars align at that given time, the error could be routed via the Workflow block to the appropiate place.  With place being an email, log to database, notify pager, create a support ticket in an internal helpdesk system, notify your helpdesk, whatever.   Essentially what we *try* to do with EntLib today a lot of the time is make our logging blocks do workflow by creating tons of different categories based on different rules.  We wind up with a HUGE config file that becomes, well, HUGE.  Instead of doing things the way were are today, just simply have a workflow that you can define to handle all logging, exceptions or whatever.   

That’s my $.02.  I’ll be here all week. Smile

Video Cast: Building Three Tiered Solutions in Visual Studio and Team Foundation Server

Posted by Keith Elder | Posted in .Net, Asp.Net, Programming, Smart Clients | Posted on 21-06-2006

0

One of the biggest challenges I see team members at work struggling with when trying to learn .Net is “how to get started”.  If you are a developer with lots of experience, coming into VS and .Net things are very different let’s face it.  For example, what’s the right way to setup a project?  What’s the difference between a solution and project?  Everyone knows about n-tiered architecture, but how do you pull it off in Visual Studio?  If you are using TFS how do you setup projects so they can be easily versioned?  How do you organize the name spaces of projects?

I put together this video cast which is about 30 minutes long which I hope addresses some of these questions.  It should serve as a good starting point as how to setup a project when starting out, things to think about like namespaces and how to name your projects.  I’ve built a lot of production apps over my time and what is in the video is all based on experience so if you know of an “easier / better” way please drop me a line.  In the future things like GAT (guidance automation toolkit) will provide an easier way to accomplish what I’ve outlined in the video.  If you haven’t looked at GAT, you should definitely do so. 

Watch The Video Cast

(this is a repost of a post that was lost due to data loss)

Calling Multiple Web Services Asynchronously

Posted by Keith Elder | Posted in .Net, Asp.Net, Programming, Smart Clients | Posted on 16-03-2006

4

There are two ways to call web services:  synchronously and asynchronously.  Below is an example of a syncronous web call.  The HelloWorld() call will not get executed until the ReturnLastCustomer() is completed.  In some cases this is fine.  For example if you need the information from ReturnLastCustomer() to then call the HelloWorld() example.

Syncronous Web Service Call

            localhost.Service proxy = new WindowsApplication3.localhost.Service();

            localhost.MyCustomObject myObject = proxy.ReturnCustomObject();

 

            // Make another call to web service

            string hello =  proxy.HelloWorld();

In the example above we wouldn’t really care if one method completed before another.  There may be code below it that can go ahead and get started processing.  To do this we change this to call the service asynchronously.  The idea behind the asynchronous call is we setup an event that gets notified through a delegate that the web service method is done with processing.  Here is how the synchronous example above would look called asynchronously.  It may look like a little more code, but in Visual Studio as you type this just press tab when it gives you a tooltip help and it will generate the right side of the += as well as the method stub for the call back.

Asyncronous Web Service Call

      public Form1()

        {

            InitializeComponent();

 

            localhost.Service proxy = new WindowsApplication3.localhost.Service();

            proxy.ReturnLastCustomerCompleted += new WindowsApplication3.localhost.ReturnLastCustomerCompletedEventHandler(proxy_ReturnLastCustomerCompleted);

            proxy.ReturnLastCustomerAsync();

 

            // Make another call to web service

            proxy.HelloWorldCompleted += new WindowsApplication3.localhost.HelloWorldCompletedEventHandler(proxy_HelloWorldCompleted);

            proxy.HelloWorldAsync();

        }

 

        void proxy_HelloWorldCompleted(object sender, WindowsApplication3.localhost.HelloWorldCompletedEventArgs e)

        {

            string hello = e.Result;

        }

 

        void proxy_ReturnLastCustomerCompleted(object sender, WindowsApplication3.localhost.ReturnLastCustomerCompletedEventArgs e)

        {

            localhost.MyCustomObject myObject = e.Result;

        }

The Problem

The problem with the above example is depending on the speed of how things get processed we will get random errors that an asynchronous call is currently being processed.  The good news is there is an easy way to address this in .Net 2.0.   There are two overrides for the Async calls.  The first one is it takes the parameters of your web service call.  The second override adds another parameter of type “object userState”.  This allows us to pass in a unique identifier of the call. 

To make sure our asynchronous example above works we need to make sure these calls can run at the same time.  It is a simple change and that would look like this:

Calling Two Asynchronous Calls At the Same Time

            localhost.Service proxy = new WindowsApplication3.localhost.Service();

            proxy.ReturnLastCustomerCompleted += new WindowsApplication3.localhost.ReturnLastCustomerCompletedEventHandler(proxy_ReturnLastCustomerCompleted);

            proxy.ReturnLastCustomerAsync(Guid.NewGuid());

 

            // Make another call to web service

            proxy.HelloWorldCompleted += new WindowsApplication3.localhost.HelloWorldCompletedEventHandler(proxy_HelloWorldCompleted);

            proxy.HelloWorldAsync(Guid.NewGuid());

Notice the only difference in how we call the methods is we generate a new Guid as our userState object.  This allows the calls to keep track of the state of each call.  Pretty easy problem to get around but if you don’t explore your overrides of method calls, you may have missed this one.  Being able to call multiple web service methods asynchronously at the same time is pretty important when writing a Smart Client so you’ll probably use this more in this scenario. With larger Smart Clients you typically have things running in the background to check on statuses, update queues, as well as carry calls to save and return data.  This is how you get around that.