Tuesday, May 12, 2009

Weird things with LINQ - stripping unwanted unicodes.

So here was my problem, I needed to strip any unicode characters above 255.
At first I thought of using a regular expression to do a replace with but then the exclusion clause might be a bit long. Then I thought "can I do it with LINQ?".

Here is the result:

  private static string StripUniCode(string originalText)

        {

            var chars = originalText.ToCharArray();

            var result = from chr in chars

                        where chr <= 255

                        select chr;

            StringBuilder sb = new StringBuilder();

            sb.Append(result.ToArray<char>());

 

            return sb.ToString().Trim();

 

        }


Not brilliant but what the hell. You gotta love LINQ.
 

    

Thursday, January 08, 2009

Integration testing for WCF with MSTest

As part of my research on WCF (with an eye on upgrading our existing web services) I started investigating how to create automated integration tests for WCF services. With our current services we write our integration tests in MSTest. To run the tests we all also need to either have the services set-up in IIS or or spin them up in an instance of Visual Studio. This makes running the tests fiddly and also complicates if we want to automate them as part of the build process.

With WCF we now can self-host a service which gives us some new options in regard to testing.
Howard van Rooijen in his blog offered a solution (Configuring WCF services for Unit Testing) which created an internal ServiceHost wrapper class for a new instance of the service host for testing. Though this solution worked there was a couple of areas where I thought it could be improved.

What I have done is:
a) remove the need for creating and maintaining a configuration file
b) move the whole thing into a seperate generic class ( called ServiceManager) which the contract and service as type parameters.
c) simplifying the the set-up process so it is more "set and forget" for a set of tests
d) incorporated the ChannelFactory creation into the class.

The code for this can be downloaded from the following link: Service manager for WCF tests.

To use the ServiceManager class we first create an instance of it:


[TestClass]

public class WebServiceIntegrationTests

