Attribute Based Caching with PostSharp

I’ve been meaning to write this post for months now to talk about my work with PostSharp and Attribute Based Caching. PostSharp is an AOP tool – I won’t get into too much detail about AOP and PostSharp as I have provided links to them

I used PostSharp to remove the amount of code required for the caching of my calls in a legacy application. Traditionally I would have had to write code such as this:

image

I was able to change the way this was done with AOP. This makes the code snippet look as follows:

image

I have effectively removed 2 lines of code from EVERY method call and replaced it with an attribute over the method declaration. In order for this to build, I had to either install PostSharp or run it as an import target manually in my .csproj file. So what does the output of the IL look like when decompiled?

image

You can see that the IL has had Aspects added to it. The 3rd party dll I used (CacheAspect) then knows what to add to the code and this is then IL woven using PostSharp. This means that the results of the MyMethod call will be added to the Cache with the key of StoredProcedureResults.

This has been majorly useful for me since I can invalidate the Cache on specific method calls

image

This method works by Invalidating the cache (key “StoredProcedureResults”) when the AddMethod is called. This scenario can be very useful when a list of users are cached and a new user is added to the list. It cuts down the code and is easy to read in my opinion.

I have posted before about how to hook PostSharp up to my CI server. AOP is not everyone's thing. It has been very useful for me on a small project. It has impacted the build time but I think its worth it for DRY code Smile

Free Speaker Training Day - 27th May 2011

It was announced last week that Guy Smith-Ferrier will be providing a free speaker training day in the UK on May 27th 2011. The details of the free training are located on Guy’s blog. I believe that this is a very popular event so if you are interested then please get in contact using the contact details on Guy’s blog post. In summary the day will cover the following details:

  • How To Explain Absolutely Anything
  • Planning Your Presentation
  • How To Give Great Demos
  • Getting Your Laptop Ready For Your Presentation
  • Presenting Your Presentation

The training will be lead by some very well known speakers in the UK community – Steve Sanderson, Dave McMahon, Ross Scott and Chris Myhill. This is a FREE event so please do try and take advantage of this if you can and are interested in speaking at user groups or regional events in the UK.

If you are like me, however, and cannot make that speaker training day then Guy has released speaker training videos. They are available on the technical community site. After speaking with Guy about this he has suggested that you would not need to watch the videos if you are going to the training day as they cover the same topics.

DDD Scotland 2011

Saturday 7th May was DDD Scotland in Glasgow. I was fortunate enough to be speaking at the event so obviously had to travel up there for the weekend :)

There were a range of good tracks available – agenda is here – and I started the day going to see Kendall Miller talking about How to Grow Your Own Software Company. Kendall is a very good speaker and delivered a very useful session. I really enjoyed the fact that he was able to relate his talk to his own experiences. If you can get a chance to see this session then I would recommend it – especially if you are thinking about setting up your own company!

I flitted in and out of a few other sessions throughout the day and really enjoyed the day as a hole. Got some great feedback on my session and Guy Smith-Ferrier very kindly critiqued my session for me so I can improve my delivery of my session.

Had a great day, met some great people and enjoyed having a day of being a geek. Thanks to the organisers – Colin MacKay, Craig Murphy, Andy Gibson and Barry Carr. Thanks again!

SQL CE 4 Database Difference Scripting with SqlCEDiffer

I wrote some posts on how to take two different SQL CE 4 db files (.sdf) and how to compare them to create a SQL change script. The general flow was as follows:

  • Generate Sql Schema File from SDF files
  • Compare the SDF files to generate a change script
    I started using this at work and it was noticed that the sql change script that was produced wasn’t fantastic. What it was doing was completely creating a new table, dropping the old table and then renaming that newly created table. We were worried that this would cause an issue with our database on our live server.
    After speaking with Erik Ejlskov Jensen, a SQL Server Compact MVP, I was pointed in the direction of the API that he wrote in order to make his Sql Compact 4 toolbox. This API allowed me to write my own little command line tool (as simple as it gets) that takes 2 .sdf files and generates a sql diff file.  I have called this, inspirationally, SqlCEDiffer.
    In order to use this tool you can do it from the command line or you can hook it into your MSBuild scripts as follows:
    Command Line

