You’re a Polyglot

As a developer what language do you use? Java, Cobol, Ruby, PHP, C#, VB.Net etc, etc?

“It’s a trick question isn’t it?”  Yes it is.  Chances are that if you are a software developer you don’t use just one language.

Eight or nine years ago I was working for a training company that was created back in the 70’s by a couple of ex IBM people.  The MD stopped by my desk one day and asked me why a project I was working on at the time was taking so long.  As I explained, trying not to make myself look like too much of an idiot, he remarked that development seems to be more difficult than it used to be.

I wasn’t coding in the 70’s, I was too busy with the going to school thing, so I can’t say if it is easier or more difficult today than it used to be, although it does appear that we have far more choices to make and far more platforms to support.  So I guess in some ways it is more difficult today.

Anyway back to the story.

I then started listing out to the MD all the languages I was using on this project in an attempt to add some credence to the complexity and duration of the project.  I don’t remember today what the exact list was but something along the lines of:

  • T-SQL at the database level to deal with all the CRUD
  • VB for creating COM objects that held data access and business processing
  • PHP as the server scripting language that hooked into the COM objects
  • XML for delivering some of the screens and data transport
  • XSLT to transform the XML into HTML
  • XSD to validate the XML
  • JavaScript for all the client side stuff
  • HTML for all the user interface

That’s eight languages on a single project!  and at the time I knew all of them pretty well, enough to do all that I needed to do without reaching out for a book, search engine or forum for assistance.

The project I’m working on right now uses just four languages.  More than some developers would use and perhaps similar to most.

  • T-SQL (just can’t shake this one off)
  • C#
  • JavaScript
  • HTML

So what languages are you using for the project(s) you’re working on today?  I would guess that the number of languages depends very much on your company structure.  You may be (un)fortunate in that you have dedicated UI and DB people, you may be a in small team where everyone needs to know a bit of everything or you’re just a hoarder and like to keep everything to yourself.

Enjoy being a polyglot – it takes hard work and dedication; and we get there without even thinking or knowing about it.

That’s about it for now. Thanks for reading and see you next time.

Keyboard Short Cut of the Week – 1

There are lot’s of keyboard short cuts in Visual Studio that I use every day and it still surprises me how many of my peers don’t know they exist.  So I thought I would start providing a few for reference.

As this is my first post on the subject I’ll give 2 short cuts this time round.

Ctrl+k, c – Meaning: While holding down the Ctrl key press K followed by C.  This will add comment markers to the current line or currently selected block of text.  The cool thing about this is that it works in most code types, for example C#, VB.Net, JavaScript, Razor and HTML.

Ctrl+k, u – Meaning: While holding down the Ctrl key press K followed by U.  This will remove comment markers from the current line or currently selected block of text.

That’s about it for now. Thanks for reading and see you next time.

Self Host Duplex WCF Service with Discovery

To begin with my service testing I need to create a class library to hold the interfaces that will be implemented by the WCF service. The first is the interface that defines the functionality the service will implement, the second is the callback interface that is required for the duplex nature of the service being implemented.

using System.ServiceModel;

namespace SharedInterfaces
{
    [ServiceContract(CallbackContract = typeof(IWCFTrainingCallback))]
    public interface IWCFTraining
    {
        [OperationContract]
        string GetName();

        [OperationContract(IsOneWay = true)]
        void GetStatus();
    }

    public interface IWCFTrainingCallback
    {
        [OperationContract]
        void Status(string statusMessage);
    }
}

The service will be hosted in a console application. Once the console application has been created there are two things that need to be performed.
First creating a class to hold the service implementation.

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)]
class WCFTrainingService : IWCFTraining
{
    string IWCFTraining.GetName()
    {
        // For testing just return a string
        return "Jester98x";
    }

