Tips and Tricks in a world of Mix

Archive for December, 2010


C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_compiler.exe -p C:\Inetpub\wwwroot\ProjectName\SubProjectName_deploy -v / C:\Inetpub\wwwroot\ProjectName\SubProjectName_precompiled -u -f -fixednames -c -errorstack


Create an WCF AJAX-enabled service and read data from external sources


Well, here’s the thing. What I needed to create was a small simple example (POC if you’d like)  of getting information from different external sources, like in Xml or Query String formats.  Requirements :

1)Write the aspx in such a way , that if tomorrow we’d add another type of external input context , I wouldn’t have to change it ( the aspx).

2)No postback aloud – we don’t want the page to render itself each time we hit the button .


My solution was to do it through Ajax-Enabled Web Service . Here is the explanation , you can download the code from here.

To create the ASP.NET client application


1. Open Visual Studio 2010.

2. From the File menu, select New, then Project, then Web, and then select ASP.NET Web Application.

3. Name the Project FillTableFromExternal and click OK.

To create the WCF AJAX-enabled service


1. Right-click the FillTableFromExternal project in the Solution Explorer window and select Add, then New Item, and then AJAX-enabled WCF Service.

2. Name the service MyGetDataService.svc in the Name box and click Add.

3. Open the MyGetDataService.svc.cs file.

4. Specify the Namespace for ServiceContractAttribute as FillTableFromExternal:

