Monday, August 3, 2009

Tip: Seting up the WCF wsDualHttpBinding or Duplex Service the correct way.

If you want to use the WCF wsDualHttpBinding to establish a consistent communication between a client and a server (i.e. server to send data to the client consistently), WCF has to create 2 channels that connect from the client to the server and from the server to the client respectively. In order to allow WCF to setup the links correctly, you have to ensure that the client and the server configurations are correct.
Programming a dual binding WCF program will require 2 ports, one for client to communicate with the server and one for the server to communicate back to the client. Basically when debugging a WCF service in your local machine, an example of the server configuration file looks like


The configuration file for the client looks like

<system.serviceModel>

<bindings>
<wsDualHttpBinding>
<binding name="WSDualHttpBinding_IStockService1" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
bypassProxyOnLocal="false" clientBaseAddress="http://localhost:8989/client/"
transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00" />
<security mode="Message">
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" />
</security>
</binding>
</wsDualHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost/DuplexGood/StockService.svc"
binding="wsDualHttpBinding" bindingConfiguration="WSDualHttpBinding_IStockService1"
contract="ServiceReference1.IStockService" name="WSDualHttpBinding_IStockService1">
<identity>
<userPrincipalName value="FTSB01NB05\ASPNET" />
</identity>
</endpoint>
</client>
</system.serviceModel>



The interesting part lies in the client configuration file where the two bolded lines of text from top to bottom, represent the client port and the server port respectively. If you did not specify any of the address correctly you will probably receive the following error:

HTTP could not register URL http://+:80/client/ because TCP port 80 is being used by another application.

The error above indicates that you set your client port number the same port number as the server.

OR

There was no endpoint listening at http://localhost:8989/Services/StockService.svc that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.

The error above indicates that the server endpoint is specified incorrectly and that WCF failed to find the service.

The server configuration looks like the following

<system.serviceModel>

<behaviors>
<serviceBehaviors>
<behavior name="mexBehavior">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>

<services>

<service behaviorConfiguration="mexBehavior" name="EssentialWCF.StockService">
<endpoint address="" binding="wsDualHttpBinding" contract="EssentialWCF.IStockService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>


Thursday, July 16, 2009

Tip: How to hide datagrid column in Windows Mobile 6.1

Scenario:
I have a datagrid that contains 3 columns; Product Code, Product Package, and Quantity. However, the datasource of the datagrid is a datatable with the name "ProductList" that contains Product Id, Product Code, Product Description, Package, and Qty. When the datatable is binded to the datagrid, all the fields in the datatable are shwon in the datagrid. So how do we hide those unwanted fields from showing in the front end?

Solution:
1. Create a DataGridTableStyle for the DataGrid and add in the styles for column: Product Code, Product Package, and Quantity.
2. In the DataGridTableStyle "MappingName" property, makes sure you put in "ProductList" as the value. This value tells the DataGrid to map the fields to the "ProductList" table and to hide those fields that are not stated in the GridColumnStyles collection.

Monday, July 13, 2009

Tip: Microsoft Service Factory Host Model Insight - The needs to include the "Service Implementation" layer.

Whether you are developing a WCF or ASMX service, the Microsoft Service Factory Host Model allows user to add in ".svc" or ".asmx" files that can be accessed from within a "Website" project that you will host using IIS. However, the problem is we cannot use the standard ASP.NET Application project to fit into the host model designer. The designer only recognize "Website" project and hence you wont be able to update the web.config or even add in the ".svc" file into the project using the designer. Even worst is, the "Website" project does not generate the necessary "Website.dll" file when you compile the project. This prevents you from generating the necessary proxy for your service when you execute "Add Service" function.

If you observed the example that comes with the Service Software Factory, you will notice that it contains another layer called the "Service Implementation". This separate dll in the solution represents a dll that you can include into the "Website" project that acts as a host for the service(s) that you are going to expose. By including the dll in the "Website" project, you will not have problem accessing the service(s) in the Host Model designer.

Monday, June 22, 2009

Tip: Solving jQuery Dialog Scrolling Problem.

Scenario:
Add a jQuery accordion widget into the jQuery dialog. The version of the jQuery used was 1.3.2 and the version for the jQuery UI was 1.7.2

Problem Statement:
The dialog vertical scrollbar did not actually scroll when the dialog content length is more than the dialog height.

Resolution:
Add a "position: relative" into the dialog "div", that will solve the scrolling problem. It's tested in IE7 and FF3.0

Example:
<div id="historyDialog" class="demo" title="Last 10 Orders" style="position:relative">

Sunday, May 17, 2009

Using RegExr to learn Regular Expression and create Regular Expression