    void IWCFTraining.GetStatus()
    {
        // We're only testing so no need to do much
        // more than just loop round for a while and
        // return a value back to the callback
        for (var i = 0; i < 100; i++)
        {
            System.Threading.Thread.Sleep(50);
            OperationContext.Current
                .GetCallbackChannel<IWCFTrainingCallback>()
                .Status(i.ToString() + "% complete");
        }
    }
}

Then we get round to creating the self-hosted part with the following:

class Program
{
    static void Main(string[] args)
    {
        Console.Title = "WCF Testing Host";

        Uri baseAddress = new Uri("http://localhost:9080");
        Uri tcpBase = new Uri("net.tcp://localhost:9081");
        using (var selfHost = new ServiceHost(
            typeof(WCFTrainingService), baseAddress, tcpBase))
        {
            selfHost.AddServiceEndpoint(new UdpDiscoveryEndpoint());
            var sdb = new ServiceDiscoveryBehavior();
            selfHost.Description.Behaviors.Add(sdb);

            selfHost.AddServiceEndpoint(
                typeof(IWCFTraining), 
                new NetHttpBinding(), 
                "HttpTrainingService");

            var net = new NetTcpBinding();

            selfHost.AddServiceEndpoint(
                typeof(IWCFTraining), 
                new NetTcpBinding(), 
                "TcpTrainingService");

            var smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            selfHost.Description.Behaviors.Add(smb);

            selfHost.Open();
            Console.WriteLine("Service has started");
            Console.ReadLine();
            selfHost.Close();
        }
    }
}

At this point we have a service hosted within a console application. Doesn’t do a great deal, that’s fine as we’re just showing that discovery is working and that we can communicate in a full duplex fashion.
So the next thing that is required is a client to assist with the testing of the service. One thing that I want to ensure is that it is proxy free, this is why the interfaces are in a shared project, i.e. not confined to the service.
The client is another console application.

class Program
{
    // Simple list to store the discovered endpoints
    static List<EndpointAddress> found = new List<EndpointAddress>();

    static void Main(string[] args)
    {
        Console.Write("Wait for proxy and press enter:");
        Console.ReadLine();
        FindWCFService();
        Console.ReadLine();
    }

    private static void FindWCFService()
    {
        Console.WriteLine("Starting Discovery Process");
        var dc = new DiscoveryClient(new UdpDiscoveryEndpoint());
        var fc = new FindCriteria(typeof(IWCFTraining));

        // Only search for 5 seconds
        fc.Duration = TimeSpan.FromSeconds(5);
        
        // Set up some event handlers
        dc.FindProgressChanged += Dc_FindProgressChanged;
        dc.FindCompleted += Dc_FindCompleted;
    
        // Start the discovery process
        dc.FindAsync(fc);
        dc.Close();
    }

    private static void Dc_FindCompleted(object sender, FindCompletedEventArgs e)
    {
        if (found != null && found.Count > 0)
        {
            // Define the object that will handle the callback
            // from the service
            var ptest = new ProcessTest();

            // Get an instance context from the object
            var ic = new InstanceContext(ptest);
            var net = new NetTcpBinding();

            // Create a channel to call the service methods
            var duplexProxy = new DuplexChannelFactory<IWCFTraining>(ic, net, found[1]);
            var dProxy = duplexProxy.CreateChannel();
            Console.WriteLine("Calling status on proxy");

            // Call the get status method, which will act as our
            // long running process and call back test bed
            dProxy.GetStatus();

            // Call a method that returns immediately
            Console.WriteLine(dProxy.GetName());
        }
    }

    private static void Dc_FindProgressChanged(object sender, FindProgressChangedEventArgs e)
    {
        // When an endpoint is found add it to our found list
        found.Add(e.EndpointDiscoveryMetadata.Address);
    }
}

[CallbackBehavior(UseSynchronizationContext = false, IncludeExceptionDetailInFaults = true)]
public class ProcessTest : IWCFTrainingCallback
{
    public void Status(string statusMessage)
    {
        // Do something with the callback
        Console.WriteLine(statusMessage);
    }
}