sqlcediffer.exe
    -source:{path to latest schema file}.sdf
    -target:{path to old schema file}.sdf
    -outputPath:{path to save the diff script to>.sql

It’s a very simple explanation really. Source is the latest sdf file created, this has all the new changes in it. The target is the source that we wish (eventually) to update. The output path is the path we want the resulting .sql file saved to.

 

Running from MSBuild

<Target Name="Build">
    <Message Text="Starting Sql Differencing" />
    <Exec Command="sqlcediffer.exe -source:{new sdf file} -target:{old sdf file} -outputPath:{save path}.sql" />
</Target>

It works exactly the same as from command line but being able to use the MSBuild exec command means that we can hook this into a build script. This now opens up the opportunity for its use in automation of sql differencing and potentially continuous database deployment.

The code is very simple and does exactly what it says on the tin. It has been created as a wrapper around the code that Erik already created for his tools already available on codeplex.

DDD Southwest – Get Ready to Register

I found out that my session on “SpecFlow - functional testing made easy” was selected for inclusion on the DDD Southwest agenda for June 11 2011.  This has made all 3 DDD events so far this year for me which I am very happy about :) More information about my talk can be found on this blog on my public speaking page

DDD Southwest registration opens on April 12th at 10am – Go to http://www.dddsouthwest.com/ and register as it will prove to be a great day judging by some of the talks I know that are selected. If you want to see the agenda before you sign up then you can find that one the dddsw site as well

On the day be sure to come and say hello :)

Using Sql Compare from the Command Line

In March, I wrote a post about how to generate sql schema scripts from a SQL CE4 database file from the command line. After working out how to do this I wanted to be able to take the generate sql server schema (in .sql file) and compare it against an existing schema. The resulting differences would then generate a sql change script.

I tried Sql Compare, a Redgate tool, and this seemed to prove effective for what I was able to do. After doing a bit of research I was able to find out that Sql Compare isn’t able to compare individual .sql files. BUT you could give it a directory path and compare ALL the .sql files in a directory.

Previously I generated a schema.sql file and placed this file in the following folder: c:\temp\schema

I had an old sql schema file that from previously investigatations with .sdf file. I took one of those files and put it in c:\temp\oldschema

I was then able to run the command as follows from the command prompt:

cd "C:\Program Files\SQL Compare 8"

sqlcompare.exe
    /scr1:"C:\temp\new"
    /scr2:"C:\temp\oldschema"
    /o:Default
    /ScriptFile:"C:\temp\SqlChangeScript.sql"

This script effectively says to compare the old schema files (scr2) against the new schema files (scr1) . O:/default specifies that the default options for comparison and synchronisation will be used. The resulting sql change script will be saved as SqlChangeScript.sql. I could have changed the last option from /ScriptFile to /sync which would have run the synchronisation in place and not produced the change file. I wanted the change file for version control and release history.

When running this through the command line I was able to see the following:

image

This worked perfectly and generated a change script that has everything rolled in a transaction to make sure no errors will run. The nice touch here is that there are messages printed when an action is started.

I didnt want to keep 2 schema files on my build server for comparison so I then started to look at how to compare the new schema file against the database itself. Low and behold this is also available with Sql Compare and works as follows:

sqlcompare.exe
    /scr1:"C:\temp\new"
    /Server2:"<server name or ip>" /db2:"<database name>"
    /o:Default
    /ScriptFile:"C:\temp\SchemaSyncScript.sql"

The only difference here is to change the /scr2 to actually be a database instead of another sql server file.Again this worked as expected and created a nice sql change script that I could then use for database deployment.

If you have any aspirations of automated database deployment then I think that you should try the trial of Sql Compare. It has been very useful for me. I’m just very glad my company already had a company of it I could use for the build server. As this process can be run from command line it can also be run from MSBuild and as part of a TeamCity build. To be continued……………

Using Add Deployable Dependencies in VS2010 SP1

Before the dawn of VS2010 SP1, when I wanted to deploy dependency dlls, I had to go to the properties of the .dll and set CopyLocal to true

image

This was critical for me when MVC3 first came out as MVC3 was not installed on our production server and we needed to deploy the MVC3 and Razor dlls to our servers in order for the application to run.

The nice people at Microsoft added a feature in VS2010 SP1 that allowed you to specify a specific set of deployable dependencies. When you right click on the web project you can now see a menu option as follows:

image

When you choose this option you get an option to choose the dependencies you want:

image

If I choose ASP.NET MVC and select ok I will now have a new folder added to my solution that has the following contents:

image

This folder is now taken care of using the new Microsoft.WebApplication.targets and Microsoft.Web.Publishing.targets file that comes with MSBuild and was upgraded after VS2010 SP1 install. MSBuild will take care of this folder and its contents and move those contents to the /bin of the application. This will work as part of a CI build or as part of using a publish with MSDeploy.

This has made my life easier as I used to have to find what files were needed as part of the deploy and keep a copy of them in a /lib folder and then use an MSBuild copy task to copy them as part of my CI build

IIS Rewrite Module and ASP.NET MVC3

This morning I found out that the IIS Rewrite module causes a problem with MVC routing when trying to POST back to the server. I added a rule to the web.config to enforce all lowercase URLs as follows:

image

As you can see this is the rule that comes out of the box with the module. There doesn’t seem to be any notes included with this particular rule. This adds the following to the web.config

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true"></modules>
    <handlers>
    <remove name="UrlRoutingHandler" />
    </handlers>
    <rewrite>
        <rules>
            <rule name="LowerCaseRule1" stopProcessing="true">
                <match url="[A-Z]" ignoreCase="false" />
                <action type="Redirect" url="{ToLower:{URL}}" />
             </rule>
         </rules>
    </rewrite>
</system.webServer>

This works as expected as it changes http://localhost/SiteName/Account/Register  to http://localhost/sitename/account/register. We have decided to keep all URL’s lowercase and enforce the rule so Google cannot see duplicate URLs due to casing. When the user is presented with the register view, a form on that page has a POST action. When that POST action posts to a non-lowercase URL then the Rewrite module weighs in a redirects to the lowercase URL and presents the user with the view again and not with the results of the POST.

I mentioned this on twitter and Phil Winstanley, of Microsoft, kindly offered to enquire about this on the internal distribution lists to see what that brought back. The responses helped me understand what actually happen. they also pointed out that the behaviour, although it isn’t strictly correct, is kind of expected. According the HTTP spec

“If the 301 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request
unless it can be confirmed by the user, since this might change the conditions under which the request was issued.“ http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.2

It was also pointed out to me that as Login and Register are not really sites that are promoted for SEO purposes then we can exclude these routes from the redirect rule. Overall I have seen that I have not got a flawless plan for enforcing lowercase URLs. If I want lowercase URLs then I should really enforce them myself in my code. The one thing I’m slightly confused about is that there are no notes that come with the IIS Rewrite module that tell me these types of things. Fair enough I can get them from HTTP spec, but I really didn’t realise that this would be an issue.

If possible, I’d love people to see that the Rewrite module documentation may be lacking in specific areas. Hopefully this will improve. I’d like to thank Phil and all the guys at Microsoft who helped with responses in this query.

Extracting a Sql Server Schema from Sql Server CE File

My current project at work sees me building an ASP.NET MVC3 project. When the project was being built initially, we had no idea of what the final schema of the database would look like. So we built the app using a fake dataset that we can in cache and read / wrote to that cache during the application. The decision was made that we would use Entity Framework 4 for our data access method and as we already had  business entities in place, we thought that Code First would be a good way to go since it was in RC.

We didnt get our DBAs involved as the schema wasn't completely finished. So we decided to use Sql Server CE 4. I know what you are thinking  - MS Fan Boy, new technology freak – and you would be right. I’m happy with what we have chosen and its worked for us. We have now managed to get a database schema that we are happy with.

BUT we hit a flaw when we looked at how we migrate our Sql Server CE 4 database to Sql Server. I know there are tools available but I didn’t feel that I had a huge amount of control over these and I wanted one to be able to run from msbuild since I will (eventually) be automating the process.

Step up web deploy v2! I managed to find an article a while back that was tweeted by Sayed Hashim on the new features of web deploy v2. There is build in support for this exact purpose. So in order to run the process I opened a command line and change directory to the Web Deploy v2 folder (C:\Program Files (x86)\IIS\Microsoft Web Deploy V2). I can then run the command as follows:

msdeploy
-verb:sync
-source:dbFullSql="Data Source=<path to .sdf>",ScriptData=false,sqlCe=true
-dest:dbFullSql="<output .sql path>"

This is pretty self explanatory but the important parts are to include sqlCe = true as this will make msdeploy know its a sql compact edition database. ScriptData = false will mean that only the schema will be output and no data will be included. If this is omitted then the schema will be exported as well as the data.

When I run this i get something similar to this screen on my console:

image 

Of course this command can be included in a batch file and be executed from TeamCity (post coming soon on this!) or manually run when necessary.

The output of the script can be opened and run against Sql Server – or if you wait for my next post – to be used to create sql change scripts that can be used from continuous database deployment.

TeamCity & IIS SEO Toolkit

TeamCity has been proving to be one of the most useful tools I have used in recent times. It has helped me from continuous deployment of sites to staging and production environments,  allowing security penetration testing, running automated acceptance tests with SpecFlow and much more. I really hoped there would be a way that I could test the SEO capabilities of a newly deployed site. Usually when I deploy a site I will open IIS console and run a Site Analysis against the URL using the SEO Toolkit but I thought there must really be a way to run this from TeamCity.

Over the past week or so I have been writing some prototype code that will allow me to use the SEO toolkit dll to create a sits analysis. This worked out to be effective and since I was able to run the analysis from code I was able to run the application from TeamCity.

the code for this application is in very early stages as it was written with the help of some sample code on MSDN. It will evolve over time and I’ll make this available. The plans for the application are to support different parts of the analysis to be added to the report by config parameters. As I am sure this code could be misused I have added a small Thread.Sleep so that it can’t be used to DoS attack a site. I accept no responsibilities for the code being misused.

Configuring TeamCity to Run the Application

Log into TeamCity as an administrator and go into the administration screen.

CreateProject

Click on ‘Create Project’ and enter some project details:

ProjectDetails

Next we want to create a build configuration. So click on ‘Create build configuration’:

CreateBuildConfiguration

Add some general details – name, description and build number format. The most important thing to note here is the artifacts text area. It has SEOReport.html added to it. This is the report that the crawler application creates. We add this to the artifacts so that we can add the results to a report tab.

BuildSettings

We will be asked next to enter some VCS settings. I have had to hack things slightly. Technically I don't have to check this application out of version control. So I enter the path to the application file in the checkout path as follows:

VCSSettings 

The build step is the next thing to add. In the build steps over click on Add Build Step and choose Command Line from the runner type drop down menu. This is where we call the application now:

BuildStep

As you can see we leave the working directory empty as we have specified the directory already in the VCS stage. We can then call the .exe from the command executable. The parameters is where it gets interesting. This is were we pass in the site to scan – if a site is not specified then an application exception will be thrown and the build will fail.

When you have finished the setup and passed in a site then run the build. On a successful build the build details page will show (a newly configured) SEO Crawler Report tab:

ReportTab

If you don't know how to add a custom report tab I’ll cover this in a separate post. But on the successful build and we click that tab we get a very simple report as follows:

ReportOutput

This report only has a summary of URLS, links and violations as well as site status codes and broken links. But as this application provides a full scan that the SEO toolkit can do we can make the application output more details. In coming versions of the code, I’ll allow it to be configurable as to which sections can be added to the report.

How Can I View The Full Report?

As I mentioned, the crawler application does a full site analysis. The same analysis that IIS does. In the application root folder an IIS SEO Reports folder. This folder will contain all the scan reports. So if you really want to you can take the scan folder and drop it to a know IIS location (for me is MyDocuments/IIS SEO Reports) and IIS can display the full report as it would have done before.

Another tip of the cap to TeamCity. I know as this is an executable it can be called from any build script but its just made very easy for me with the build in build runner and parameters acceptance.

About Me

 

Web Developer. My most used framework is C# with MVC but use Webforms on occasion. Im an advocate of clean, maintainable code and am very passionate for what I do. Absolutely obsessed with Continuous Integration and how it should be used in every day development scenarios. Trying to move towards a system of Continuous Deployment
Follow Me on Twitter

Jetbrains Academy Member

 

MVB Blogger

 

Friends Of Redgate

Month List