When to use a Window Services

Posted by cgreeno | Filed under


Why does everything need to be a service? Almost every company I have worked for has requested some kind of automated process. Anything from a nightly ftp upload to cleaning up some DB records. Sure enough someone always suggests a windows service.

Windows says a service is:
A program, routine, or process that performs a specific system function to support other programs, particularly at a low (close to the hardware) level. When services are provided over a network, they can be published in Active Directory, facilitating service-centric administration and usage. Some examples of services are the Security Accounts Manager service, File Replication service, and Routing and Remote Access service.


Console App and the Windows Scheduler
Windows has a built in scheduler that is perfect for TIMED jobs, that in combination with the a simple console app is perfect for these types of requests. Simple to build, simple to debug, simple to deploy and simple to maintain. What asset does it bring to the business to create a Service? Most of the time the person doing the recommending either doesn't really know what a service is for or they are just attempting to challenge themselves? Console apps are so much better in most cases. You can kick off a console app whenever you want, you can change it and work with it on the fly, rerun it whenever you want, and generally speaking you are going have a harder time accidental bringing down a server with a console app

99% of the time a console app is going to be less intense on the server then a service especially if that service is poorly written, like the person that suggests we build a service with a timer to kick off processes, probably should not be your first choice of someone to take advice from.

Windows Services
Windows Services can be very useful and necessary but like everything it needs to be used when the business needs actually justify it. If you need to monitor a directory, use a service. If something on the server needs to up and running at all times, use a service. Services do have some built in advantages over a console app such as failure recovery. Such as "do nothing", "restart the service", "run a different app" or "restart the computer". I personally love the restart the computer option.

Both Windows Services and console/windows scheduler have their place just be sure you have a think about what you really need and how much business value the service your itching to build really brings.

BlogEngine Stackoverflow Flair Widget

Posted by cgreeno | Filed under

For anyone too lazy to create their own or does not wish to just use an Iframe, I have created a Stackoverflow flair widget for  blog engine.  This was quite an easy task due to the great job the blog engine guys have done with their plug in architecture for widgets.


It was quite straigtforward but I will explain a bit.  First you create a folder with your widget name(StackOverFlow - and yes I know it should be StackOverflow, I just go a bit overboard with my camel casing sometimes). Inside this folder you need to create 2 files edit.ascx and widget.ascx. And there you have it, the start of your very own widget! Told you it was easy. If you would like a turtorial on how to create a widget there is a great one here.


I chose to use jQuery with server tags for most of the work. I did this for two reasons one I wanted to learn jQuery and..... Ok I only had one reason. All the styles sheets are referenced from stackoverflow and the jQuery library from Google.

 

Jquery Code below.

<script type="text/javascript">
       $().ready(function() {
       $.getJSON("http://stackoverflow.com/users/flair/"+'<%=GetUserId() %>'+".json?callback=?", flairCallback);

       });

       function flairCallback(data) {
           $("#gravatarHtml").attr("href", data.profileUrl);
           $("#gravatarHtml").html(data.gravatarHtml);
           $("#reputation").append(data.reputation);
           $("#reputation").after("<br/ >" + data.badgeHtml);
           $("#UserNameLink").append(data.displayName).attr("href", data.profileUrl);
           $("#so-flair").height("55px");
           $("#so-flair").width('<%=GetControlWidth() %>');
           $("#imgFavIcon").css("display", "none");
           $("#so-flair").css("display", "inherit");
          
       }
   </script>

Edit Screen

 

 

You can download the files StackOverFlow.rar (2.41 kb).

Please note this was built on the 1.5 RC framework.

Fixing to be Unsafe

Posted by cgreeno | Filed under

The fixed statement is used in the context of the unsafe modifier. Unsafe declares that you are going use pointer arithmetic(eg: low level API call), which is outside normal C# operations. The fixed statement is used to lock the memory in place so the garbage collector will not reallocate it while it is still in use. You can’t use the fixed statement outside the context of unsafe.

Example