Learning Regular Expression might not be a tough job but writing a correct regular expression requires experience and hell lot of patience. Especially for newbee, it will be tough job to apply what have been learnt from the text book or Internet without the help of a good utility.
In this case, I would recommend an utility that is known as RegExr. This utility allows you to see real time Regular Expression procesing against some text that you intend to extract from. Below is the picture that shows how you can extract the substring "]." that is located at the end of the string line.
This utility actually comes in two forms, a web based and a desktop based applications. You can download the utility from this url for free http://www.gskinner.com/RegExr/desktop/

Tuesday, March 3, 2009

Tip: Export a HTML table or a GridView with unicode characters such as Chinese to Excel

The ability to export data to Excel from a web page is a great ability. However, you might want to cater your web page for multi languages. Usually this is how you can export the data without the language support:

Response.Clear();
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition", "attachment;filename=" + strFileName);
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
MyCustomControl.RenderControl(htmlWrite); //This is where we call our custom object
//to render its' HTML, it can be an ascx control.
Response.Write(stringWrite.ToString());
Response.End();

The versiont that can export to Chinese zh-CN:

byte[] leader = new byte[2]; //To tell Excel engine to interpret the content correctly.

leader[0] = 0xFF;
leader[1] = 0xFE;
Response.Clear();
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition",
"attachment;filename=" + strFileName);

Response.ContentEncoding = Encoding.Unicode; //indicates that the content is unicode
Response.BinaryWrite(leader);
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
MyCustomControl.RenderControl(htmlWrite);
Response.Write(style);
Response.Write(stringWrite.ToString());
Response.End();

Wednesday, February 25, 2009

Tip: ASP.NET Button Post Back in a JQuery Dialog

Most of the time we want to build some ASP.NET controls inside a JQuery dialog and that when user click on the controls, they will post back to the web server. However, by nature if you put those controls inside a Jquery dialog, they will not behave as expected.

The reason is because JQuery builds the dialog outside of the "form" element in the form that you have created. Any control that is put outside the form will not post back. The following illustration shows the scenario that i am talking about.

<form id='formName'>     
</form>
<div id='dialog'>
    <input id='btnSubmit' type='button' />
</div>

In order to fix the problem we need to move the div section inside the form. In order to do that we can write something like this:

dlg.parent().parent().appendTo($("#formName"));

The code above appends the resulting dialog to the form element named 'formName'. The end result basically shows the following code structure.

<form id='formName'>
     <div id='dialog'>
         <input id='btnSubmit' type='button' />
     </div>
</form>
        

Thursday, February 12, 2009

Tip: Converting MS Ajax JSON DateTime to JS Date object

In my recent project, i have to use JSON Serialization to translate a datatable into JSON format. The table contains fields that are of DateTime data type. When the data is deserialized in the browser, the data is always represented in "/Date(1325926000000-0800)/".

I know i can use the the MS Ajax JS API "Sys.Serialization.JavaScriptSerializer.deserialize" to deserialize the data back into JS Date object but i dont want to include the immense MS JS script library into my page. So the option is to use the JS below to do the trick.

var dt    = data.updateDateTime; //assuming it's like /Date(1325926000000-0800)/
var s = eval(dt.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"));
dt = new Date(s);
    
var mil = Date.UTC(dt.getFullYear(),dt.getMonth(), dt.getDate(), dt.getHours(), dt.getMinutes(), dt.getSeconds(), dt.getMilliseconds()); //Miliseconds since January 1, 1970
dt      = new Date(mil); //Convert to local time
alert(dt);

Tuesday, January 13, 2009

Some thoughts on Google Chrome 1.0.154.43

This post is not meant to insult the Chrome team but instead i have to praise them to have done an excellent job in introducing a fast and different browser than the others. 

I have been using the Google Chrome browser for a few months now and i noticed the following shortcomings which i think can be further improved in future release of the Chrome.

1. I am a premium Rapidshare user and when i used Chrome to login, I tried to download files using FlashGet but FlashGet was not able to detect the Rapidshare cookies. This does not happen in IE or FireFox. In addition, Chrome could not seem to remember login cookies from some web sites that used to visit.

2. I tested a web project that i developed using the famous JQuery library with Chrome, some functions did not work as expected. What i believe is JQuery is suppose to be compatible cross browsers and should be working fine with Chrome.

3. Some existing CSSs are not working well with the Chrome. For example, some table formattings will be out of the ordinary with bad content alignment.

4. There's is no online bookmark storage available for use, therefore it's not really convenient to share the same bookmark across different PCs. In addition, if Chrome allows me to modify a bookmark icon, that will be more fancy to use.

Even though with all the temporary shortcomings, I still have high hope on getting a perfect browser in the future releases.