Archive for ◊ December, 2011 ◊

Author:
Thursday, December 22nd, 2011

Before starting with how to achieve continuous integration with TFS, lets try and understand why it is required?

Wiki says:

In software engineering, continuous integration (CI) implements continuous processes of applying quality control — small pieces of effort, applied frequently. Continuous integration aims to improve the quality of software, and to reduce the time taken to deliver it, by replacing the traditional practice of applying quality control after completing all development.

Which is very true!

And the principles of continuous integration are :

  1. Maintain a code repository
  2. Automate the build
  3. Make the build self-testing
  4. Everyone commits to the baseline every day
  5. Every commit (to baseline) should be built
  6. Keep the build fast
  7. Test in a clone of the production environment
  8. Make it easy to get the latest deliverables
  9. Everyone can see the results of the latest build
  10. Automate deployment

If you can think for a while how every stuff adds value and helps in getting the quality product out at a higher rate by reducing risk you have already 50% through with the process. Rest all is matter of utilizing the feature provided by the CI tools/infrastructure.

Lemme start with maintaining code repository. If you follow few guidelines, it makes the life much easier to maintain the code. It all depends on nature of the application & process you follow.

Define your environments:

Defining branching is major decision to be taken and it all depends on the scenario or the process you are in. Go throw this site before strategizing the branchinghttp://branchingguidance.codeplex.com/.

For instance, lets consider a simple branching strategy around environments (dev,qa & prod) with single release.

Define environment dependant configuration:

First and foremost, define the configurations for each environment. Configurations as such, DB to point, service url to refer, user accouts to be used etc. This can be achived by having a configuration file each environment.

1.       Select the solution

2.       Right cick -> configuration manager

3.       Define the configuration for all environments (and debug/release modes for local settings & dev workarounds) -> Using Configuration Manager

4.       Make use of the interesting feature Add Config Transforms offerred by VS2010/.net4.0 web application project template to maintain web.config for each configurations.