{

static string baseAddress = @"http://localhost:8000/WebServiceTests";

static ServiceManager<IPeopleService, PeopleServiceType> serviceManager =

new ServiceManager<IPeopleService, PeopleServiceType>(baseAddress);


Having do that we then add the following code to the class initialise and cleanup methods:

[ClassInitialize()]

public static void ClassInitialize(TestContext testContext)

{

serviceManager.StartService();

}

[ClassCleanup()]

public static void ClassCleanup()

{

serviceManager.StopService();

}


With the service of the service host running all we need to do is write our testing using an instance of the ChannelFactory class:


[TestMethod]

public void GetNewPersonFromService()

{

string firstName = "Bob";

string lastName = "Builder";

int age = 35;

RelationshipType relation = RelationshipType.Business;

using (var factory = serviceManager.GetChannelFactory())

{

IPeopleService client = factory.CreateChannel();

Person person = client.GetNewPerson(firstName, lastName, age, relation);

Assert.AreEqual<string>(firstName, person.FirstName);

Assert.AreEqual<string>(lastName, person.LastName);

Assert.AreEqual<int>(age, person.Age);

Assert.AreEqual<RelationshipType>(relation, person.Relationship);

}

}


The key thing with this solution is that it is very easy to set-up and we don't have to worry about configuration settings for the services since it is all done programmatically. This means that setting and maintaining the tests is a lot easier which is always a good thing.




Monday, October 13, 2008

Interesting article on locking

Thread locking is one of those things where one moment I understand it with crystal clarity and the next moment become a blithering idiot. One day I know it is going to stick.

In the mean time here is an interesting article by Jeff Moser on how locks lock

Tuesday, October 07, 2008

Why is it so difficult to find the price?

This is one of those things that has been of a point of contention for a while but this morning has really got my goat. why don't software vendors give the pricing of their product up front? Think about it. How many times have you gone to a vendor's web site and had to have drill to forever to get an actual price for the product you are interest. Are they ashamed of the price? Is their software so horrendously expensive that they think it will scare you off before you have even considered it? Who knows what they are thinking I am just as likely to not buy their product because I need a compass to find the price.

My example for today is Scooter Software and their product Beyond Compare. The product was mentioned by Scott Hanselman as his favourite diff tool so I thought would check it out since I am always on the look for new tools to add to the armoury. Anyway when you follow the link you are presented with a nicely designed little web site but no where can I find the price. So where is it? In this particular case you need to chose to purchase the product before you'll actually find out the price. Imagine if supermarkets operated that way.

So here is a word of advice to all those online software vendors, make the products pricing easy to find. Either have it on the front page or at least a link to a page with the pricing. Cost is one of the things that is considered before someone purchases something but I tell you now I will be less inclined to purchase something if I don't know how much it will cost.

Sunday, August 24, 2008

Rick Strahl on ASP.Net

Rich Strahl from West Wind technologies has written a wide ranging blog post on ASP.Net (ASP.NET gets no Respect).  I won't attempt to summarise it but it is worth a read.

I have worked with ASP.Net since 1.0 and having come from an ASP classic background I found the web forms model a nice change. True it has its issues but for me it always got the job done which is what you want. 

Sunday, April 20, 2008

Tools for exploring Linq

One year down and one year to go on my masters. I have month without study ahead of me so time to catch up on some of the new features in .Net 3.5. To this end I have decided to tackle Linq in all, at least of, its permutations.

Rather than spinning up an instance of VS2008 just to explore some simple examples and ideas I am a couple of tools to assist my explorations.

The first one is Snippet Compiler by Jeff Key. There has been a version of this around since .Net 1.1 and the latest version, Snippet Compiler Live 2008 Ultimate Edition for Developers (Alpha), is a real gem. This is a great little tool to have for testing bits of code or exploring what a class does. At the moment I have been using to play with the code examples in "Introducing Linq" which is a free e-book that can be found at http://introducinglinq.com/. The only gotcha you need to keep in mind is you need to reference the relevant libraries. For the underlying Linq stuff this System.Core.

The other tool that I have just come across this weekend is LinqPad by Joseph Albahari. This application has been written as tool to explore Linq and has some nice database features. I haven't had a real chance to play with it yet but it is one that I will be recommending to colleagues.

So there you go, if you are looking for a light-weight utility to explore Linq with either of these will do the job.

So it is a tip of the hat to Jeff and Joseph.

Monday, March 31, 2008

WCF and websites with multiple identities

The other day at work we were installing a bunch of stuff on a new production web server which has been set-up for load balancing. One of the services we had installed used WCF. Went to test it and it didn't work. Turns out that WCF has an issue with multiple identities being assigned to a website. It's one of those little gotchas that can catch you unawares on a roll-out.

Anyways Rob Reynolds provides a simple solution to the problem on his blog.

Publish Post

Friday, March 28, 2008

Where is ASP.NET 3.5 on IIS ?

Vijayshinva Karnure wrote a short on blog why you can't find a reference to .Net 3.5 in IIS. Kinda of thing that is easy to forget in the heat of the moment.

Monday, November 26, 2007

HttpHandler and Session State

Back doing web development at the moment and it is taking me a little while to get back up to speed.
Anyway for the project I am working on decided to create an HttpHandler as a Front Controller. All well and good until I tried to use the session state. Couldn't access it from the Handler or pages that were called through to. Bugger! Turns out you need to implement the IRequiresSessionState interface for the HttpHandler if you want to use the sesssion state.

You learn something new everyday.

Wednesday, October 24, 2007

Beneath the Radar

How time flies, it's been six months since I last posted. Things have been pretty busy though. Started a new job, started my Masters in Systems Development and I've been neck deep in WPF.
Unfortunately the project I was working on has been put on hold for a few months but it has given me a chance to come up for air.

Since the focus of the project I was working on was the development of a line-of-business application in WPF my intention for the next few posts is to cover some of the things I've run into during development. In part this I'm doing this for myself to keep track of ideas and solutions to problems but other people may also find it useful.

Thursday, April 12, 2007

Back to the Salt Mine

After two weeks as a man of leisure I have rejoined the working masses. Damn it was good while it lasted.

Spent most of the day setting up my computer and revelling in the joy of a clean machine. Unfortunately I have had to go back to working with Office 2003 which after using 2007 is a bit of a bore but thems the breaks. At my last gig I had a reasonable amount of freedom in terms of what software I could use, etc. With the new job I am not sure what the boundaries are so for the moment the path of least resistance is the best bet.

Thursday, April 05, 2007

Code Camp Oz 2007 - Some Thoughts

This is my third bloody attempt on writing up some thoughts on the last Oz Code Camp. Every time I start I get bogged trying to be too analytical about the whole thing. So this time I am just going to whack down some quick thoughts with complete disregard for continuity and form.

The main reason I want to get these thoughts down is that I feel that of the three code camps we have had so far in Australia that this was the best yet. Not to say that the previous two won't worthwhile but this year it feels  that all the bits came together in the right mix and captured the spirit of what Code Camp is about. The three things that I made this camp work for me were venue, pacing and the topics discussed.

The venue: This year we were back in the Wal Fife Theatre. Yes it is a lecture theatre but it has good sight lines and gives you a sense of being with a group of people. Last year the main presentations were in a hall (can't remember the name) which was a little too cavernous and isolating.

Pacing of the presentations: This year each speaker had 55 minutes for their presentation which is just the right amount to keep the presenters on point but give them the time to do justice to their topic. In addition the ratio of two sessions for each break period (morning tea, lunch, etc) reduced the data overload factor. Finally, unlike last year, there were no breakout or concurrent sessions. In my opinion having breakout sessions at Code Camp tends to be more disruptive than beneficial. Not only is it a case of the logistics involved with people moving between sessions and the corresponding impact that is bound to have on session running times, etc but you also lose continuity between attendees.

Content: This the big one isn't it. This year my feeling was that material that the presenters covered was a bit more "real world" in that it covered technologies and issues that us average Joes could run with. This is an important point. With Microsoft's new "openness" and almost obsessive releasing of CTP versions of up and coming technology we are all pretty well aware of what is coming down the track. Which is all well and good but, at least for some of us, gaining a better understanding of current technologies and how we can plug them into the work we are doing now is probably of greater benefit. And this is how I feel about this year's code camp. I came away from it feeling enthused about what could be done.

Finally I just want to say that without the effort and hard work put in by Mitch Denny and Greg Low to pull Code Camp Oz together there would be no code camp and for that these guys deserve our thanks.

The evils of PowerPoint

the following article from the Age newspaper on research done on the effectiveness of PowerPoint presentations. PowerPoint presentations a 'disaster'

Basically the research shows that it is more difficult to process information if it is presented in both written and spoken form at the same time. So to all of those presenters who insist on slavishly reading out their dot points from the slides, cease and desist.

Tuesday, March 27, 2007

Quitting My Job

I  am into my second work day after finishing up at Consolidated Travel. After five years and two months there it is a bit odd not having to think about it (but I am getting the hang of it :-)).

Now I am thinking about how to gear up for my new job which I start in a couple of weeks. There is part of my that just wants to kick back and do sweet FA but the other half is looking at the pile of books and magazines that are sitting unread or half read that I should start making a dent in.

For example I have Charles Petzold's "Applications = Code + Markup" which I have only just started in on it and it's a good two inches (5 cm) of book so maybe I'll dedicate my time off to that.

Sunday, March 04, 2007

My japan trip or the blog that never was

Well so much for my grand plans to blog about my trip to Japan. First I get nailed by power plug issues and a seriously flat battery (fixed that one in Hiroshima) only to discover that my accommodation in Tokyo was not quite as wi-fi as it claimed to be.

Let me say that the Bed & Breakfast Zen in Asakusa was actually damn fine accommodation. Basically you get the top floor of a private house.you have your own entrance, a small, though not quite functional kitchen, bathroom, bedroom and living room. On top of that Mrs. Takeuchi cooks a very good Japanese breakfast (I can't comment on the western style breakfast since I tend to go native when in Japan).

Ahh but the wi-fi issue. The main reason I chose B & B Zen was that it advertised itself as having wi-fi. Technically it did and it was secured as well unfortunately Hemaji, who managed Zen, had a friend set it up so he had no idea what the access key was or how to log into the router. What this meant is that the only way I could access the Internet was using his laptop during the morning in the dining room. I guess on the up side this meant I couldn't login into work, which in turn meant that I didn't think about which meant I actually had a holiday. 

One shouldn't really complain should one  

Wednesday, February 28, 2007

Where did the days go or the computer doth fail me

on the off chance anyone actually reads this blog I wish to apologise. I had planned, if only for my own amusement, to post a blog each day of my Japan trip. I thought this would be a good chance into blogging mindset but then technology got in the way.

On the flight over and on the trip to Kobe I had been running on the battery or to be more precise, batteries (I brought a extra battery which hooks on externally to HP laptops). By the time I reached Kobe I had maybe just over an hour left on the clock. Cool, get to the hotel and plug then thing into the mains.

You know that thing where something is so familiar you don't actually think about it. Like for instance power plugs. Will there I was with my Japanese power adaptor, wiring up the laptop, go to fit the adaptor and... well how about that the laptop power pack has a three pin plug and the adaptor only takes a two pin.

From there it all went downhill. Log in to work only to find that there were problems with my project which had to fixed. So I am madly coding coding away trying to get a solution in place before the computer dies. I must say there is nothing more relaxing then of an evening debugging code while watching your battery charge drop below 2%.  Believe it or not the problem was resolved but the laptop was dead in the water.  

For the next few days things weren't looking good. Couldn't find an adaptor anywhere. At one point I considered hacking off the power plug and hand wiring a Japanese plug to the cable. Thankfully sanity prevailed. 

On reaching Hiroshima the tech god smiled upon me. I hit the downtown shopping area and within half an hour I found CompMart, a full four/five floors of computer stuff. And there on the third floor I found it, the Road Warrior power cable which provided adaptors for both the power points and power packs. I was saved. 

Tuesday, February 20, 2007

Japan Day 2 on the Train to Kobe

I love train travel. It is something that we don't do well in Australia but here in Japan it is a hoot.

Currently I am sitting on the Shinkansen on my  way to Kobe. One could fly, which is in principle quicker,but then you have to get out to the airport, queue up to get on the plane, queue up to get off the plane and get to your final destination (since airports are rarely located in the center of town).  Catching the train on the other goes something like this:

  • rock up to Tokyo station and by ticket
  • Buy an eki bento(a lunch box) for the trip
  • Board train when it arrives (and they always leave on time)
  • Settle back and enjoy the view
  • 3 hours later your in Kobe. No muss no fuss.

May not be as quick as flying but sure is a hell of a lot less stressful. 

News Flash: Finally, having done this trip a number of times, I have seen Mt. Fuji in all it's snow capped glory.

My Japan trip Day 1 - on the plane

Well here I am on the plane flying to Japan. I've a scotch and dry, a bag of pre-shrunk bar snacks and a bloody battery for the laptop.

This is my third trip to Japan. The first two times I went was to primarily train at the Seidokaikan dojo in Osaka (though I did spend a week travelling with my wife doing the tourist thing). This time I more focused on just seeing a bit of  the country and to catch up with my friend Eigo.  To this end, after arriving in Tokyo, I am travelling down to Kobe for a day and then down to Matsuyama for a couple of days. From I'm going to Hiroshima for two days then travelling back to Tokyo to spend the rest of my time there (I didn't say it was long trip).

I love Japan. I can't say why. It is this that weird mix of chaos and order that they have achieved. Or maybe it all stems from watching the "Samuria" and "Phantom Agents" on TV as a kid. Who knows?

Sunday, February 04, 2007

HP TC4200 & Vista Drivers

For those of you who have a TC4200 tablet PC with the release version of Vista installed but are having issues with things like the wireless drivers and the like it appears that HP are not releasing drivers for the TC4200. This is a bugger there is a work-around go to the drivers page for the TC4400 and download the drivers form there. I installed the IntelPro/wireless driver and the QuikLaunch driver and it all worked fine.

Tuesday, January 02, 2007