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.

AT&T Blackjack II Windows Mobile Phone Review

Posted by Keith Elder | Posted in Uncategorized | Posted on 18-01-2008

image Like this article? Subscribe to my news feed at http://feeds.feedburner.com/keithelder

A few weeks ago I acquired a Samsung Blackjack II cell phone.  I have been using the phone for several weeks straight and it is time to tell the story of the Blackjack II.  The Blackjack II replaces an aging 8525 Pocket PC phone as well as an aging Cingular 2125 Smart Phone.  Unlike a lot of other reviews from magazines that spit out feature after feature I hope to provide you with a different perspective as a technologist / geek who has used the phone for several weeks.  Let’s get started.

Software Bundle – A
The Blackjack II comes in two colors as shown in the picture to the left, black and wine.  Wanting a different color than black I opted for the wine color.  So far no regrets on the the color choice but it is funny how many people have mistaken this phone for a Blackberry because of the wine color.  The Blackjack II runs Windows Mobile 6 and comes pre-bundled with a nice software bundle.  It includes an AOL, Yahoo! and MSN messenger application, XM Radio, Notepad, Smart Converter, Stopwatch, PDF Viewer, MobiTV, RSS Reader, Smart Search (for searching email, files, documents, etc), Excel Mobile, Powerpoint Mobile and Word Mobile. 

I added a few standard applications I use as soon as I turned it on.  This list included my RSA token generator needed for work along with CodeWallet Pro 6, Google Maps, Facade, Telenav, and Tiny Twitter (recommended by a friend).  The one application I desperately needed was Internet Connection Sharing but it wasn’t included on the phone.

Fixing the Internet Connection Sharing Problem – F before, A after
The Blackjack II does not bundle Internet Connection Sharing which is apart of the standard Windows Mobile 6 application set.  For those of us on the go and traveling this is a must have program to connect our laptops to the Internet while sitting in the airport or other locations outside of the office.  Why it was left off I am not sure.  I was concerned at first about not having this because this was a deal breaker for me.  A few searches later and I had found out there was already a hack to provide the application.  To setup Internet Connection Sharing for the Blackjack II follow these steps (reprinted from the Howard forums).

1. Download the two files below:

 

2.  Transfer both of the files to the “My Documents” folder on the phone.  Do not copy to your storage card FYI.
3.  Install the secpolicies.cab file but using File Explorer located in the Applications folder.  Reboot.
4.  Install the BJ.WM6.ICS.Enable.cab file using File Explorer located in the Applications folder.  Reboot.
5.  Using File Explorer located in the Applications folder navigate to the “My Documents” folder and click on the Certs.cab file and reboot.

After following these simple steps “Internet Application Sharing” should now be visible within the Start Menu of the phone.  Connect the phone to a laptop or desktop computer using the provided USB cable, launch the application and press connect.  Your computer should now be online using the 3G or Edge services from the Blackjack II. 

New Jog Wheel – A-/B+
One of the new features of the Blackjack II is the jog wheel.  The wheel is located in the center of the phone and allows for easy navigation of menus, email, web browsing and much more.  Having had phones with a jog wheel on the side of the phone I have to say that having it on the front is the way to go.  It is natural and allows for easy one handed use.  There are times when the jog wheel feels sluggish though.  I am not sure if this is a programming sensitivity issue or hardware.  To clarify what I mean by sluggish there are times when I will move the wheel just a quarter turn and nothing happens, nothing moves.  It is like you have to move the wheel a lot farther than expected which leaves a sluggish impression sometimes.  I think this could be fixed with a possible registry hack although I haven’t found one yet. 

The only other complaint I have about the jog wheel is I haven’t yet found a hot key that I can press to scroll a page at a time or to select multiple emails for deletion.  When viewing web pages this is really important or when scrolling through 100 emails in the Inbox.  I remember the Blackberry 7290 I had years ago had this feature where a certain key could be pressed and if the jog wheel was moved it would scroll in pages or select multiple lines.  I miss it.

