| Steven's profileDevelopmentalBlogLists | Help |
|
01 June Advanced GenericsI've recently been reading this book by Jon Skeet about C# 2 and 3, and it got me all inspired to write about generics. Jon's great; he's extremely active in the C# and Java community, and recently responded to an email question I had in less than 10 minutes! I'm finding the book insightful and useful and greatly enjoying my bus rides to work when I get to read it. This post is going to assume you are familiar with using generic classes like List<T> and Nullable<T>. So lets start with the syntax for declaring our own generic class:
This is pretty simple: the Animator class accepts a generic type T and requires you pass a T in its constructor. The MoveOnce() method is empty at the moment, we'll get to that later.
Great! We've decided that the fact a Monkey can move and stop is pretty general, and is probably something other animals can do, so we've declared an interface to indicate our monkey has those capabilities. We might also define IEater, or IDungFlinger. Now we can revisit our generic type in our Animator class definition as follows:
This is where the magic begins. We've placed a constraint on our generic type, stating that it will only ever accept a type that implements our IMoveable interface. If we try to use the Animator class with something else, we will get a compile-time exception. Even better yet is that our development environment knows that since our instance will always implement IMoveable, it therefore must have a Move() and Stop() method, therefore we can call it anywhere in our class; in this case the MoveOnce() method. You can declare more than one generic type in your class definitions. Imaging a class that facilitates a race for monkeys, whether they be chimps, gorillas, or marmosets. Consider this generic class declaration:
Here we have defined that our class accepts 2 generic types: T and U. We have further restricted them that they both have to inherit from the Monkey class. Here's some example calls using this new class:
As you can see that anything that inherits Monkey is valid. Of interest, the second example shows that even an instance of Monkey is valid. This shows that the where clause in our type restriction means anything that inherits from X or is X. Finally here are a couple of extra cool things you can do with generic type restrictions as general non-monkey related examples:
TestCase1 demonstrates that we can force the type to be a reference type. TestCase3 shows that we can ensure T implements multiple interfaces.
This is valid because Chimp inherits from Monkey. The last TestCase7 is somewhat interesting. Its declaration indicates that T is a reference type, and U is a value type that must inherit from T. How can this be? It essentially states that you must pass a struct that inherits from a class. But C# won't let you declare this. To be honest at first I didn't know the answer to this. Thus the email to Jon, and his explanation was simple and it made sense: interfaces are reference types. Hopefully you are aware that structs can implement interfaces also (but can't do inheritance), so we might define a new animal as follows:
Then we can legitimately use the TestCase7 syntax:
Then of course, valid values for an IMoveable are a Chimp, Marmoset, or even another Mouse, while of course you can only use a Mouse for the second type. Hopefully this has given you a better idea of what you can do with generics. 29 May Contravariance, Covariance, and GenericsThe common question is: what the hell is contravariance and covariance? Ok so you are familiar with casting right? In C# we can do this:
We can also do this:
Because the 'MoveWest' method accepts the narrowing type 'Animal' it is said to support 'Contravariance'. If the C# compiler forced us to only pass exact types, then it would be considered 'invariant'. Likewise C# methods support covariance, in that we can return a narrower type and cast the resulting instance to its correct form. There are parts of the .Net framework however that aren't so flexible. In this case, I'm talking about generics.
Unfortunately this does not compile. We get the following warning:
Why is this? Monkey inherits Animal, so the list conversion is perfectly safe. Well this is one case of invariance. But here's the kicker: word on the street is that the CLR actually supports contravariance of generic lists; its just the C# compiler that doesn't!
Circle Of InterestMy mate and fellow Readifarian Paul Stovell recently posted his 'Circle Of Interest' and started a meme by nominating 5 others to do the same. I was one of the lucky ones, so here's my circle. I don't doubt this circle will change completely in 6 months. I expect the TFS parts to fade into the middle circle, with more focus on WPF and SilverLight perhaps. Who can tell? The big red circle indicates things I don't care about, the baby-vomit yellow indicates stuff I am interest in but have little time or dedication to fully pursue. The dog-vomit green is the stuff that I am loving working or am most eager to develop as soon as possible. Oh, WCF shouldn't be in the middle circle, but I couldn't be bothered changing it. Now, to continue the meme, I nominate these people: Matthew Rowan , John McFadyen , Joseph Cooney , Leon Bambrick Get to it chumps! 05 May Personal Development PlanAt Readify we are encouraged to do a plan that we can use for guidance in our personal development. I was pretty generic with mine and have decided to give it more focus today (given that its labour day and all). I figure I'd post it here so that I can hold myself accountable to it next year. Lets say, end of financial year 09? Gives me 14 months to see what I can achieve off of this list. Naturally having a baby will affect this in yet to be discovered ways. The list is priorities by importance for high level headings only. Its mostly just a bullet point list, which is interesting because I am unsure how to measure success for some of these things (for example, when will I 'know' WCF? I've done a bit already but still am not across all the security aspects). Anyway, here's the list. Feel free to critique or post your own list in response. Certifications (importance: HIGH) Improve blog quality (importance: HIGH) Attend Special Interest Conferences (importance: HIGH) Presenter (importance: MEDIUM) Author (importance: LOW) Wow, looking at this list, I feel like I know absolutely nothing! But to be honest, I have groundings in a lot of the technical stuff (like SilverLight, WCF, Reporting Services, BI, LINQ, etc), but I really want to gain expertise in these areas. Why do I rank certifications so highly? Well I'm keen on the MCT, which requires an MCTS or two. Also they are useful to an employer for points towards partner status. Finally, its a personal milestone, ie the certification is for me, not you. I want to pass all those tests based on my knowledge (not cheating, examining practice tests, etc). I don't expect I'll get to Tech Ed Europe or Mix US anytime soon, but ReMix is a possibility for me this year (I've never been) and Tech Ed is a definite (see you there!).
Technorati Tags: PD Plan,Personal Development Plan,certifications,blog,technology,conferences,author 02 May So You've Ruined Your Life...You might recognise this quote from the Simpsons as the title of the book that doctor Hibbard gives Homer and Marge after they realise they are 'up the duff'.
Since I just got the news that my wife Sonia and I are now also 'up the duff' I figured it would make for a good article title. I don't actually believe it of course... I'm as excited as a little kid with a lollipop! It will however mean that my lifestyle will change somewhat, since I now have new priorities.
So that's all this article is about. Its been just the two of us (married for over 6 years) and a sausage dog up until now. I'm excited, curious, and nervous, all rolled into one. I have no idea how it is affecting my work, but no doubt this will change me in ways I have yet to understand.
Anyway, I just want to apologise for the lack of blogging in the last few weeks, and hopefully I'll be back at it again soon. 06 April TeamBuild, PowerShell, and Exit CodesAt my work I am starting to integrate PowerShell scripts into the TeamBuild projects for our nightly build. Including PowerShell is very easy: you just need to ensure that PowerShell is installed on the build server. Installing PowerShell will adjust the PATH environment variable, but you may need to restart the build service in order for the build server to pick up this new path. Including a PowerShell script in your TeamBuild (or MSBuild) targets is very easy. You might have a target that looks something like this:
As with most environments, we need to change the execution policy so that it will allow scripts to be run. We only need to do this once for our whole build, so best to create it in its own target and place a dependency to it from other targets that will be calling PS scripts. In the above example, we are calling a script in the Scripts folder called testscript.ps1. Pretty simple really. If an error occurs in that script, then the exit code will indicate an error, and the build will fail. However, we may not want the build to fail. Perhaps only a warning is required. Or perhaps we want the script to do things that might break the build AND raise warnings. How can we handle this? An easy way is to use exit codes. Most EXEC calls will return an exit code. 0 usually means success, and 1 or higher means failure. In MSBuild (ie. TeamBuild) that means 1 or higher will break the build. So what if we want our PowerShell script to return all sorts of exit codes, and only have certain exit codes break the build, and others raise warnings? This is easy too, but requires some jigging. First, we call the EXEC task with an attribute that tells it to ignore the exit code that results from the call. This means that even failed calls will not break the build. Second, we capture the exit code of the EXEC task with the Output attribute and assign it to a Property. The target might then look like this:
Cool... we now have the exit code in the property 'ScriptExitCode' and can manually do what we like with it. Using the Warning and Error tasks that already exist, we can react in a more custom way:
In this example, if the script returns exit code 1, the build still fails. However if it returns 2, then only a warning is raised, and the build continues on. So only one thing remains: how do we tell PowerShell scripts to return a specific exit code? Well that's just as easy. In our PS script, we can use this line:
$host is a reserved variable in PowerShell. In the above code, we are telling the script to return exit code 2 when it exits. Note that this line does not tell the script to exit straight away. To do that, call the command 'exit'. So using this knowledge, we can sprinkle the relative exit codes throughout our scripts and let MSBuild handle it on the outside. 03 April How to lose an entire week in conferences and presentationsBrisbane: A rich ecosystem of technology, enthusiasm, and deliverance. I often wonder how I manage to stay married when there is so much going on in this town that keeps me distracted. I'm sure its just as rich in other cities, but hell, I don't care about those infestations. Brisbane is great for the developer/enthusiast and here's why: my week since last Wednesday. Weds 26th March - Heroes Happen Thurs 27th March - Sql Server User Group Mon 1st April - QUT Alumi Presentation Tues 2nd April - Security Interest Group Weds 3rd April - Briztalk User Group So as you can see, Brisbane is a rich fertile ground of development. I've learnt so much over the last week, its ridiculous. Luckily the VSTS user group (was suppose to be Friday morning) was postponed, help give meaning to the weeks ahead! Next week looks rather quiet, which should be good because I have some interesting ideas around F# that I would like to explore. See you at the next Brisbane community event. 30 March Introduction to PowerShell XMLHopefully by now you have heard of PowerShell (PS) and know that it is a scripting shell much like BASH or any other Unix shell. However unlike those other languages, PS uses an object pipeline, rather than strings, and those objects work directly with the .Net Framework. What this gives us is a powerful, flexible, scripting platform where developers don't have to learn a whole lot before they can start leveraging some real scripting power. There's lots of examples around how to get started with PowerShell so I'd like to skip that part but first I just want to cover off a few key commands. First, everything in PS is an object of some sort. Unless otherwise specified, most commands will deal with objects or arrays of objects. Since all .Net types inherit from System.Object, this is how PS assumes type safety for all command calls. Creating an instance of a class is relatively easy. This is how you would create an XmlDocument:
The dollar $ sign indicates a variable in PS. The New-Object is a command that accepts a string input for the type. We could then continue to load some XML as we would in .Net:
However, PS has a nice XML shortcut that you should know about, and that's the [xml] cast:
Now lets use something more interesting for our XML:
Another cool operation with XML is referring to Xml Nodes specifically rather than searching. The .Net way would be this:
This would return an array of Xml Nodes that represent all continents. In PS we can shortcut to:
You can look at the type of any variable with the standard GetType method:
Interestingly you will see that rather than an XmlNodeList or array of XmlElements, this in fact is an array of Objects! However if we look at the contents of that array we will see each item is something more reasonable:
This command tells us that we have 2 objects of type System.Xml.XmlLinkedNode in our array. The syntax is specific to PS. The | symbol is a pipe, and means to take everything from the left, and shove it into the right. The % symbol is a shortcut for "foreach", which also works just as well. Because we have piped in some content, we don't need to specify any condition logic for our loop, and can go straight to the inner contents. When you don't specify what the name of the variable in the range will be, you can use the $_ variable instead. So overall, the above statement is the same as:
Let's do something a little different. We will attempt to now take our array of Xml nodes representing continents, and put just the continent name into a generic collection of strings. The syntax around creating an instance of a generic class is as follows:
The type shows as Collection''1 without the generic part (string) but it is still there, under the covers. Now lets put the name of all continents into that collection with out % syntax:
The second command shows the type of each item in the collection, and we can see they are all strings. So that wraps up my first PowerShell post. I've been doing a lot of it this past week, and was very green to begin with. All my work has been around Xml, and I'll be doing some more this week. My specific usage is relating to build and release management in TFS, and am enjoying it greatly. When it comes to using PowerShell you can create your scripts in any text editor. Currently I am using Notepad++ with PowerShell language definitions, and also PowerGui. PowerGui in particular is an excellent free tool. 24 March My Introduction To F#During the week the QLD MSDN User Group was treated to a presentation by Leon Bambrick who demonstrated F# in all its glory. The very next day I had the opportunity to meet with Don Syme, inventor of F# (you never know who you meet sitting at the hot desks in Microsoft SDC!). To me, this was a sign from the powers that be: play with F# or else! First step to get started, is download the latest version MSI from the Microsoft Research site. After you install it, you can open Visual Studio and you will find that you have a new project template: Not a lot of options for project types I'm afraid. But its a start at least, and Leon indicated there will be full commitment from Microsoft in the future (don't make me a liar Leon). When you create a new project, its very empty. So we add a new item, and notice a few options: To be honest, I have no idea what they all do. I'm as new to this as you! The script file has a FSX extension and this makes me wonder what kind of cool script stuff you can do with it. Undecided, Leon's voice came to me as if in a dream... "Use the Source, Luke, use the source". Sure enough, the F# Source File option has a FS extension which is what I was looking for. It comes with a heap of template code for you see how the language syntax works. I was pretty comfortable with the magic keyword 'let' already from Leon's presentation. I set off instead to get the 'interactive' mode to work, since I found this quite a neat feature. So it seems that there is a 'FSI.exe' which stands for F# Interactive. It is a command line tool that allows you to pass snippets of F# code followed by two semi-colons ;; to terminate the parameter. It seems to remember your 'state' such that functions are remembered in between calls. This is further implemented as a command window in Visual Studio, which is a nice feature. You have to activate it after you have installed F# MSI, by turning it on in the 'Add In' section: Tick all the boxes, and the F# Interactive window opens. Now you can start typing inline F# code to your heart's content! You can even execute sections of existing code in your .FS files by highlighting a section of code, and pressing ALT-ENTER. This is very similar behaviour to SQL Management Studio - Query window, where you can highlight some T-SQL and press F5 to run just that code. The F# Interactive window will show any errors inline. For example, when you add a new source file, it comes with a bunch of syntactical examples, and one of them is the "highest common factor" code, as follows:
So 'let' is kind of like 'dim' in VB, or 'var' in C# / Javascript, but, er, not really. 'rec' means that the call is recursive. You can't have a function that calls itself without the 'rec' identifier. Can't say that I am a fan of 'elif' instead of 'elseif'. I wanted to test to see if this function really works as indicated. Some tests I thought up in my head:
I could have deleted all the other redundant code, and entered my 3 tests above, then compiled and run it, but I suppose I would also have to print out the results or some such... too much hassle! So I highlighted the function definition and pressed ALT-RETURN to run it in interactive mode. Then in the interactive window, I typed each example, placing ;; at the end. Each time it returned the correct result:
Finally I wanted to see the DirectX demo again that Leon showed in his presentation. The MSI also installs these examples, and I ran the DirectX demo in script mode, just to prove to myself Leon isn't a trickster or a witch (burn him!). Very impressive stuff. Great! I now feel that I can put F# on my resume. I am interested in exploring it some more. I have some thoughts around graphing and other mathematical applications. I am particularly interested in finding examples of complex algorythms made extremely simple through the F# syntax. Finally, since FSI can effectively be called script-like (and there are those mysterious FSX files), what advantages might we be able to see in using it with other scripting tools/languages like PowerShell? Oh, see you all at the launch on Wednesday.
20 March Recent AnnouncementsSome interesting things announced today. First, the official dates for ReMIX Australia:
To be honest I've never been, but I think I'll see if it's doable this year. Secondly, the speakers for CodeCampOz are announced! Check out the sessions here. Of particular interest to me are the following sessions:
Oh and don't forget the Readify presenters! Finally, my new Microsoft SDC friend: Joseph Cooney on Silverlight 2.0 and WPF (even though it doesn't say WPF on the CodeCampOz site, trust me, he's doing a WPF / SL comparison). All in all its going to be a great weekend! Look forward to seeing you all there. 16 March Visual Studio Built-in Web ServerNo doubt you've hit F5 to debug your web application from time to time, and you noticed the little web server that sits in your system tray.
By default, it assigns a random port on the local machine. If you already have IIS then no doubt it is configured to use port 80, which is why the VS web server uses another random one. When your application loads up in browser, it will usually have a URL similar to this:
Even when you stop debugging, the web server continues to run! Which means you can manually open a browser and still hit that same location. This is perfect if you don't have a version of XP or Vista that comes with IIS. However, by default the port number is random. This means tomorrow when you go to open and debug your application, it will run up with a different port number. Sometimes this is no big deal, but with web services and (in my case) WCF services, this hurts. Your WCF endpoints will be configured for debugging and the endpoints will specify the port number, so that you can debug them in VS. Every time the VS web server starts up, it gets a new random port number, which you then have to update in your WCF client endpoint. Luckily the internal web server is configurable. With your web projects there is a property under the project properties - Web section: By default, the "Use Visual Studio Development Server" option is checked, and "Auto-assign Port" is selected. However you can change this to "Specific Port" as I have in the above example. You can also configure the web server further to pretend to use a virtual directory. This is great because it allows you to simulate production environments by specifying the same combination of ports and virtual directories as would exist on the production server. Of course, you can save yourself the hassle of having multiple web servers running at once by just selecting "Use IIS Web Server" instead, however this isn't ideal in environments where multiple people work on a project and could potentially have different workstation setups. Further Resources 07 March Notification Services : Debug Tips and TricksSql Server Notification Services can be tricky to debug. You have no code that you can put a breakpoint in (except for your managed api calls of course) and there's a lot of working parts inside. Here's a couple of tips that have helped me today dealing with a local and alpha problem. Rule Testing Even one step lower, you can just view the contents of your events schema (view) to make sure the event was posted there correctly, and view your subscriptions schema (view) to ensure that the correct subscription information is in there. Enable Distributor Logging
Obviously this will add some overhead to your processing, but if you don't have high through-put then you could leave this on all the time. But for initial setups (such as on your local machine, or on production databases) it is invaluable. As the name suggests, the logging occurs around distributions of notifications. Your event should get stored in the relevant table after it is submitted, and your rule should process the events and post them to the notifications table. At this point, the distributor logging kicks in. When the service 'ticks' and processes your notifications for delivery, information is logged in a view matching your notifications view. For example, if your notifications class name is "BlogUpdates" then there will be a view automatically created called 'NSBlogUpdatesNotificationDistribution'. You can monitor this view for changes while you post an event that should trigger a distribution. Keep refreshing the view and you will see a new entry appear for a desired notification delivery. It details what delivery channel to use, the user, the content being delivered, and a status of the delivery. Eventually this will change to "Delivery Succeeded". If not, you can find information inline to assist in debugging. Windows Event Log Email Notifications More Information On Notification Services Notification Services tutorial: http://msdn2.microsoft.com/en-au/library/ms170337.aspx
06 March ParticlsPresumably you subscribe to blogs. You might even subscribe to this blog. How many do you subscribe to? What about other feeds, like Engadget? Podcasts? What about sites you read manually, like your local newspaper site? Let's be honest, there's a lot of information on the web that we would like to be able to digest. 3 years ago, I would simply load up my browser, go to a url of a site, and read the articles. This is how I kept up with the technology industry and the goings-on. Unfortunately it was hard to sort through and get to the quality pieces of information that I really wanted to know about. Then I started subscribing to blogs. A few at first. People who were at the top of their game and were driving the technologies I was interested in. I tried some other tools (including Outlook) for periods at a time, and eventually came across Google Reader. By now my list of feeds had grown. Content Is King I was following the big corporate bloggers as well as the little guy down the street. I was more interested in the content of the post than the poster themselves. In a move that would shock the world, some of the big guys on campus were scratched from my feed list. I spent a fair amount of time customising my Reader with the information I really wanted to receive. I still had a dilemna though: what about those posters who provide quality sometimes, boring crap other times? Well I guess I just had to skip those entries, 'mark as read', although sometimes I didn't know the post was crap and not related until I had spent time reading the first few lines or summary. Enter Particls I can't remember how I first came across Particls. But for me it was a smash hit instantly. The essence of Particls is about attention management. From their About page:
You see, every person, site, feed has something interesting to say at some point. Why not have a system that can rate and prioritise the information for you so that you only get presented with information you are specifically interested in?
Furthermore, you can tell Particls to search for information. This means even if you don't have any blogs you subscribe to, you can still specify keywords of interest, and let Particls do the rest. The information can be delivered to you in a number of ways. If you must, you can get the annoying popups around the system tray area. You can get a news ticker across the top, or a nice sidebar (my personal choice). You can use multiple options if you like: tell your news ticker to display only the most relevant and important information, tell your sidebar to disappear when not in use and show less relevant information. Particls can even provide an RSS feed for you to consume in another reader if you so desire. On my wide screen it works great with Vista sidebar. If you get a notification that is inaccurate, you can simply right click and tell it to give less information about this topic. In this way, Particls quickly learns what your priorities are in order to deliver you more relevant information. Oh, did I mention Particls is free? Enough reason to go and give it a shot. Currently the download link on the site is disabled. I believe this is because they might be moving from beta to production (can anyone shed light on this?). I'll let you know as soon as I hear something. Update: I've tried to contact Chris Saad from Faraday Media about this a few times this week but didn't want to delay the article any longer waiting for a reply. 24 February Notification Services - Part 4 - Managed APIIn the first 3 parts of this series we discovered how to define a Notification Services application via the instance and application XML. In part 2 we defined an example scenario where users are automatically subscribed to receive notifications in their email whenever someone posts a comment against their blog post. In this part we will discover how to write code to access the event schemas we defined in part 3, and how to insert subscribers. For the sake of brevity, this post will define sections of code, not entire classes. Assume a class library project in C# that will be called by our main blog ASP.NET application. The methods defined in this post would be found in such a library. This post assumes that the Notification Services instance is installed on the same machine that will be running the code. This is important because to access a NS instance, you don't use connection strings as is typical in ADO.NET. Instead, it refers to registry entries which are automatically placed by NS when the instances is 'Registered' and 'Started'. For more information, see the MSDN link. Preperation At some stage we will also need to define what our Instance name and Application name is. We could put this in a constant but ideally it would be in web.config and accessible through a property somewhere. This code will assume a class called 'Settings' which has properties called 'InstanceName' and 'ApplicationName'. Eg. string application = Settings.ApplicationName; A lot of the time we will need access to an instance of a class called NSInstance. Through this class we can get access to our NS application, by getting an instance of NSApplication. The rest of this post will refer to instances of both of these classes, so we will define some code for them now:
In Notification Services an instance can have multiple applications, even though previously we have defined only 1. Subscribers
You may remember from our application schemas that we needed to store a UserId. This was a simple string that represents the user a subscription belongs to. Before we can add subscriptions, we need to first check that the instance has the user registered as a subscriber. We can do this as follows:
As you can see we use our BlogInstance to access an enumeration of all users, which can be indexed by the user's name. We could easily modify this into a Delete method for a subscriber as follows:
Now we know how to check for a subscriber, we can write code to add one:
Simple! If we don't check for a subscriber first, an exception will occur if a name is added twice. Devices SubscriberDevice device = new SubscriberDevice(BlogInstance); DeviceName is often referenced in your application XML rule for subscriptions so it is usually important to have a naming standard for your devices. However we have not been using devices up to this point so the values you put here aren't important for now. DeliveryChannelName however is extremely important. In part 2 we defined the instance XML and part of that was the <DeliveryChannels>. In that XML we defined a protocol of type SMTPDelivery with a name of SMTP. When defining the device delivery channel name, it must match one of the channels specified in your instance XML otherwise no deliveries will be sent (no exception will occur, however when the service runs your notifications, it is at this point that it will fail, and details will be inserted into the event log). As with subscribers you can check if the device has already been added as follows:
As with subscribers, we should check the device has not already been added before commencing the insert process. And as with subscribers, deleting a device is as easy as calling the Delete method of the SubscriberDevice instance.
Because the .Net type subscription has no way of knowing at design-time what fields your subscription schema might have, it uses a common method for setting your subscription fields. This means you won't get compile errors if you don't really have a "PostId" in your subscription schema in Notification Services, but you will get a runtime exception on the subscription.Add(); method call. Important to note is that in the constructor for a Subscription takes an NSApplication instance now. This is because even though subscribers and devices are specific to an instance, an actual subscription is specific to an application only. Also note the second parameter of the constructor. This is the name of the subscription class we are subscribing to. Note that the name we supplied 'CommentSubscription' is the name we specified for our subscription class in the application XML. Once again, this will cause a runtime exception at the subscription.Add(); method call if the subscription class name is invalid. Well done! The user is now subscribed to receive notifications when comments are posted. However we still have to define how comments are posted to Notification Services so that those subscriptions can be acted upon. Events
In this code, we assume that we already have the values of the comment text, the user who posted the comment, and the original blog postId that the comment was posted against. The Event class lets us define the details to post to Notification Services through an indexer, and the EventCollector class will write the events to Notification Services for us. As usual incorrect fields specified will result in a runtime exception. Some items to note here. First, the constructor for an event needs the name of the event class we are posting to. We only defined 1 in our events schema; this was defined in the last post in our application XML. Secondly, in our last post we also defined the <Providers> node and the <NonHostedProvider> sub node (right at the end). When defining an EventCollector we must pass our provider name to the contstructor. You can not post an event to Notification Services without using one of the providers specified in your application XML. Because we are specifying events in code outside of Notification Services, our provider is a 'NonHostedProvider': not hosted by Notification Services. Summary This brings us to the end of my series on Notification Services. If there is anything else you would like to know about, please don't hesitate to contact me and I'll do my best to provide useful information. Notification Services Series Further Resources Technorati Tags: Notification Services,Sql Server,Sql Server 2005,XML,Events,Notifications,Subscriptions,C#,API 17 February Notification Services - Part 3 - Application XMLThe application XML defines the key information about your Notification Services (NS) application. This includes the schemas for event data being received, schemas for what users can subscribe to, and schemas for the information that will be sent in a notification. At the bare minimum you need 1 schema for each section. The application XML is defined with the following elements:
Here you can see the sections where we define our 3 different schemas: EventClasses, SubscriptionClasses, and NotificationClasses. Events It is important to decide early on what information you want to track about the comment. In our case, users will be subscribing to receive comments for a particular blog post. The best way to track a blog post is by its Id. Referring to the schema defined in part 2 each blog post has a 'PostId' and this is referenced as a foreign key to the Comment table. This is the first item of information we will need in our schema. No doubt we'll also need to send notifications with the comment title and contents, so we'll add those two fields to our schema as well. Our final entry for the application XML looks like this:
When your instance XML is imported into NS, and the application XML is processed, these fields will actually be created in a new table. You can also see we have defined an index that will be created in the same table. Notifications
The first part of the <NotificationClass> node is very simple to create. First we give our schema a name via the <NotificationClassName> node. Then we define our 2 fields in the <schema> node. Next we go on to define our content formatter. In our case, we want to use a stock standard XSLT to define our markup. So we specify its base path and file name. Finally, we want to define what protocol to use for this notification class. In the last post we defined our <DeliveryChannels> in the instance XML. Here, we must match a valid protocol from that list. We only defined one: of type SMTPDelivery, and called it 'SMTP'. We use that name here and go on to define all the important fields required for sending an email. Note that the <SqlExpression> field specifies the value to use. This actually is a valid SQL expression; the value you supply here is translated into an SQL statement to retrieve the values required for the notification. For example, the 'To' section specifies DeviceAddress as the value to use for this field. We'll learn about the DeviceAddress and where it comes from a little later, but suffice to say it is pulled from the database. For absolte literal values, we follow normal SQL conventions and put them in 'single quotes'. In the case you want to combine a literal value with a database field, combine them with normal SQL syntax, such as in the case of the 'Subject' field where we defined the literal '[BLOG_COMMENT] ' and the database value Name (which is the comment name). Subscriptions
As usual, we give our subscription class a name through the <SubscriptionClassName> node. Then, we define 2 parts to our subscription: the schema defining what fields can be subscribed to, and the rule that will process an event and turn it into a notification. The schema follows the same format as the <EventClasses> schema. For our application, we only want the user to be able to subscribe to a particular blog post, so we specify the PostId. However, we also want to track subscriptions on a 'per device' basis, so we also need to track what device the user is subscribing with. This manifests in our schema as follows:
Essentially, any subscription being made to the application will have to supply a device name and a PostId. However, when this schema is created as a table, Notification Services adds some other fields, such as a SubscriberId. In fact, all the schemas we've seen so far will be slightly modified by NS to keep things running smoothly. Next we must specify the rule to bring it all together. This rule takes the form of an SQL statement which joins potential subscriptions on to events. Each resulting row is a notification to be sent, and is parsed through the NotificationClass schema and formatters. Here is the <EventRule> we need to create notifications:
So first up we give our rule a name. Next, we define which event class we are comparing against for subscriptions. We only have one: NewCommentEvent. Finally the <Action> node defines our SQL statement that discovers potential notifications. Later on we define a time interval for how often NS should check for new notifications. When this interval fires, NS goes through all Event Rules and runs them. So lets delve into that a little further. Lets pretend NS is configured to fire off notification checks every 4 hours. Over a 4 hour period, our blog site gets 15 comments spread over 3 different blog posts: PostId 17, 18, and 19. NS already has received subscriptions for PostId 17 only, however 3 people are subscribed to that single post. In total 7 comments were made. The 4 hours expires, and the above SQL statement is processed. The result of the select statement will return 21 rows, which are inserted into the 'BlogCommentNotification' table. Another process fires and passes each row in this table to our XSLT formatter, and sends the notification via the defined channel (SMTP). Other Settings
This node is used to define how events get submitted to the application. If you intend to provide events through the managed API (as we are) then this must be set to a <NonHostedProvider>. The name you specify here will be used in your code. More on this in the next post. Summary Notification Services Series Further Resources Technorati Tags: Notification Services,Sql Server,Sql Server 2005,XML,Events,Notifications,Subscriptions 10 February Notification Services - Part 2 - Example Overview and Instance XMLAs mentioned in part 1 of my discussions about NS, I described how 'Subscribers' can subscribe via certain 'Devices' to 'Events' to receive 'Notifications'. In this post, I will demonstrate how to declaratively define your Notification Services instance. In following posts I will discuss how to create your application XML and talk to Notification Services using the API. Example Overview Previously we mentioned a user's blog as an example. We'll continue with that concept here. Imagine the database schema for a blog: You have 'Posts' which are performed by 'Users', and people can publish 'Comments' to them. A simple schema might look like this: You want to be able to let the Users of the blog subscribe to Comments made against particular posts. You've already built the main web application where users can login and post new blog articles. Now you want to extend the application to allow for notifications, and decide to use Notification Services as your technology. So you come up with a design and decide on the following:
You now have to do a couple of things. The first is to create the Notification Services instance/application, and the second is to modify your existing blog code to do 2 things: auto-subscribe a user who writes a post, and to also send all comments to Notification Services. Instance XML You decide to call your instance 'Intranet' and this specific NS application 'BlogNotifications'. First you have to create the instance XML, and the first draft looks something like this:
This means that you want an instance called 'Intranet' to be created on the local server. The available delivery channels to this instance are only SMTP at this stage. The name 'SmtpServer' is the name that will be used by your application code to reference what delivery channel to use for a particular subscription (more on that later). In Notification Services, anyone who is a subscriber, is defined at the instance level. This means only 1 list of subscribers is maintained for multiple applications. The application will be called 'BlogNotifications' and the 'ApplicationDefinitionFilePath' points to the second XML file we need to define. As mentioned earlier, you can have multiple applications in an instance. Here we have defined only 1, as with the Delivery Channels. It is possible to define variables to use in your instance XML to make things easier. These are called 'parameters' and when your instance xml is uploaded to Notification Services, it will prompt you for the values of any parameters you have defined. This works great in our case, because we may not want to 'hard code' the SMTP server. When testing we might use 1 server, but the production server might be somewhere else and therefore we would want to change the SMTP server at upload time. Parameters work perfect for this:
We would then alter our deliver channel code like this:
You can define any number of parameters. You can even use machine variables from the local OS in your parameters, as follows:
Finally, you can pass on values to your Application by defining some parameters for your application XML:
Here we are passing the value of a paramter called _Server_ as a parameter to the application XML called _NSServer_. That pretty much sums up the instance XML. In the next part, I'll discuss the Application XML how to link notifications to subscriptions. Notification Services Series Further Resources 29 January Getting Ready for 2008A new year comes with a new job and a new perspective. One of my personal goals this year is to increase my community involvement. There are lots of user groups in Brisbane, and similar opportunities Australia wide. I've filled in my Outlook calendar with all the upcoming events and thought I'd share the list with you as well (for the next month):
Also, in April (26th and 27th) is CodeCampOz. Although I've never been before, I'm aiming to drive down this year. Want to car pool? CodeCampOz has been described to me as a mini Tech.Ed except without the advertising. Readify provide presentations called RDNs which I hope to also get moved to Brisbane in the not-so-distant future. RDNs are like user groups, but the content can have more depth. Getting these in Brisbane will be great for the local development community. My blogging will hopefully increase in both quantity and quality. Currently I am working with Notification Services, Visual Studio 2008 and .Net 3.5. All new technologies for me, which means more tidbits to learn and share with you. I've also decided to move my blog to Windows Live Spaces. As portals go its not too shabby, and has a nice layer of extensibility. The official link is here. If you have a windows Live account then its easy for you to leave comments. Oh, and add me as a friend! I am currently cross posting to both old and new blogs for now, but you should definitely subscribe to the new version as soon as possible. There's some projects I'd like to attack this year. I've found a podcast receiver that works on Vista called PrimeTIme Podcast Receiver, but am not wowed by it. So one of my plans this year is to build a podcast subscription tool. Could be a good candidate to learn some WPF as well. I also want to work on a patterns web site that provides code examples in multiple programming languages. Most importantly, this year I will get certified. I've delayed it for too long. I have a few certifications in mind long term, but currently I am aiming for MCTS in Web Applications. Let me know if there is something you would like me to cover in my blog. Otherwise the direction will be random (as it has been). Also, if you would like to provide feedback on how to improve my writings/content, please feel free to be open and honest since it will only benefit you when my style gets better. Watch this space. 24 January Notification Services - Part 1 - Technology OverviewSql Server 2005 is more than just a relational database management system (RDBMS). It also contains a reporting engine called Reporting Services (SSRS), a data warehousing system called Analysis Services (SSAS), an engine for selectively pulling data from various sources called Integration Services (SSIS), and a subscription based notification engine called Notification Services (although I've not seen this notation, I presume we can say SSNS for short?). Each of these items are a technology unto themselves and take time to learn and master. Recently I started with Readify and my first job was to do a proof of concept for an internal project. It involved Notification Services and I was able to learn some cool stuff to share with you. SSNS is available in all versions of Sql Server 2005 ; all my development was done with Developer which costs around $80AUD. The core components of a SSNS instance are as follows. Subscribers Subscriber Devices Subscriptions Events Notifications How It Works Don't I Get To Write Code? However, some of the stored procedures are there for you to call; particularly around submitting subscribers, devices, subscriptions, and events. So you could call those sprocs, or you could use the managed API for Notification Services. This is just an assembly you can add a reference to in your application. For example, create a windows app that provides the interface for logging in and adding/removing subscriptions. With a few quick method calls you have connected to your SSNS application and not had to write any SQL! I can't stress enough how easy it is to use this API. You can do everything you need, and its very logical to use. Pulling It Together Now I just need to specify how the event data gets into the event schema in SSNS. I have lots of options here:
Oh I need to add a subscription too; since I now know SSNS well enough, I can just insert this into the right table; I don't plan on subscribing/unsubscribing continuously. Notification Services Series Further Resources Ready For ReadifyThought I'd post this before I go on vacation. Today I resigned from my position as a Lead Developer for Zap Technology. I've been offered an excellent position with Readify, a company I've viewed from afar for about a year and a half now. One of the things that sold me on Readify was their attitude towards the development community. Most people who attend user groups (anywhere in Australia) will have seen a Readify presentation. They hold a large percentage of the Microsoft MVPs in Australia and empower their employees to do the same if they so desire. In fact they even give you incentives to get active in your community. These are all the things I love to do: And they want to pay me to do it! Well I start with Readify in mid January. I was planning to move this blog to Microsoft Live Spaces when I got back but I'll reconsider this move now. One thing's for certain is that my postings will increase in frequency and in quality. Hopefully they'll sound less like a letter written to my mother, and provide more technical information. I also have a personal mission that I have challenged myself with: To drive the Brisbane/QLD development community, to the point where the other states get jealous. I hear so much about what's happening in other states on various blogs, but never Brisbane. This might be because I read the wrong blogs, but I'm a patriot none-the-less, and I want to see user groups with turnouts in the hundreds! I also want to see some deviation and expansion. Perhaps a Rich Web user group? Or a group dedicated to code quality (which fits with another idea I hope to act upon next year)? All ideas are welcome, and if you have any ideas for an open source project, I'd also be keen to hear. I'll use my holiday to think through some strategies for how to achieve these meager goals. I wonder if Readify have an indoor soccer team? Books Books BooksI'm sorry! I've been so busy, posting has suffered. I am now the Lead Developer for our primary Business Intelligence product at Zap Technology. My 3 month review at Zap resulted in this promotion which I happily accepted. Those who know me know that I was in a Lead Developer position prior to Zap. This post though is more of an encouragement to all you fellow Australian developers. The Australian dollar is doing quite well against the US dollar. Now is a great time to buy those technical/reference books you always wanted! My recent Amazon order went in when the dollar was at USD $0.92. Normally I purchase theory related books but saw this as a good opportunity to get some reference books as well. Here's my order, some of which have already arrived:
Hopefully you recognize some of the names. Kent Beck has been around the code quality scene for ages, and this new book of his is due out in January. Darren Neimke works for Readify, a Microsoft Gold-Partner consulting firm in Australia. Jon Skeet is a Microsoft MVP based in the UK. I respect Jon's commitment to the community and enjoy is writing style in news groups, and am looking forward to his first .Net related book in January. So get those orders in! Create yourself a wish list and get your family to buy you some books for Christmas. This will be the last blog post until after the Christmas season, so happy new year to you all! |
|
|