Wednesday, March 2, 2011

Retring WCF calls, using Func

Sometimes because the internet connection is not great, you might want to implement code to retry WCF calls.  However, instead of writing for loops on each clientproxy.MethodCall, you can write an extension method that retries it for you.  Since WCF calls has a single parameter - TWcfRequest and a single response - TWcfResponse, we can use the delegate Func<TWcfRequest, TWcfResponse> and pass the desired WCF method down this extension method.


public static class WcfClientBaseExtension
    {
        /// <summary>
        /// Retry WCF call multiple times
        /// </summary>
        /// <typeparam name="TChannel"></typeparam>
        /// <typeparam name="TWcfRequest"></typeparam>
        /// <typeparam name="TWcfResponse"></typeparam>
        /// <param name="client"></param>
        /// <param name="tryExecute"></param>
        /// <param name="wcfRequest"></param>
        /// <returns></returns>
        public static TWcfResponse ExecuteRepeatedly<TChannel, TWcfRequest, TWcfResponse>(this ClientBase<TChannel> client,
                    Func<TWcfRequest, TWcfResponse> tryExecute, TWcfRequest wcfRequest)
            where TChannel : class
            where TWcfRequest : class
            where TWcfResponse : class
        {
            int maxRetryCount = FrameworkConfigHelper.MaxWcfRetryCount;

            for (int currentRetryCount = 0; currentRetryCount < maxRetryCount; currentRetryCount++)
            {
                try
                {
                    return tryExecute(wcfRequest);
                }
                //SOAP Faults - business requirement
                catch (FaultException)
                {
                    //need to allow business exceptions to go to top level
                    throw;
                }
                //Connection timeout
                catch (EndpointNotFoundException)
                {
                    //endpoint don't respond in timely manner
                    //swallow technical exceptions unless it's the last retry
                    if (currentRetryCount == maxRetryCount - 1)
                        throw;
                }
                catch (Exception)
                {
                    //rethrow all other errors for now
                    throw;
                }
            }
            return null;
        }

       
    }


For usage:
var searchResponse = client.ExecuteRepeatedly<ICollateralRegistrationSearch, SearchByRegistrationNumberRequestType, SearchByRegistrationNumberResponseType>
                                            (client.SearchByRegistrationNumber, searchByRegistrationNumberRequestType);

Monday, January 24, 2011

SSRS tools

In addition to the Report Builder 3.0 tool from microsoft, there are also different ways to create RDL and RDLC reports:
http://msdn.microsoft.com/en-us/library/ms155792.aspx

Report builder provides a very quick report designer to play with your existing data.  It generates RDL and can be uploaded to the reporting server immediately.

Saturday, January 1, 2011

Passing Data from ViewModel to Javascript on the View

Option 1: construct the javascript into a string put it directly into the ViewData
ViewData["clientScript"] = "<script type='text/javascript'>initialize();addOverLay(-34.397, 150.644)</script>";

<%=ViewData["clientScript"]%>

This is of course prone to javascipt attacks

Option 2: Convert to Json(InfoList is of type string)

var js = new JavaScriptSerializer();
var listOfInfos = new List<string> {"abc", "cde"};

var viewModel = new InfoModel
               
{
                   
InfoList = js.Serialize(listOfInfos);
               
};
return View(viewModel);

In the View
   <script type="text/javascript">
      var result = <%= Model.InfoList%>
   </script>

Monday, December 27, 2010

Windows's Default Share c$

Because of Windows UAC, to allow c$ default share, you'll need to add the following key:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System

DWORD - LocalAccountTokenFilterPolicy and set the value to 1

Friday, December 24, 2010

Windows 2008 R2 random freezes

Microsoft has a hotfix for "a deadlock condition between the Lsass.exe process, the Redirected Drive Buffering Subsystem (Rdbss.sys) driver, and the Winsock kernel.

http://support.microsoft.com/kb/2265716

Wednesday, December 15, 2010

C# code formatting

http://www.manoli.net/csharpformat/format.aspx#html

For blogger.com, you'll need to use "Design" -> "Edit Html" -> Under <head>, paste
    <link href='http://www.manoli.net/csharpformat/csharp.css' rel='stylesheet' type='text/css'/>
See CSS paste below if you want to incorporate this
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #006080; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }

JQuery $.ajax call with custom error function

$.ajax({
        url: 'RequestSearchCertificate',
        type: "post",
        data: { registrationNumber: $('#ResultsRegistrationNumberValue').text(),
            changeNumber: $('#ResultsChangeNumberValue').text(),
            searchNumber: $('#ResultsSearchNumberValue').text()
        },
        success: function(response) {
            $('#GenerateCertificateResultDialog').html(response);
            $('#GenerateCertificateResultDialog').dialog("open");
        },
        error: function() {
            alert('error');
        }
    });