Tuesday, August 11, 2009

using statement causes undesired result in WCF client.

I have a WCF service client which is a WinForms desktop application.  I have a method that is responsible for uploading certain information by consuming the WCF service.  I was initializing the proxy as follows:

using ( proxy = new WCFServiceClient. WCFServiceClient() ) {
proxy = new WCFServiceClient. WCFServiceClient() ;
proxy.UploadInfo();
}

It was working just fine.  Then I had to make some changes (added a new method to the business logic layer called by the service) on the server side, it compiled without any error and passed the unit test.  So I published it to my test site and ran a unit test against the test site.  To my surprise, I started getting the following error message: 
The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state.

After bit of searching, I found this article.

According to this artical, one "should not use the C# "using" statement to automatically clean up resources when using a typed client".  The reason being, "The C# "using" statement results in a call to Dispose(). This is the same as Close(), which may throw exceptions when a network error occurs. Because the call to Dispose() happens implicitly at the closing brace of the "using" block, this source of exceptions is likely to go unnoticed both by people writing the code and reading the code. This represents a potential source of application errors."  That was exactly what was going on with my code.  The "using" statement was masking the actual error that was occurring on the server.  After removing the "using" statement and running the test again, I got the actual error which was the service couldn't find the new method that I had just added.  It turned out that the publishing tool didn't update the business logic DLLs.  The problem went away after forcing a replacement of the BLL DLLs.



Tuesday, August 4, 2009

Display Grid Page info ("Page # of Total Page Count")

The GridView control has the options to enable paging.  You can also specify different paging modes (NextPreviousFirstLast, Numeric, etc.) but it does not provide a way to display page info in the format of "Page 1 of 10" when you're in the mode of NextPriviousFirstLast mode.  So I decided to add my own.  Below the GridView control, I added a label control as follows:



I don't like to hard code strings into code if I can avoid it for internationlization reasons and for ease of editing at one place.  So I put all my messages in a resource file.   To display "Page # of #", I create a string resource named "PageNumberOfTotal" and its content is "Page {0} of {1}".  Then I handle the DataBound event of the GridView control as follows:

        protected void gdRecords_DataBound(object sender, EventArgs e)
        {
//If there are records to display, then display the message
            if (gdRecords.Rows.Count > 0)
            {
                lblNumberOfRecords.Visible = true;
                lblNumberOfRecords.Text = string.Format(
                   MessageResource.Properties.Resources.PageNumberOfTotal,
                    gdRecords.PageIndex + 1,
                    gdRecords.PageCount);
            } //If there isn't any records to display, the hide the label control
            else lblNumberOfRecords.Visible = false;
        }

About Cullen

My photo
Christian, Father, Software Developer/Architect who enjoys technology and using it to make people's lives easier!

Followers