Keyboard / Navigation – A-
I’ve owned a lot of integrated keyboard phones over the years and I find the keyboard on the Blackjack II easier to type on than the previous Palm Treo versions I have owned but not as good as the Blackberry or the slide out keyboard on the 8525.  Of course the main reason for this is the keys are smaller on the Blackjack to save space.  Because the keyboard is smaller it makes the device more portable so this is a trade off.  For my purposes I like the smaller form factor so the rating gets bumped back up.  With this type of phone consumers want the device to function as a phone first and a messaging device second.  In this regard it accomplishes what it sets out to do.

One of the nice features of the keyboard is the built-in hot keys on the front to launch email or the camera.  On previous Windows Mobile devices I’ve owned I’ve always felt there were too many buttons on the sides.  The Blackjack II is very simple in the fact that it only has two buttons not located on the front of the device:  Power and Volume.  This makes the device more one hand friendly.  A big plus over phones like the 8525.

GPS – B+
image The Blackjack II has an integrated GPS SiRF III chip.  This means the phone is capable of being a real GPS not a simulated GPS using cell towers.  Available for $9.99 a month users can select to download and install Telenav on the Blackjack via a program provided out of the box.  I chose to enable this feature since I don’t have a portable GPS and I because of the fact I had removed the $10 fee for push to talk on my account I never used.  The only reason the GPS gets a B+ rating is due to the speed of locking onto satellites while moving.  I had a couple of instances whereby it took a long time to get a signal because I was moving.  As soon as I stopped it locked on.  Once Telenav is setup the Blackjack II speaks directions and offers a lot of the same functionality a regular GPS system offers like finding places nearby, gas prices, favorite locations, hot spots and more.  Telenav is truly a remarkable application and I have been impressed using it.   

As I mentioned earlier I installed Google Maps on the phone as well.  I mainly wanted to try Google Maps with the GPS built into the phone.  I selected the option to enable the GPS option in the menu and once the GPS locked on Google Maps was tracking me as I drove down the road.  While not anywhere near the features of Telenav Google Maps can provide basic GPS functionality.  I found myself launching Telenav much more often since it included spoken directions.  While driving it makes all the difference in the world to hear when you are supposed to turn rather than constantly looking down.  Not to mention being more dangerous.  If you decide to buy this phone and travel a lot, pay the $10 for Telenav, it is worth it.  If you just want to occasionally  do some GPS directions for fun you could probably stick with Google Maps.

Video Share – D
The phone supports a feature called Video Share.  This allows you to conduct one-way video calls.  At $9.99 for 60 minutes of use I don’t suspect many people are going to use this feature.  It is the first phone to support this option from AT&T but the cost is more than likely going to keep 99% of the population from using it.  The other problem is the phone on the other side must support it as well.  So unless you are a road warrior and your spouse has the same phone it isn’t even looking into.

Camera – B
The 2.0 mega pixel camera included with the Blackjack II is decent but will not break any land speed records.  The camera is very sensitive to movement.  I took several photos where I thought I was being extremely still.  But when viewing the picture it turned out to be very blurry.  This may be due to the lengthy shutter speed of the camera.  Once the button is pressed it takes several seconds for the picture to get taken which requires you to keep still longer.   The video capability of the camera will work well enough to post a video to YouTube if needed by the way.

Bluetooth Hands Free Driving – A+
While not a feature of the phone itself I use Bluetooth to get hands free driving all the time.   My Chrysler Aspen has a system called UConnect which supports Bluetooth hands free driving.  Once the phone is paired with UConnect and someone calls while driving a simple press of the UConnect button located on the review mirror answers the call.  People I have talked to say they can’t tell the difference either way and the nice thing is their voice comes across over the speakers.  If a CD is playing in the player when a call comes in the CD is paused automatically while the call occurs. 

There are times when driving with passengers that you don’t want them eaves dropping on the conversation (could be a client or a co-worker).  The Blackjack II has the option to enable an auto pop-up feature which I have found to be the extremely useful to avoid these situations.  Once it detects the hands free headset it pops a dialog to ask if you want to connect or not.  Previously with my 8525 and my 2125 if the truck was running, calls went through UConnect. 

Note:  Previous phones I have had did not pair very well with UConnect but the Blackjack II synced up quickly with no problem.

One Handed Usability – A+
The Blackjack II is extremely easy to use with one hand.  Of all the one handed keyboard devices I’ve used the Blackjack II takes the prize.  It is due to the small form factor and the jog wheel being on the front of the device.  Even with one hand it is easy to navigate calls, email and web surfing.  Once you get used to where applications are in the menus it becomes a quick series of keys to launch one.   The shortcut keys on the front to launch email, camera or turn the phone to silent make it easy to navigate with one hand.

Overall Rating- A-
As far as an overall rating I give the Blackjack II an A-.  I feel that I am well connected because of the 3G data support, built-in GPS, and direct push email.  It is great to be back to a form factor that I truly feel is the best of both worlds (8525 / 2125).  The full QWERTY keyboard makes it a snap to reply to text messages, emails, Twitter and surf the web.  It isn’t anywhere as bulky as the 8525 nor as cumbersome to operate as the 2125 (no keyboard).  For me the Blackjack II combines the best of both worlds and offers a geek like me a good balance of power but in a phone that I don’t feel like I’m carrying around a brick.  My wife’s cell phone plan is up for renewal in February, I am considering getting her one and enabling GPS on it with Telenav so when she travels she doesn’t have to worry about directions and getting lost.

 

Exposing a WCF Service With Multiple Bindings and Endpoints

Posted by Keith Elder | Posted in .Net, WCF | Posted on 17-01-2008

Windows Communication Foundation (henceforth abbreviated as WCF) supports multiple bindings that allows developers to expose their services in a variety of ways.  What this means is a developer can create a service once and then expose it to support net.tcp:// or http:// and various versions of http:// (Soap1.1, Soap1.2, WS*, JSON, etc).  This can be useful if a service crosses boundaries between intranet and extranet applications for example.  This article walks through the steps to configure a service to support multiple bindings with Visual Studio 2008 and the .Net 3.5 framework.  For those that want to jump directly to the sample solution it can be found at the end of this article.

Setting Up The Test Solution

Below you will find the steps used to create the sample solution.  I’ll explain a few WCF concepts along the way but probably not all of them.  I will however link to relevant documentation where applicable.  To get started, within Visual Studio 2008 create a new WCF Service Application project.  This template can be found within the web section of Visual Studio 2008 as seen here.

image

After the project is initialized modify it so it contains four files, note this will require deleting the default files:  IMyService.cs, Service.cs, Service.svc, and Web.Config.  Once you have cleaned up the default template, it should look like this:

image

Creating Our Service Contract
The IMyService.cs file will contain our service contract.   In old ASMX services, in order to expose a method as a service one would attribute the method with the attribute of [WebMethod].  In WCF, everything is tied to a Service Contract.   By defining our service contract as an interface, any class that implements that interface can be exposed as a WCF service.  In order to achieve this a ServiceContract attribute will be placed on the interface and an OperationContract attribute will be placed on the method we want to expose for our service.  Our interface is going to be simple and will just contain one method to add two numbers.  Here is the sample.

[ServiceContract(Namespace="http://keithelder.net/Learn/WCF")]

    public interface IMyService

    {

        [OperationContract]

        int AddTwoNumbers(int x, int y);

    }

Implementing The Contract
The Service.cs file will contain the implementation of the IMyService interface.  Again, since we defined our contract as an interface our service class doesn’t need or have any attributes on it.  Here is the code:

    public class Service : IMyService
{
public int AddTwoNumbers(int x, int y)
{
return x + y;
}
}

Creating The Service Host
The file Service.svc will provide the endpoint for our URL that will be exposed by IIS.   Within this file, we only need one line which identifies the class that contains our service using the ServiceHost directive.  Here is the code for this file.

<%@ ServiceHost Language="C#" Debug="true" Service="WcfService1.Service" CodeBehind="Service.cs" %>

Now that we have the contract specified and implemented along with an endpoint to host the service everything from here on out is just configuration.  This is the beauty of WCF.   How we want the service to be exposed is stored within the web.config file.  There a lot of options in WCF and those just learning WCF are typically overwhelmed at the amount of options.  Luckily Microsoft has a tool to help us along.  The upside to this approach is we do not have to code how our service is secured as we did in older ASMX services.