public static void PointyMethod(char[] array)
{
   
unsafe
   
{
       
fixed (char *p = array)
       
{
           
for (int i=0; i<array.Length; i++)
           
{
               
System.Console.Write(*(p+i));
           
}
       
}
   
}
}

Boo Who?

Posted by cgreeno | Filed under

Anybody that reads Ayede Rahien's blog will notice that he is writing a book called Building Domain Specific Languages with BOO. What? BOO? What the hell is BOO? I am a big fan of Anede and quickly wanted to get up to speed with BOO and what it is all about. Most of the my initial reading was taken right off the BOO website.

Boo is an object oriented statically typed programming language for the Common Language Infrastructure with a python inspired syntax and a special focus on language and compiler extensibility.


GREAT! I Love python's syntax! Who needs all those brackets anyway. But why do we need a whole new language to do Domain Specific Programming? And wait, what's up with Iron Python are these two not destine to clash? What am I not understanding here?

IronPython is not a take on Python it is just a reimplementation of python, where as BOO is completely different Language that is based off python syntax. Boo is statically typed, while IronPython is dynamical typed. OK, they are not even close to the same thing. Its another one of the those classic programming examples where upon first glance something that looks like a snake and moves like a snake but is actually some type of Duck.

OK! Then the difference has to be with the fact it is a Domain Specific Language. Hmmm "Domain Specific" I think I know what that means... but do I?? I mean I have heard the term thrown around and thought I had a grasp on it. I must still be missing something.....

Martin Fowler says:

Domain specific language (DSL) is a computer language that's targeted to a particular kind of problem, rather than a general purpose language that's aimed at any kind of software problem.


OK well that is kind of what I thought. SO if company XYZ has a corporate policy that all phone numbers have to be 3 digits, why wouldn't I just create a method or an even better an extension method. Seems pretty straight forward...