That’s about it for now. Thanks for reading and see you next time.

Link

Microsoft Virtual Academy wants to help you to become a hero:

Go over to the MVA Hero page for details and sign up.  Lots of free training from Microsoft and over 2,000,000 members.

C#, TCP and UDP–Part 1 Update 1

After further work on my distributed processing system [some details found here] I have concluded that Windows Communications Foundation (WCF) is going to be where I go with this.

I’ve used WCF for various things in the past.  It’s usually been forced on me, i.e. it hasn’t been something I have chosen to do.  This time, after some research and testing, it looks like I’ve found a reason for me to use it.

  • WS-Discovery allows me to get rid of my own UDP discovery process.
  • Duplex bindings allow me to have the systems conversing the way I want them to
  • Self hosting in a service or a console app is very simple

Importantly, perhaps more than anything else, the other devs in my team are familiar with WCF so nothing, or very little, for them to learn.

I’m going to write some posts later to take me (you (us)) through the process of creating a simple app to do the WCF basics that I need for my app.  This allows me to dump information for my reference, if it helps anyone else – great!

That’s all for now.   Thanks for reading and see you next time.

Computer Dies a Horrid Death

Status

Ah the family computer died.

Last week the family computer got rebuilt.  Didn’t loose much thanks to everything being backed up.  One thing I did learn, don’t install the the software I need for work on the family PC.

As of now the family PC is for the family and my upgraded laptop is for me.

Lesson learnt – move along.

C#, TCP and UDP–Part 1

I’ve started playing around with distributed processing, i.e. having multiple servers set up to manage the execution of tasks, for example to assist with some over-night batch processing.

I’ve been hunting around for a light weight messaging system to allow all of the executioners and marshalling systems to communicate amongst themselves.  Initially I thought about each system self hosting with something like Owin or Nancy and then have them talk together using something like SignalR.  Interestingly the proof of concept worked out fine, i.e. I could get a number of systems to talk.

I finished the proof of concept work feeling good, but there was something in the back of my mind that was telling me this wasn’t the right thing to do.  So I thought about SignalR and how it worked.  At the top level it will attempt to establish a communications channel between the client and the server with Web Sockets.  Huh?  Light-bulb moment.  Sockets!!  Why not have the various parts of the distributed system talk with sockets?  I feel like I’m on to something with this.  I should be able to define a set of ports that my system is gong to use.  I can the call out from a central controlling system with UDP to the network asking for the distributed components to identify themselves.  Once identified TCP can step in and provide the actually communications channel.

A couple of prototypes later and I’m back to being not so sure.  It should be fine, but getting everything co-ordinated isn’t working quite as expected.

In my next post I’ll post some code, issues and possible solutions.

That’s all for now.   Thanks for reading and see you next time.

Code refactoring–a brief introduction

As the title suggests this is just a very brief introduction for now, later posts will go into more detail.

The starting point is understanding what is meant by re-factoring, or more specifically what I mean by re-factoring.

Re-factoring is not about bug squashing, it’s not about you hunting down bugs and fixing them, that’s what testing and bug squash days are for.  If you find bugs while re-factoring you have to make the call as to do you stop your re-factoring and fix the bug or just log it and come back to it later.  Personally I’d log it and come back later as I’m on a re-factor session not a bug fix session.  You choose what is most appropriate for your situation.

Re-factoring is about making code easier to understand by humans, easier to read by humans and easier to support by humans.  For example a variable named lbBlue, using an older coding style that means local variable (that’s the l) of type Boolean (that’s the b) to indicate if something is Blue or not (that the Blue).  Of course it can be argued that if this is a known convention then that’s fine.  The only issue is what happens then you hire new developers?  It’s not obvious without being told.  If this was re-factored to isBlue then most developers will get it’s meaning instantly.  BTW, has the added advantage that you’ve just taken a step towards self documenting code.

That’s all for now.  Thanks for reading and see you next time.