Setting Up Basic Http Binding

Before we add multiple bindings to our single service, let’s focus on adding one binding.  In order to configure our service we are going to use the Microsoft Service Configuration Editor.  This editor allows us to configure our WCF service and can be launched right from Visual Studio.  To launch the editor, right click on the web.config file.  An option should be in the menu that says “Edit WCF Configuration”.  If it doesn’t don’t worry, sometimes, Visual Studio doesn’t pickup this option.  A trick is to go to the tools menu in Visual Studio and select the editor from there.  After the editor is launched it will then show up in the menu when you right click the web.config file.

For this example I have removed everything from the web.config file before launching so I can configure the service completely from scratch.  Opening the web.config with the editor shows a blank configuration as follows.

image

Follow the steps to initialize the configuration.

Step 1
Make sure your solution builds.

Step 2
Click “Create a New Service” in the right panel of the editor.

Step 3
Browse to the bin directory and select the WcfService1.dll (if the file doesn’t show up, go back to step 1)

image

Double click this file and select your service implementation.

image

Step 4
The service type should be displayed on the previous screen.  Press Next->.

Step 5
Select the contract.  This should automatically be selected to WcfService1.IMyService.  Press Next->.

Step 6
Since we are using IIS to host our service select the HTTP option for the communication mode.

image 

Step 7
Select the basic web services interoperability.  We’ll come back later on and add advance web services interoperability.

image

Step 8
Delete the “http://” from the address field.  Since we are developing we don’t know which port this will get assigned as of yet.
image

Press Next->.  A dialog will appear.  Select Yes.

image

Press Finish on the next screen.

At this point we have an almost configured service.  If you drill down into the Services folder within the tool down to the end point the configuration should look something like this.

image

Step 9
Let’s clean up our configuration.  In the screen shot above in the configuration section.  Give this endpoint a name of “Basic”.  This will let us know later on this endpoint is our BasicHttpBinding configuration which supports the SOAP1.1 protocol.

Step 10
Click on the “Services” folder in the editor.

image

Step 11
Next to the binding configuration there is a link that says “Click to Create”.  Click this link to create a binding configuration.  While this isn’t necessary in all instances it is a good practice to have a binding configuration.  This gives you more control over how your binding is configured and is a good practice to initialize early on.

Clicking this link will create a new binding.  In the name field under the configuration section, give it a name of “Basic”.  The defaults for this example are fine.

image

Note:  Clicking on the Security tab in the above screen the Mode should be set to None. 

At this point you should be able to save your configuration and press F5 in Visual Studio to launch your service in debug mode.  You should be presented with a web page that looks like the following:

image

Exposing Our Service’s WSDL

Unlike previous ASMX services, the WSDL (web service definition language) for WCF services is not automatically generated.  The previous image even tells us that “Metadata publishing for this service is currently disabled.”.  This is because we haven’t configured our service to expose any meta data about it.  To expose a WSDL for a service we need to configure our service to provide meta information.  Note:  The mexHttpBinding is also used to share meta information about a service.  While the name isn’t very “gump” it stands for Meta Data Exchange.  To get started configuring the service to expose the WSDL follow the following steps.

Step 1
Under the Advanced folder in the editor, select the “Service Behaviors”.

image

Click the “New Service Behavior Configuration” link in the right pane.

Step 2
Name the behavior “ServiceBehavior”.  In the bottom section press the add button and select the “serviceMetaData” option and press Add.

image

The end result should look like the following.

image

Step 3
Under the “Service Behaviors” folder, select the newly created “ServiceBehavior” option and then the “serviceMetadata” option underneath that.  In the right pane change the HttpGetEnabled to True.

image

Step 4
Select the service under the Services folder and apply the new service behavior to the service.

image

Step 5
Save the configuration and reload the service in the browser.  The page should be changed to the following:

image

Clicking on the link should display the WSDL for the service.