namespace FillTableFromExternal
[ServiceContract(Namespace = "FillTableFromExternal")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyGetDataService
// Add more operations here and mark them with [OperationContract]

5. Implement the operations in the service.

-Add the OperationContract Attribute to each of the operations to indicate that they are part of the contract.

-[WebGet(ResponseFormat = WebMessageFormat.Json , BodyStyle = WebMessageBodyStyle.WrappedRequest  )]  – is defining in what format the data is returning to the caller .

The following example implements a method that returns MyData object .

MyData object is actually is a basic from of BL . Some separate component , that will be changed if we add some other logic in the future.

public class MyGetDataService
[WebGet(ResponseFormat = WebMessageFormat.Json , BodyStyle = WebMessageBodyStyle.WrappedRequest  )]
public MyData  GetDataFunc(int ReturningTypeEnum)
return new MyData(ReturningTypeEnum);

Adding The Business Logic component


1. Right-click the FillTableFromExternal project in the Solution Explorer window and select Add, then New Item, and then Class.

2. Name the service MyData.cs in the Name box and click Add.

3. Build a constructor :

public MyData(int FormatType)
switch (FormatType)
case 1://Xml
case 2://QueryString

So . in this constructor we are only switching to some logic by a parameter of a FormatType we were asked to return .

The functions are working as some basic type of providers analyzing the incoming data from predefined URI’s .

4.The predefined URI’s  will be defined as private member in the class and added as keys to the web.config – in the future , if the external service will change its location all you’ll need is to adjust a web.config , without any recompilation of the whole together application and the drag of the republishing an so on .

Private member in the class :

private string XmlUri = ConfigurationManager.AppSettings.Get("XmlUri");
private string QueryStringUri = ConfigurationManager.AppSettings.Get("QueryStringUri");

The web.config addition :

<appSettings >
<add  key ="XmlUri" value ="http://localhost:65304/ExternalXml.aspx"/&gt;
<add key ="QueryStringUri"  value ="http://localhost:65304/ExternalQueryString.aspx&quot; />

(as you can see I’ve created the externals inside my project , but in real life you’ll get it from third party).

5. Add some container to carry your data. In my case I’ll have a structure of two fields only  – ( transaction id and status )  , so I’ve chosen the Dictionary and declared it as property :

public Dictionary<string, string> rows { get; set; }

6. Get and Analyze an Xml from predefined url :

private void GetXML(string url)
rows = new Dictionary<string, string>();
HttpWebRequest myWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
myWebRequest.Method = "GET";
// make request for web page
HttpWebResponse myWebResponse =(HttpWebResponse)myWebRequest.GetResponse();
StreamReader myWebSource = new StreamReader(myWebResponse.GetResponseStream());

string myPageSource = string.Empty;
myPageSource= myWebSource.ReadToEnd();
XmlDocument xd = new XmlDocument();
XmlNodeList xnl = xd.SelectNodes("//new");
foreach (XmlNode el in xnl)
string tran = el.SelectSingleNode("transaction").InnerText;
string status = el.SelectSingleNode("status").InnerText;

rows.Add(tran, status);



Through all the first part of the code we are getting the Xml into myPageSource and than we are analyzing the content to get each child into temporary parameters tran and status , afterwards we are adding it to the rows. Pay attention – I’ve initialized it at the beginning of the function. It’s a property and nothing is needed to be returned.

This ends the Initializing of the MyData Object for Xml Format.

7. Get and Analyze an Query String from predefined Url :

Same logic  , different libraries :

private void GetQueryString(string url)
rows = new Dictionary<string, string>();
HttpWebRequest myWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
myWebRequest.Method = "GET";
// make request for web page
HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse();
string tran = System.Web.HttpUtility.ParseQueryString(myWebResponse.ResponseUri.ToString())[0];
string status = System.Web.HttpUtility.ParseQueryString(myWebResponse.ResponseUri.ToString())["status"];
rows.Add(tran, status);

Here we’re ParseQueryString the myWebResponse  , entering the data into temporary parameters and adding it to our dictionary property .

This ends the Initializing of the MyData Object for Query String Format.

Creating Xml output from ASPX


Well , I didn’t have an Xml External Source , so I’ve created one :

1. Right-click the FillTableFromExternal project in the Solution Explorer window and select Add, then New Item, and then WebForm.

2. Name the service ExternalXml.aspx in the Name box and click Add.

3. Delete the files  :  ExternalXml.aspx.cs and ExternalXml.aspx.designer.cs

4. Copy and paste to aspx file :

<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Text" %>
<script language="C#" runat="server">

void Page_Load(object sender, EventArgs e)
// Create a new XmlTextWriter instance
XmlTextWriter writer = new
XmlTextWriter(Response.OutputStream, Encoding.UTF8);
// start writing!
// Creating the <first child> element
writer.WriteElementString("transaction", "1");
writer.WriteElementString("status", "Decline");
// Creating the <second child> element
writer.WriteElementString("transaction", "2");
writer.WriteElementString("status", "Accept");
Response.OutputStream will stream the xml to the caller by url.

So if we’ll address the url ( right-click the ExternalXml.aspx –> View in Browser ) we’ll get :

<?xml version="1.0" encoding="utf-8" ?>

This is our data , that will be analyzed in GetXML function.

Creating QueryString output from ASPX


1. Right-click the FillTableFromExternal project in the Solution Explorer window and select Add, then New Item, and then WebForm.

2. Name the service ExternalQueryString.aspx in the Name box and click Add.

3. Enter the ExternalQueryString.aspx.cs and add to the Page_Load the query string creation (automatically when called the page will redirect with some parameters (hardcoded just for example in our case) in the url:

private void Page_Load(object sender, System.EventArgs e)
string url = "Default.aspx?";
url += "transaction=" + "10" + "&";
url += "status=" + "QueryStatus1" ;

Lets check it out : ( right-click the ExternalQueryString.aspx –> View in Browser  ) …

What the heck are we getting – we are at our front page ?! That must be wrong..


So the point is that I want to redirect to some existing page , and I know that my front page will be alive , because we ar coming from there , so the only thing that we are interested in here is the address string  after the ? sign .


This is our data , that will be analyzed in GetQueryString function.

Configure the client to access the WCF service


1. Open the Default.aspx page and select the Design view.

2. From the View menu, select Toolbox.

3. Expand the AJAX Extensions node and drag and drop a ScriptManager on to the Default.aspx page.

4. Right-click the ScriptManager and select Properties.

5. Expand the Services collection in the Properties window to open up the ServiceReference Collection Editor window.

6. Click Add, specify MyGetDataService.svc as the Path referenced, and click OK.

(be advised , if the svc is in your project you better add it as relational path – ~/MyGetDataService.svc  , otherwise it may not find your service)

7. Expand the HTML node in the Toolbox and drag and drop an Input (Text) on to the Default.aspx page.

8. Expand the HTML node in the Toolbox and drag and drop an Input (Button) on to the Default.aspx page.

9. Right-click the Button and select Properties.

10. Change the Value field to Show Data.

11. Double-click the Button to access the JavaScript code.

12. Pass in the following JavaScript code within the <script> element.

<script language="javascript" type="text/javascript">
// <![CDATA[
function Button1_onclick() {
var service = new FillTableFromExternal.MyGetDataService();
service.GetDataFunc(parseInt($("#Text1").val()), onSuccess, null, null);
function onSuccess(result) {
function buildTable(tableData) {
var table = $("#grid");
table.html("");  //clear out the table if it was previously populated
var thead = $(‘thead tr’, table);
//create the table headers
thead.append(‘<th>Transaction ID </th>’);
//add the table rows
$(tableData).each(function (key, val) {
var tr = $(‘tr:last’, table);
tr.append(‘<td>’ + this.Key + ‘</td>’);
tr.append(‘<td>’ + this.Value + ‘</td>’);
// ]]>

In Button1_onclick we are accessing our service as has been defined in the ScriptManager –

you can see the reference at :

<asp:ScriptManager ID="ScriptManager1" runat="server">
<asp:ServiceReference Path="~/MyGetDataService.svc" />

Once the client recognized your Service you’ll have full intelisence for it.

We are accessing the GetDataFunc ,that we’ve written at the very beginning, passing the value of the textbox , using jquery syntax , we’ve added to the page.

buildTable(tableData) – will dynamically build a table using the property of the object  MyData named rows . So from result that we get we are passing the rows property value to the building function  -  buildTable(result.rows);

13. Add some div with table in it to the source code of the aspx :

<div class="centered" >
<table id="grid" ></table>

I’ve already added the class to the div .

This way our table will be at the middle of the page.

Another thing is the grid id of the table – this is the identifier in which we are building the table in the buildTable function  – ("#grid")

14. Let’s add some styling – go to Styles folder , and double-click Site.css.

15. Add to the file :

text-align: center;
div.centered table
margin: 0 auto;
text-align: left;

16.Double-click Scripts folder.

17.Double-click Site.Master

18.Drug and Drop the files into the header of the master – this will allow us to use the JQuery in the web site

<head runat="server">
<script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
you may do it relative..

That’s about it.

We are pressing F5 –

When we’ll enter 1 and press show data – we’ll get the Xml format to the client.


When we’ll enter 2 and press show data – we’ll get the Query String format to the client.




We used Ajax-Enabled WCF service to get different sorts of input data source .

1) There is no PostBack in our code.

2) When we’ll need to add another type of data – Data Services  or RIA Services or some not invented yet services , we will only adjust the MyData.cs part. I’ve made the handling of the formats in functions , but you can export it to a separated provider component if needed.

3) We are working with JQuery , which is always more fun and quick .

4) Added some styling just for the kicks of it.

You can download the sample code from here


So , if you liked it and used it , say thanks in comments.


Upgrade to MVC2


Well , the problem I’ve met was moving to a new computer.

All of a sudden my development project , that has been upgraded from MVC1 to MVC2 months ago , suddenly refused to work . My new computer never had the installation of MVC1. (You can check if the GAC is familiar with MVC1)


I’ve got message saying that I’ve got MVC1 references in my project :


The stack trace was :


So I started looking for MVC1. Well after a little wild I found this :


Through Red Gate’s .Net Reflector I opened a suspicious  .dll in this mortal sin , and found out that the Mvc it references is MVC1. Also in their site it said :

Important: xVal is recommended for use only with ASP.NET MVC 1.0

If you’re using ASP.NET MVC 2.0 or later, the framework has built-in support for generating client-side validation logic directly from model metadata. You can still use xVal if you wish, but it won’t be actively developed or supported.

My solution was


Welch Allyn IT3800 code interceptor


Look at the scanner jack.

If it looks like this:

, then it’s a keyboard wedge scanner. It acts like a keyboard: just types your barcode into an edit field.

If it looks like this:

, it’s a serial port scanner.

You need to create an instance of System.IO.Ports.SerialPort and use it to communicate with the scanner.

If it looks like this:

, it’s a USB scanner. From programmer’s point of view, it can be either a keyboard wedge or a serial port scanner.


Ours was wedge scanner. If we want to intercept the barcode input , for instance for validation or some logic decision where to show the input , then you should catch the Ctrl+b event , initialize a flag saying you’re now reading the barcode . On each KeyPress you’ll hit , if it’s not “Enter” (the end of Barcode input) and the flag of  (Ctrl+b)  (Start Barcode Read) is true , add each time add the value to some parameter. When the “Enter” comes you have all the bar code data in the parameter. :


  1: var barcodeWorking = false;
  2: var barcode = "";
  4: //This function fired when ctrl+b insert : begin reading with barcode
  5: function FireEventB_KeyAndONE_Key()//Override the function of KeboardUse.js
  6:       {
  7:           barcodeWorking = true;
  8:           window.focus();//כדי שהברקוד לא יכתב לרכיב טקסט בדף
  9:       }
 11: function KeyPress(theKey)
 12:       {
 13:            var key=theKey.keyCode || theKey.which;
 14:            if(barcodeWorking)
 15:            {
 16:                if(key == ENTER_KEY)//'Enter' pressed : end of the barcode
 17:                {
 18:                     alert(barcode); )//לעשות מה שצריך עם הברקוד
 19:                     barcode = "";
 20:                     barcodeWorking = false;
 21:                }
 22:            else
 23:            {
 24:                 barcode += String.fromCharCode(key);
 25:            } 
 26:         }
 27:       }


Don’t forget to connect the KeyPress to Body tag.

   1:  <body language="javascript" onload="windows_onload();" oncontextmenu='return false' onkeypress="KeyPress(event)">


The idea has been generated by me.

The source code (implementation) has been written by Sharon Said.

.Wsdl 404

After upgrading Windows Server 2003 to framework 3.5 and publishing some project to IIS6 , with files with extensions .wsdl , when we tried to browse the file from Published Virtual Directory in IIS we got 404 .
The solution was to enter to the properties of the project -> Virtual Directory (Home Directory) -> Configuration -> Find .wsdl extension -> Remove.

Another possible solution is to enter
Web Service Extensions -> Add a new Web Service Extention ->
add SOAPIS30.dll -> Allow

The second solution is not checked,because the first one worked for us , but you can try it.

Tag Cloud

%d bloggers like this: