Tuesday, 1 December 2009

WCF and serializing custom objects

I’ve been setting up an wcf service for a new n-tier system which I am currently architecting and developing at work. I’m trying to get to the point where all tiers store the data in the same objects (Entities) and they are worked on at different levels. These are simple POCO Entity objects which only store the data values and defined the data annotations to be used with validation. This will require the WCF service to be able to serialize the custom objects and transmit them between the tiers and in future, who knows maybe to a Silverlight client application as well?!

So, with the objects in place and with the correct DataContract and DataMember attributes in place I get the following error message when trying to pass them through the service between tiers:

“The underlying connection was closed: The connection was closed unexpectedly.”

After doing some Googling I came across the following blog post which had some handy pointers … thanks Bishoy Labib. But the biggest help was from a post by Damir Dobric. His post explaining how the "KnownTypeAttribute” is used when sending through data over a wcf service was very handy.

So after decorating the interface with each type of Entity I had which (3 so far) I built the project, got the service reference to update to get the latest definition and ran it with fingers crossed … it worked!

Damir had adding each of the types which might be used through the WCF service interface defined individually, then refactored it by adding them through the KnownTypeContainer (similar to below) and added them manually. This wouldn’t quite work for me as there are going to be, probably, lots of entities and I don’t want to have to add one individually each time. As all the entities are defined in a single project, I thought with a little reflection on the Assembly I could dynamically load them in so came up with this …

[ServiceKnownType("GetAllMyKnownTypes", typeof(KnownTypeContainer))] 
[ServiceContract(Namespace
= "http://mynamespace/2009/IApplication")]
public interface IApplication
{
[OperationContract]
string Echo(string value);

[OperationContract]
Entities.EntityBase Execute(
string action,
Dictionary
<string, object> parameters);
}

static class KnownTypeContainer
{
public static IEnumerable<Type>
GetAllMyKnownTypes(ICustomAttributeProvider p)
{
return new List<Type>(
Assembly.Load(
"Entities").GetTypes()
);
}
}




Nothing special, or clever, just need to make sure that all the Entities derive from the common EntityBase abstract class to work.



Thanks go to Damir for the original post and getting me through my issue I was having :-)

Thursday, 26 November 2009

Code formatting

Wrote a while back about not using Windows Live Writer as it was a pain and the code formatting was poor etc. well I’ve started using it again the past month due to being able to start multiple posts and edit them all at once etc etc. I’ve needed to post some source code recently (which I’ve not actually posted yet) and I went on the hunt for another plug in to make the code nicely formatted and came across Code Formatter for Windows Live Writer. Downloaded, extracted the files to the live writer plug in folder and fired up windows live writer and seems to work fine. :-)

Some demo code as an example:

    [HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
ViewData[
"Message"] = "Welcome to ASP.NET MVC!";

return View();
}

public ActionResult About()
{
return View();
}
}


Will let you know how I get on with it when I post a more in depth post :-)

Sunday, 22 November 2009

Naming convention rant (no. 2)

I know it comes down to personal preference when it comes down to naming and I know it has been ingrained into me due to the people I have worked with over the past few years, but why do some people continue to do such things in this day and age.

Enums

Everyone who codes should know what an enum is, if they don’t then they either don’t code much or never use much if any of any frameworks which are out there. Either way then they should be looked and used; they are good!

Anyway, when someone writes their own enum to use instead of special strings or numbers then why prefix the enum with the string ‘enum’.  It’s not a enumOrientation it’s Orientation! etc. etc.

Class Names

I know this comes back to my rant the other week about the use of namespaces, however I came across another example with class names in another project which all the names of the classes where prefixed with the name of the last part of the namespace they where in.

So for example it was for a twitter app, and the namespace was <blah>.Twitter, and one of the classes was called TwitterRequest. I know this doesn’t sound bad in itself, but the fully qualified name of it would be <blah>.Twitter.TwitterRequest. This shows the issue I’m trying to portray. If its a Twitter request, then it should be in the twitter namespace and called Request. If it’s not a request to do with Twitter then it shouldn’t be in that namespace. Hope I’m making sense.

I just don’t understand why someone would do this? I think I might just be turning into a naming obsessive :-)

If you can’t think of a good name for one of your new classes then why don’t you try the class namer :-)

Wednesday, 18 November 2009

Smtp4dev

Ever had to setup a local SMTP server when you’re developing some functionality but don’t want the emails being sent out by accident?

I’ve had a couple of systems which I’ve written over the years and been a little paranoid each time that some test emails will be sent out to a live email address. This stems from a project I worked on a while back and had to send the system to the company development team so they could do some in depth testing/debugging and they ended up sending over thousand emails out to the client ;-)

Any how …

I went to setup an SMTP server locally to do some testing today and found out that Windows 7 doesn’t have a built in SMTP server option anymore so went on a Google hunt. After doing some searching I came across a few free server options, but the one which caught my eye was on Codeplex. My initial thought was “Woo, can see how they wrote it if I want to” and the description was spot on …

Project Description
Dummy SMTP server that sits in the system tray and does not deliver the received messages. The received messages can be quickly viewed, saved and the source/structure inspected. Useful for testing/debugging software that generates email.

Anyway, I downloaded the latest build of smtp4dev as it seemed to fill all my requirements; a) does not deliver email and b) doesn’t interfere with anything else. 

The description was spot on, and so far so good it works as required. Will post an update once have used it a bit more.

Hope it helps others out in the future. Let me know if this helps you!

Asp.Net MVC 2 Beta released

After checking my morning blog feeds the first one in the list which was of interest was Phil Haack’s post about the release of Asp.Net MVC 2 Beta for VS 2008. I was going to put off downloading it and updating until later but after the teaser about nuclear facilities in the Eula I had to investigate …

This was the full Use rights which he was referring to:

a. Because the software is a pre-release version, and may not work correctly, you may not use it, alone and/or in conjunction with other programs in hazardous environments requiring fail-safe controls, including without limitation, the design, construction, maintenance or operation of nuclear facilities, aircraft navigation or communication systems, air traffic control, and life support or weapons systems.

I should read EULAs more often; Genius!