Archive for category C#.net
Globalization “Current Culture” settings in the web.config (ASP.NET)
Quick example of the current culture switch in the web.config of a dot net project.
<configuration>
<system.web>
<globalization fileEncoding=”utf-8″ requestEncoding=”utf-8″ responseEncoding=”utf-8″ culture=”en-GB” uiCulture=”en-GB”/>
</system.web>
</configuration>
Quick XML serialisation of an object..
Posted by shawson in .net, C#.net, Web Services, XML on June 25th, 2009
Quick code snippet for taking a serialisable object, serialising to XMl then spitting out a string for you to write out to a debug log or something- i used this for spitting back the responses I was getting from a web service call i was making
StringBuilder sb_xml = new StringBuilder();
XmlSerializer s = new XmlSerializer( typeof( Hachette.Checkout.Vista.Stock.ProductStockLiteResultResponse ) );
StringWriter w = new StringWriter(sb_xml);
s.Serialize(w, ws_response);
w.Close();
Response.Write("<!-- Response = " + sb_xml + " -->");
Web Services Debugging with Fiddler 2
Posted by shawson in .net, C#.net, Web Services on May 26th, 2009
We have a bunch of e-commerce web services available here and we have the envyable task of supporting numerous third party developers using a plethora of different languages, which often don’t have native support for SOAP web services, meaning they have to hand craft their SOAP calls. Seeing that we are totally dot net, we only ever interface with our services using the proxy classes generated from the WSDL files by the .net framework so when they send us reams of XML with a few question marks it’s difficult for us to diagnose the fault.

Fiddler2 Web Service SOAP Debugging
The answer I’ve found is to use Fiddler2 which is a HTTP sniffer application which monitors all your HTTP traffic and allows you to inspect whats going on. So fire up fiddler2 FIRST, then fireup your test application (which generates the web service calls), place a call and watch it being logged on fiddler.
It’s imperative that you start Fiddler FIRST though or it won’t hook in and capture your SOAP calls!
You can download Fiddler 2 from here
As you can see from the screen shot on the right, the outgoing request is shown in the top right pane, and the response in the bottom right.
Creating a re-usable shopping basket with Generics in C#
We recently had troubles with our new reusable basket we’re developing at work. We sell books, and most books come from one central data source which for the sake of this article i shall call CDS- so we have a CDS entities project which holds elements Group, Book, Author etc. However not all of our sites run use the CDS for their data- so we have other projects with their own elements for Book, Author, Category etc which hold their own data.
We wanted to create a central project containing the basket functionality which we could drop into any project and use. So the basket needs to be able to add lots of different types of books- some from the CDS entity library or some any other entity library we use. To do this we created an Interfaces project which holds an interface called IBook which held the minimum data required by the basket such as Title, Price, ISBN, Cover Image. This project is then referenced by the sites, the entity libraries (like CDS or any other’s we build) and the basket itself.
Adding the book to the basket was fine, however the problem arose when viewing items- the basket needed to be able to load the book data back- this is fine we thought- we would simply add a load() method to IBook, however this didnt solve the problem as the basket only had IBook signature of load- it didn’t know which concrete implementation of load to run.
The answer was to use generics. We added a generic parameter to the top of the basket which accepted a type which it assumed to be derived from IBook and always have a default constructor, like so;
public class Basket<T> where T : Hachette.ProcessLayer.IBook, new() {
private List<BasketItem<T>> _basket_items;
...
}
Likewise we made the basket item’s generic, and pass the type T from the basket to the basket item when instanciating them- the basket items are then able to define a real book instance of type T so it knows which concrete book it’s dealing with, so it can call the correct load method. The use of generics here means the basket object can reference a real book class from the CDS project (in a separate assembly) without having to reference that assembly, meaning we can drop in any type of book from any assembly for future sites, and with the parent website stating the type to use when creating the basket, the basket can remain completely independent.
SessionId re-generated on each page hit
If the session is not used at all on a page then the sessionId will get trashed. To maintain the same sessionId, when you have pages which don’t use the session, you just have to add some kind of call
protected void Page_PreInit(object sender, EventArgs e)
{
//Need this line otherwise it resets the Session Id everytime!
Session["keepme"] = "True";
2 tabs fresh!
In visual studio 2008 you'll notice a lot of operators such as foreach/ switch etc etc are listed as snippets in the intellisense menu, however when you hit tab to insert them it just autocompletes the keyword- if you hit tab twice it will insert the full snippet, giving you a dummy template usage for that keyword!
Useful commonly used C# functions
Another useful site- i was after a Proper Case (PCase) style function for c# and found this online;
public static String PCase(String strParam)
{
String strProper = strParam.Substring(0, 1).ToUpper();
strParam = strParam.Substring(1).ToLower();
String strPrev = "";
for (int iIndex = 0; iIndex < strParam.Length; iIndex++)
{
if (iIndex > 1)
{
strPrev = strParam.Substring(iIndex - 1, 1);
}
if (strPrev.Equals(" ") ||
strPrev.Equals("\t") ||
strPrev.Equals("\n") ||
strPrev.Equals("."))
{
strProper += strParam.Substring(iIndex, 1).ToUpper();
}
else
{
strProper += strParam.Substring(iIndex, 1);
}
}
return strProper;
}
This and a whole bunch of other useful functions can be found here.
VB to C# Code Converter
Just found a cool tool online for converting vb code to c#, and vice-versa. It's not 100% perfect so make sure you have a look through what it turfs out, but it's handy for converting small code snippets of example functions you find online. It's up on the DeveloperFusion website here.
Google Code Highlighter
You may have noticed that the little snippets of code I’ve been dropping into these post’s suddenly got a bit swankier looking. This is down to a new javascript tool I found for use on blogs and the like, for highlighting code you post on your pages.
Basically you download the zip file- unzip it and copy the javascripts and css files over, then add a few lines of code to the head of your page;
You then put your code in-between >pre< tags and set a name attribute to “code” and set the class to whatever syntax you have- it has support for a whole bunch of languages including c#, vb, php, javascript, css, sql and a whole bunch of others. You can grab this plug-in from here
The install was easy- but a couple of bits did catch me out- the code sample above was the example they give you in the install page. So I blindly copied over this code to the head of my blog, and copied all the files from the zip file over to my sites js and css folders. I found that html highlighting worked fine and so did c# but vb and sql weren’t happening… It eventually clicked that you need to add the references to the additional syntax types you need as the example only has a line for the CS and XML/HTML “brush” libraries. So I popped those lines in and it worked fine.
The other problem that I had, and indeed still have until I get home to actually edit the code, is compatibility with TinyMCE which is the editor used by default in BlogEngine. I found a couple of articles which might help with this but won’t know till tonight;
- TinyMCE Code highlighter Plugin to work with Google Syntaxt Highlighter
- TinyMCE configuration settings to help everything get along a little better
As mentioned on the Wiki pages there is also the issue that adding a name value to a pre tag, isn’t valid xhtml.