At this point our service is configured to support the SOAP1.1 protocol through the BasicHttpBinding and is also exposing the WSDL for the service. What if we want to also expose the service with the SOAP1.2 protocol and support secure and non-secure messages?  No problem.  We can expose the service all of those ways without touching any code only the configuration.

Adding More Bindings and Endpoints To Our Service

Now that we have the basic binding working let’s expose two additional endpoints for our service which support the WS* protocol but are configured differently.  We’ll expose a plain SOAP1.2 protocol message using the wsHttpBinding that is a plain message as well as a secured message using the wsHttpBinding.

Step 1
Under the Services folder select the EndPoints folder for the service and add a new service endpoint.  Give it a name of WsPlain.  This endpoint will serve as a standard SOAP1.2 message that isn’t encrypted.  Change the binding to wsHttpBinding.  In the address field, copy the address from your browser.  In my case, my project used the address of http://localhost:5606/Service.svc.  Paste this into the address field and add /WsPlain to the end of it so it looks like the following:

http://localhost:5606/Service.svc/WsPlain 

Each binding for each endpoint has to have a separate address.  Doing it this way allows us to keep our one service file and offer it up in various configurations.  In the contract option browse to the DLL in the bin directory and select the contract used previously.  The end result should look similar to this:

image

Step 2
Just as we did previously, click on the Services folder and create a binding configuration for this endpoint.  Refer to step 10 and 11 above.

Provide a name of WsPlain for the new binding.  In the security tab change the mode to “None” and set all other options to false or none.  This is setting up our binding so there is no security on our message.  By default the binding is configured to be secure.

image

The final settings for the WsPlain endpoint should be similar to this.

image

Step 3
To configure a secure binding using wsHttpBinding follow these same steps above but leave the default values in the security tab of the binding configuration.  Call this end point and binding WsSecured.  The new endpoint should look like this:

image

That’s it, our service is now configured three different ways and is configured to support SOAP1.1, SOAP1.2 and WS*.  Pretty cool huh?

Testing Our Service

Now that our service is configured with three different bindings, let’s look at the messages as they go back and forth across the wire.  In order to do this we are going to borrow knowledge from a previous article I did called “How to Get Around WCF’s Lack of a Preview Web Page and Viewing WCF Messages“.  From this article we are going to borrow the MessageViewerInspector class and build a small windows application to view our messages.

If you are still following along, add a new project of type Windows Application to the existing solution and then copy the MessageViewerInspector class from the referenced article and add it to the project.

If you have been following along you may have noticed that we only have one WSDL.  The WSDL contains all of our endpoints.  Even though we have three endpoints, we still only have one WSDL.  In testing with some third party clients my experience has been that clients only generate proxies for the endpoints they understand.  Add a service reference to the service to the windows application.

For the user interface I decided to use several split panels and create a three panel layout.  Here is what it looks like.

image

The idea is simple.  When I click on each button I want it to invoke my service and then display the request and response messages the service is using.  To do this I created one service method called CallService which is passed the end point name the service is to invoke.

        private void CallService(string endPoint)
{
MessageViewerInspector inspector = new MessageViewerInspector();

ServiceReference1.MyServiceClient proxy = new WindowsFormsApplication1.ServiceReference1.MyServiceClient(endPoint);

proxy.Endpoint.Behaviors.Add(inspector);
proxy.AddTwoNumbers(12332, 12323);
proxy.Close();

richTextBox1.Text = inspector.RequestMessage;
richTextBox2.Text = inspector.ResponseMessage;
}

The endpoint is the name of the endpoint we specified in our configuration.  For example to invoke the secure wsHttpBinding it is called like this.

CallService("WsSecured");

The first thing created is create the inspector so messages can be viewed coming in and out of the proxy class.  Once the proxy method is called we can then grab the messages and put them into the RichTextBox control on the form.  Here are screen shots of each call to the service.

BasicHttpBinding – SOAP1.1

image

WsHttpBinding – SOAP1.2 Plain / Unsecured

image

WsHttpBinding – SOAP1.2 Secured

image

Conclusion

WCF services are powerful as you have just seen.  It is possible to expose a single service in more ways than just one.  This allows developers to support things like net.tcp binary messages and SOAP messages from the same service.  If you are an enterprise developer supporting multiple clients this is a blessing since it means .Net consumers of your service can use TCP while other consumers use WS*.  The nice thing is the developer doesn’t have to code it, just declaratively express how he wants the service configured. 

Download Sample Solution:

Like this article? kick it on DotNetKicks.com

My Codemash Podcast Is Up!

Posted by Keith Elder | Posted in .Net, Smart Clients, Speaking | Posted on 15-01-2008

Chris Woodruff caught me at Codemash and we did a podcast together.  It was a very free flowing conversation.  We covered a lot of topics including my open source background, how I got started in .Net, Smart Clients vs Web Applications, WPF, music, the value of learning multiple languages and why Codemash is so unique.

You can grab the podcast here:

http://codemash.podbean.com/2008/01/15/codemash-2008-interview-with-keith-elder/

And you can also try playing the podcast below.


Powered by Podbean.com

By the way there are other podcasts from Dustin Campbell, Sara Ford and Michael Rozlog as well on the Codemash web site.

How to Get Around WCF’s Lack of a Preview Web Page And Viewing WCF Messages

Posted by Keith Elder | Posted in Asp.Net, WCF, Web Services | Posted on 15-01-2008

I have a love hate relationship with Windows Communication Foundation.  I love it because the concepts of building a service are so simple.  I love the fact that I can expose the same service three different ways supporting different interoperability points within the business.  I also hate it because a there are so many ways to configure the service it can get daunting at times.  Configuration isn’t as bad as the built-in lack of ability to view messages.  WCF doesn’t provide a sample message page describing the SOAP messages for a request and response like the old ASMX services.  While this may not seem like a big deal there are systems I am having to interop with that can’t simply point to a WSDL and generate a proxy client from it (yes there still are some out there) and it is a problem.

The Problem

WCF offers a lot of binding options but when interopping with non .Net clients the basicHttpBinding is the obvious choice because it is the simplest.  If you can’t get that to work, good luck on the other ones.   Here is the quote from the MSDN pages about basicHttpBinding.

Represents a binding that a Windows Communication Foundation (WCF) service can use to configure and expose endpoints that are able to communicate with ASMX-based Web services and clients and other services that conform to the WS-I Basic Profile 1.1.

Changing bindings is really easy in WCF.  In VS2008, right click the config file in the project and select “Edit the WCF Configuration”.   The Microsoft Service Configuration Editor will then appear.  In the Services folder select the endpoint you want to change and in the binding section select the one you want.  This is the easy part.  The hard part is showing someone the message tied to the binding the service expects if they cannot generate a proxy within their language or platform from a WSDL.  In old ASMX services if the URL of the service was visited it would generate some nice pages for us automagically like this:

ASMX services display methods in service 

image

ASMX services when running locally could be easily tested with a prebuilt form

image

ASMX services provided a sample request and response XML message 

image

To the contrary WCF only provides .Net client info

 image

Yeah it is real useful isn’t it?

The Quick Fix

Why this is the case I have no good explanation.  One would at least think if the service was configured with the basicHttpBinding it would offer close to the same features of old.  Not the case though.  There is a big difference in the two obviously.  My first thought was to write a pluggin or some code that would provide the same functionality the old ASMX services do but I realized I had enough side projects to work on and I didn’t have the time.  Thus I was looking for a quick alternative to the problem.

The very first thing I did was open the Microsoft Service Configuration Editor and enable Message Logging in the Diagnostics folder.

image

With this enabled messages are written to the client or server.  It is a nice built-in feature that takes a lot more work with ASMX services.  With message logging enabled I ran my test service which added two numbers and then opened the message log using the SvcTraceViewer.exe.  SvcTraceViewer is part of the Windows SDK and does not ship with Visual Studio by the way.  Here are the messages generated in the log and viewed via the trace viewer.

image

This almost gave me the result I wanted.  Notice that it includes a lot of extra nodes and information.  The only nodes we care about start and end with <s:Envelope />.  This is our actual SOAP message.  If the trace option is enabled in WCF via diagnostics even more information is shown within this message.  I wanted to know the *exact* message that was going over the wire and back and I wanted to be able to look at it as I wrote my service or debugged it not having to launch a new tool.  Here is what I came up with.

The Programmatic Fix

WCF has tons of hooks and hooks mean extensibility.  My goal was simple.  I wanted to capture the request before it left my client and then capture the response message when it was received.  At first I thought this was going to be hard.  Then I stumbled upon an interface called IClientMessageInspector and my troubles were then over.

The first thing I did was create a class called MessageViewerInspector which implemented two interfaces.  The IClientMessageInspector and IEndPointBehavior.  Then I added two properties to my object which contained the request and response messages.  After that I implemented both interfaces with the object.  The IEndPointBehavior interface has four methods that need implementing but I only needed to use one which was ApplyClientBehavior.  In this method I added my MessageViewerInspector to the clientRuntime.   The IClientMessageInspector has two methods that need to be implemented which are the before and after.  This is where we get to see the messages before they leave the client and as soon as they come back.  Here is the complete object that will allow us to view our messages in WCF.

    /// <summary>
    /// Provides a custom behavior that allows a client to capture messages and log
    /// them or assist in debugging messages.
    /// </summary>
    public class MessageViewerInspector : IEndpointBehavior, IClientMessageInspector
    {
        
        #region Properties
        public string RequestMessage { get; set; }
        public string ResponseMessage { get; set; }
        #endregion

        #region IEndpointBehavior Members
        public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {

        }

        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
            // adds our inspector to the runtime
            clientRuntime.MessageInspectors.Add(this);
        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {

        }

        public void Validate(ServiceEndpoint endpoint)
        {

        }
        #endregion

        #region IClientMessageInspector Members
        void IClientMessageInspector.AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
        {
            this.ResponseMessage = reply.ToString();
        }

        object IClientMessageInspector.BeforeSendRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel)
        {
            this.RequestMessage = request.ToString();
            return null;
        }
        #endregion
    }

All that was left was to add this custom inspector to the proxy as an endpoint behavior and we can see our messages coming and going.

Here is the code I added to my sample console application to test.

    class Program
    {
        static void Main(string[] args)
        {
            MessageViewerInspector inspector = new MessageViewerInspector();

            ServiceReference1.Service1Client proxy = new ConsoleApplication1.ServiceReference1.Service1Client();

            proxy.Endpoint.Behaviors.Add(inspector);
            proxy.AddTwoNumbers(12332, 12323);
            proxy.Close();

            Console.WriteLine("Request message:");
            Console.WriteLine(inspector.RequestMessage);
            Console.WriteLine(String.Empty);
            Console.WriteLine("Response message:");
            Console.WriteLine(inspector.ResponseMessage);
            Console.ReadLine();
        }
    }

As you can see above I created an instance of my new inspector and then added it to my proxy class.  After my request ran I simply printed the messages to the console as seen here.

image

The really nice thing with this approach that I liked is I now have the ability to view the request and response objects by simply placing a break point in either the after or before methods in the inspector class.  For example, here is a QuickWatch of the request object:

image

Now that the basic plumbing is in place we can do all sorts of things.  For example if an exception is thrown we could now log the specific message that was requested to cause our client to throw an exception.  Of course this also solves my original problem of being able to grab the specific messages sent and received and works with all bindings supported.  I hope this helps. 

The Redneck Elder

Posted by Keith Elder | Posted in Funny Stuff | Posted on 15-01-2008

Today was my four year anniversary at Quicken Loans.  Anniversary day at Quicken is always fun because typically what happens is your team leader writes up an email about your accomplishments and emails it to all of Information Technology or various teams.  It is used as a time to say thank you for all of the hard work you have done at the company.  The rest of the day you are flooded with congratulations and funny responses to fill your day.  It is one of those nice things that makes us different and it is always nice to get propped out to your teammates. 

One of the guys took the time to make this commemorative photo to capture the occasion.  Oh how the Northerners I work with make me laugh.  Enjoy.

clip_image001