5.       Strangly we don’t have this luxury for other application templates. Thr is a combursome procedure of modifying your cs proj file to achive this feature (refer:http://philbolduc.blogspot.com/2010/03/using-config-transforms-outside-web.html).

I prefer to write a small batch scripts which overrides the app.config  befroe the build. Call the batch script on pre-build event of the project.

Define TFS build for each environment:

TFS2010 has very strong and extensible support for build J. Its easy and scallable. You can get a smiple build procedure up, by following the new build wizard

1.       Go to team explorer in VS 2010 ->  Builds -> New Build Definition

2.       Define the build for dev environment.

a.       Name

b.      What triggers the build. Notice the interesting scheduling options J

c.       For which solution?

Path of the soultion to build. You can have multiple solutions build on same time. Have an eye on Build Agent Folder path.

d.      Need to drop the output of the build to a location? Oh yeah, you can do it in build defaults.

e.      Here comes the most important step of build definition, the Process.

f.        Quick tips:

i.      As I stated earlier, we will be having a different builds for each environment. Now it’s the time for chosingthe configuuration we defined. Provide appropriate configuration name for the property Configuration to  Build.

ii.      Want to run the unit testcases? Provide the assemblies for the propert “Test Assembly Filesspec”.

iii.      Want to fail the entire build on test fail even if the compliation was successful ? set “Fail Build On Test Failure” to true

iv.      Want to run the code coverage? Enable code coverage (refer): http://blogs.msdn.com/b/ukvsts/archive/2009/11/06/enabling-code-coverage-in-vs-2010-beta-2.aspx) and provide the local.testsettigns file to “TestSettingsFile”.

v.      Want to run the Code Analysis on the build? Set the “Perform Code Analysis” option. Make sure, you have selected appropriate rulesets in the project (refer:http://visualstudiomagazine.com/articles/2010/03/25/working-with-static-code-analysis.aspx).

vi.      Want to deploy the application? (Only Web Apps) Provide publish parameters to “MSBuild Arguments

/p:DeployOnBuild=True

/p:DeployTarget=MsDeployPublish

/p:MSDeployPublishMethod=InProc

/p:CreatePackageOnPublish=True

/p:DeployIisAppPath=”Default Web Site/WebApplication1″

/p:MsDeployServiceUrl=localhost

Ref: http://weblogs.asp.net/jdanforth/archive/2010/04/24/package-and-publish-web-sites-with-tfs-2010-build-server.aspx

vii.      How to deploy web or any other applications? This is a generic process, invbolves writign some batch scripts J

Ref : http://blogs.msdn.com/b/benoitsarie/archive/2010/06/03/how-to-deploy-a-web-application-with-web-application-project-deployment-part3.aspx

g.       You define the rention policy too.

This ends defining the build for a specific environment. Repeate the above steps for other 2 environments/branches with environment specific details such as ,

1.       Solution location

2.       Scheduling

3.       Drop location

4.       Configuration to run

5.       Enablingor disabling of Automatic deployment, Runnign unit test, code coverage or any other details.

Once we have have all the 3 build definitions defined, we can view, enquue,delete or modify the build.

Click onView Builds to see all possible builds

Click Open to explore  different features provided and majorly to see what happened with the build.

Opt for alerts to be on track with build status by selecting Project alert option under “Team” menu in toolbar.

Author:
Tuesday, December 20th, 2011

I was told to write blogs many times in many ways! I always wanted to write a blog too. And I did write few on an average of one blog/year. I was never motivated to write blogs. But then, one day I had really interesting conversation with couple folks at my work place. Even that did not motivate me to write a blog, but that conversation made me ask myself lot many question about writing blog.

The answer I got back was, It was not about writing blog. It’s more about telling a story, sharing an experience and I read somewhere “if you don’t tell your story then who will?” I think, that makes a lot of sense! Ironically, I got that from a blog!

So, I decided to write this blog to talk about why to write a blog or tweet.

I know, most of us are fascinated by history. It amazes surprises and even horrifies us.  What is the biggest take from history the current generation is enjoying? “The lessons learnt”. So, how did we receive all these information? Was these things told to us by our ancestors? How? As stories? How did they communicate their experiences to us? Who told us?

What would have happened, if everybody has seized their knowledge to themself? Lemme not elaborate too much, you know what I’m talking about!

You have learnt something from the community you are in. It’s your responsibility to give it back! We living in the digital world should be very lucky that we can contribute back without much effort. And that need a not be a blog!

Communication is important. How do you communicate effectively is even more important. The best way of communication is make use of the digital world u live in. It is fine, if you write a book! It’s fine, if you click a pic! Don’t miss to tell the story! Whatever it might be, something which has a flavor is always stands out! You story has a flavor, share it! Just, casually share your thoughts “If you don’t tell your story then who will?”

Category: General  | Leave a Comment
Author:
Friday, December 16th, 2011

If you want to restrict access to your Azure webrole (url) based on users’ IP Address, then this is the best article:

http://blog.bareweb.eu/2011/04/restricting-access-by-ip-in-azure-web-role-v1-4/

It has clear instructions and screenshots. But if your webrole keeps restarting when trying out those instructions, then read further for a fix.

However, I just couldn’t get it working at first. When I added the “Startup.cmd” to my project as mentioned in the article and tried to publish, the it just wouldn’t publish. It would just show me publish in progress for a long long time. On the Azure Management Portal, the webrole would just keep restarting.

I had to connect to the webrole using remote desktop to figure out the issue. (Ref: http://msdn.microsoft.com/en-us/library/windowsazure/gg443832.aspx).

It turns out, I needed to save the “Startup.cmd” file as an “ANSI” text file and NOT Unicode as mentioned in that article.

And more more thing, don’t forget to add the missing slash (\) in your batch files. You can copy paste from below:

@echo off

@echo Installing "IPv4 Address and Domain Restrictions" feature 
%windir%\System32\ServerManagerCmd.exe -install Web-IP-Security

@echo Unlocking configuration for "IPv4 Address and Domain Restrictions" feature 
%windir%\system32\inetsrv\AppCmd.exe unlock config -section:system.webServer/security

There you go, just save the file in ANSI text format and things should work for you as described in that article.

If your cloud deployment still fails or if the role instance keeps recycling (not starting), you should disable the startup action temporarily and deploy. Then remote desktop to an instance and try to run the Startup.cmd file on it yourself or try the commands (above) individually from command prompt. That should help you detect what’s wrong.

If you deployment works fine, but the IP address restrictions doesn’t to work (i.e. you are able to access from unallowed locations), then there problem might be with the location setting in Web.config. Remove the location setting and try with a global restriction like below. This should block access to all pages on the site.

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
        <security>
            <ipSecurity allowUnlisted="false">
              <add allowed="true" ipAddress="111.111.111.111"/>
              <add allowed="true" ipAddress="222.222.222.222" />
            </ipSecurity>
        </security>
  </system.webServer>
</configuration>

If this works, then you know that there was something wrong with the location settings. You can try to fix it or leave the above setting (if you’re OK with restricted access to all pages and instead of  a particular location).

Cheers.

- Anand Gothe

Friday, December 09th, 2011
Interceptor pattern can be used for addressing cross cutting
concerns in the application that happens at either the beginning or end of a
method. For e.g. checking the validity of the input parameters before executing
the method and throwing exceptions, catching and logging exceptions etc. These
situations can be encapsulated in a reusable component that can be processed by
the compiler and produce executable code (AOP).
Unity allows developers to implement the idea of
interception by allowing creation of objects that can add some extra code on
the target method before or after the regular execution of these target
methods. In this post, I’ll show how to create a Session handler for ASP.Net
using the interface interception mechanism provided by unity application block,
that will allow you to address encapsulate the logic for checking session for a
value before execution of the method and returning back the value from session
if available. The same handler can be used to also add the return value to the
session if not available so that this value can be retrieved next time.
Creating the session handler
public class SessionHandler : ICallHandler
{
private readonly string _sessionKey;
public SessionHandler(string
sessionKey)
{

_sessionKey = sessionKey;
}
public ISessionService
SessionService
{
get { return Container.Instance.Resolve<ISessionService>(); }
}
public IMethodReturn
Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
{
if (SessionService.Contains(_sessionKey))
{

input.CreateMethodReturn(SessionService.Get(_sessionKey));
return getNext()(input, getNext);
}
var returnValue = getNext()(input, getNext);
SessionService.Add(_sessionKey,
returnValue.ReturnValue);
return returnValue;
}
public int Order { get; set; }
}
The session handler uses the session service which
encapsulates the actual Session object to add/ remove values from the
session.  You can also use the
HttpContext.Current.Session object here.
Creating the Attribute for the handler
public class SessionDependencyAttribute : HandlerAttribute
{
private readonly string _sessionKey;
public SessionDependencyAttribute(string sessionKey)
{

_sessionKey = sessionKey;
}
public override ICallHandler CreateHandler(IUnityContainer container)
{
return new SessionHandler(_sessionKey);
}
}
Creating a service and using the SessionHandler we created.
public interface IMockService
{
[SessionDependency("TestKey")]
string GetValue(bool
flag);
}
public class MockService : IMockService
{
public string
GetValue(bool flag)
{
return flag ? “True”
: “False”;
}
}
Configuring the Unity container
_container = new UnityContainer();
_container.AddNewExtension<Interception>();
_container

.RegisterType<ISessionService, SessionService>()

.RegisterType<IMockService, MockService>()

.RegisterType<MockPresenter>()
.Configure<Interception>().SetDefaultInterceptorFor<IMockService>(new
InterfaceInterceptor());
Test cases
[TestMethod]
public void
MethodShouldReturnValueFromSessionIfValueDoesNotExistsAndAddToSession()
{
var service = Container.Instance.Resolve<IMockService>();
var actual = service.GetValue(true);
Assert.IsTrue(SessionService.Contains(“TestKey”));
}