But Wait is that really a DSL? If you dig into it there are 4 types of DSL(From Chapter one of building DSL's) External, Graphical, Fluent and Internal/Embedded. OK now we are getting into it.

  • External is specifying everything from how an If statement works to operator semantics.
  • Graphical is a DSL that is not textual, but rather uses shapes and lines in order to express intent
  • Fluent - are interfaces are a way to structure your API in such a fashion that operations flow in a natural manner.
  • Embedded - Internal DSL are built on top of an existing language, but they don’t try to remain true to the original programming language syntax. They try to express things in a way that would make sense to both the author and the reader, not to the compiler.

Ayede Says:

Extension methods and lambda expression will certainly help, but they will not change the fundamental syntax too much. There are better alternatives for writing Domain Specific Languages than C#. A DSL should be readable for someone who is familiar with the domain, not the programming language. A DSL built on top of an existing language can also be problematic, since you want to limit the options of the language, in order to make it clearer in what is going on, rather than turn the DSL into a fully fledged programming language; we already have that in the base language, after all. The main purpose of an internal DSL is to reduce the amount of stuff that you need to make the compiler happy, and increase the clarity of the code in question.



So what does this all mean? Well it means he sold another book....





Using Using

Posted by cgreeno | Filed under

Using should be used with anything that implements IDisposable. The using syntax can be used as a way of defining a scope for anything that implements IDisposable. The using statement also ensures that Dispose is called if an exception occurs.

    //the compiler will create a local variable 
   
//which will go out of scope outside this context
   
using (FileStream fs = new FileStream(file, FileMode.Open))
   
{
         
//do stuff
   
}

Alternatively you could just use:

    FileStream fs;
   
try{
       fs
= new FileStream();
       
//do Stuff
   
}
   
finally{
       
if(fs!=null)
           fs
.Dispose();
   
}

Extra reading from MSDN

C#, through the .NET Framework common language runtime (CLR), automatically releases the memory used to store objects that are no longer required. The release of memory is non-deterministic; memory is released whenever the CLR decides to perform garbage collection. However, it is usually best to release limited resources such as file handles and network connections as quickly as possible.

The using statement allows the programmer to specify when objects that use resources should release them. The object provided to the using statement must implement the IDisposable interface. This interface provides the Dispose method, which should release the object's resources.

Inlining in C#

Posted by cgreeno | Filed under

What are They?

In the terms of C and C++ you use the inline keyword to tell the compiler to call a routine without the overhead of pushing parameters onto the stack. The Function instead has it's machine code inserted into the function where it was called. This can create a significant increase in performance in certain scenarios.


Dangers

The speed benefits in using "inlineing" decrease significantly as the size of the inline function increases. Overuse can actaully cause a program to run slower. Inlining a very small accessor function will usually decrease code size while inlining a very large function can dramatically increase code size.


Inlining in C#

In C# inlining happens at the JIT level in which the JIT compiler makes the decision. There is currently no mechanism in C# which you can explicitly do this. If you wish to know what the JIT compiler is doing then you can call System.Reflection.MethodBase.GetCurrentMethod().Name at runtime. If the Method is inlined it will return the name of the caller instead.

In C# you cannot force a method to inline but you can force a method not to. If you really need access to a specific callstack and you need to remove inlining you can use : MethodImplAttribute with MethodImplOptions.NoInlining. In addition if a method is declared as virtual then it will also not be inlined by the JIT. The reason behind this is that the final target of the call is unknown.

More on inline here

Open Source Is The Root Of All Evil

Posted by cgreeno | Filed under

We don't use open source.

I hear this all the time and is always accompanied with a look of pride and a slight gloat. I can understand I guess. I was young once but more and more of these people are in there 30's, 40's and 50's. So I turn to them and ask "So ahh Bill, how long you been with X?" 10 years + is always the answer. Ahhh.. the institutionalized then.

Enterprise Risk

Enterprise Risk is the number one reason given. Do the people that say this even know what that means? I would have thought it meant assessing risks in a pragmatic way to avoid any loss for your company. OK lets take this scenario. Say I'm doing some financial transactions using .Net on Server 2008 with MSSQL. It goes wrong. O CRAP! Who's fault is it? Who do we blame? Where do we point the finger? Microsoft? Even if there is a hidden bug that has been documented it is still the developers fault. No ifs, ands or buts. The first rule in programming is its always the developers fault.

Open Source is the root of all Evil

This is something that I have heard for years and I firmly blame the Microsoft "Sales Pitch" for this. Microsoft has been implying, if not outright saying, that if you use open source you are more likely to get attacked because it is less secure. It's how they sell their products and services and it filters from the boardrooms to the mangers and so on. The biggest and most common misconception is: "Anyone can change the Source".

No. Not just anyone can change the source on the base version that is released to the public. While it is true you can download and change whatever your like, that change will not be reflected in the version that is distributed to the public. It boggles my mind how people cannot see the beauty in that...

Lets roll back a moment to our 'O CRAP' financial transaction. Lets pretend the problem was in a MS library. Lets take a real leap and say that it's open source. So as a developer we need to fix the problem to make it go away. So we pull the source down to make a change and submit it to the project administrators. They say thank you. Check and scrutinize what you have done. Test our change and release it to the public. No more problem for me, or anyone else. It doesn't become a piece of obscure documentation that is in a knowledge base that has well over a million articles.


Open Source Tools Libraries

I really believe that a lot of the open source tools/technologies are much better then those that that you have to pay for, Sourcesafe being the most obvious example. Subversion and GIT(just to name two) are heads above Sourcesafe. Other tools such as TortoiseSVN intergate into windows to help users that perfer UI to commandline.

Nunit, NHibernate, Rhino Mocks and Structure map, to name a few, are awesome open source liabaries that can only aid a developer. There are hundreds of these tools out there that a lot of developers are not able to use due to out dated company policies.


No Open Source allowed here

 Most of the large companies stand by their "No Open Source Rule" usually  citing support and maintenance. But big companies like Microsoft end support for things all the time the only difference is with an open source product you can continue to upgrade and update you are not forced onto an entirely different stack/server/library ect. I have to admit though I used to buy into the whole "open source is insecure mumbo jumbo" and stuck strictly to MS libraries.   But hey I also used to only masturbate before I could find a woman that would sleep with me.... What's my point? If you're refuse to use open source then you're only screwing yourself.