Sunday, February 28, 2010

Upgrading TFS event subscriptions to 2010 SDK

Some days ago I started to upgrade one of my TFS customization to the 2010 SDK. The application I moved uses WCF to host its services and automatically subscribes and consumes TFS events. It didn’t turn to be the easy, straight on upgrade I thought it should be. This is my findings during the upgrade.

Relocated TFS SDK assemblies
The first (and about the only) thing I expected was to be replace TfsServer with TfsTeamProjectCollection, and in some cases TfsConfigurationServer. Moving my solution over to a new and clean machine with only VS2010 on it, I discovered that the Tfs SDK Assemblies has moved away from its old locations. After some searching I found the new location C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ReferenceAssemblies\v2.0

Where did IEventService go ?
After finding the new assemblies, replaced TfsServer with TfsTeamProjectCollection I still had some compiler errors, the IEventService interface was unknown? I tried to search for any information about changes in 2010 but came up short. After some searching I found it again. It had simply moved around in the namespaces and is now located at Microsoft.TeamFoundation.Framework.Client namespace.

Windows 7 security
With all compiler errors fixed, it was time for a first test. I started the application and got direct failure. It turns out that you have to grant rights to url namespaces with
netsh http add urlact url=http://+:8001/ServiceUrl user=mydomain/mysuser
After granting rights to my user the the services starts and the application subscribes to events.

TFS2010 switched to Soap1.2
After some testing I don’t receive any incoming notifications. Following Grant holidays post TFS2010: Diagnosing Email and SOAP subscription failures (http://blogs.msdn.com/granth/archive/2009/10/28/tfs2010-diagnosing-email-and-soap-subscription-failures.aspx) shows me the following error
HTTP code 415: Cannot process the message because the content type 'application/soap+xml; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'
This indicates that tfs has switch to Soap 1.2. To solve this I simply switch the bindings of my services from BasicHttpBinding till wsHttpBinding

Finaly working
The last thing it took to get the events working was to change the security mode to SecurityMode.None. So if you want to set up an WCF endpoint this code will do it, without any extra configuration files.

Uri baseaddress = new Uri("http://" + System.Environment.MachineName + ":8001");
srvHost = new ServiceHost(typeof(NotificationServiceImpl), baseaddress);


// Check to see if the service host already has a ServiceMetadataBehavior
ServiceMetadataBehavior smb = srvHost.Description.Behaviors.Find();
// If not, add one
if (smb == null)
smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Default;
srvHost.Description.Behaviors.Add(smb);
// Add MEX endpoint
srvHost.AddServiceEndpoint(
ServiceMetadataBehavior.MexContractName,
MetadataExchangeBindings.CreateMexHttpBinding(),
"mex"
);

srvHost.AddServiceEndpoint(typeof(INotificationService), new WSHttpBinding(SecurityMode.None ), "StructureChangeNotify");
srvHost.Open();


Complete code
Below you can find the complete code, you can also download a zip file with the solution from my skydrive http://cid-5d46cae8c0008cf0.skydrive.live.com/self.aspx/.Public/EventSubscriber2010.zip

// INotifyServices.cs
namespace EventSubscriber
{
[ServiceContract(Namespace = "http://schemas.microsoft.com/TeamFoundation/2005/06/Services/Notification/03")]
public interface INotificationService
{


[OperationContract(Action = "http://schemas.microsoft.com/TeamFoundation/2005/06/Services/Notification/03/Notify", ReplyAction="*")]
[XmlSerializerFormat(Style = OperationFormatStyle.Document)] /* Took me hours to figure this out! */
void Notify(string eventXml, string tfsIdentityXml);


}
}


//
NotifyServices.cs
namespace EventSubscriber
{
public class NotificationServiceImpl : INotificationService
{
void INotificationService.Notify(string eventXml, string tfsIdentityXml)
{
MessageBox.Show(eventXml);
}
}
}

// Form.cs
using System;
using System.Windows.Forms;
using Microsoft.TeamFoundation.Framework.Client;
using Microsoft.TeamFoundation.Client;

using System.ServiceModel;
using System.ServiceModel.Description;





namespace EventSubscriber
{


public partial class Form1 : Form
{
protected ServiceHost srvHost;
protected int subscriptionId;



public Form1()
{
InitializeComponent();
}



private void cmdStartWCF_Click(object sender, EventArgs e)
{

Uri baseaddress = new Uri("http://" + System.Environment.MachineName + ":8001");
srvHost = new ServiceHost(typeof(NotificationServiceImpl), baseaddress);


// Check to see if the service host already has a ServiceMetadataBehavior
ServiceMetadataBehavior smb = srvHost.Description.Behaviors.Find();
// If not, add one
if (smb == null)
smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Default;
srvHost.Description.Behaviors.Add(smb);
// Add MEX endpoint
srvHost.AddServiceEndpoint(
ServiceMetadataBehavior.MexContractName,
MetadataExchangeBindings.CreateMexHttpBinding(),
"mex"
);

srvHost.AddServiceEndpoint(typeof(INotificationService), new WSHttpBinding(SecurityMode.None), "WorkItemChangedNotify");



srvHost.Open();

lblRunning.Text = "Running";



}

private void cmdStopWCF_Click(object sender, EventArgs e)
{
srvHost.Close();
lblRunning.Text = "Stoped";


}



private void cmdSubscribe_Click(object sender, EventArgs e)
{


TfsTeamProjectCollection tpc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(txtServerUrl.Text));
tpc.EnsureAuthenticated();


IEventService eventService = tpc.GetService(typeof(IEventService)) as IEventService;
DeliveryPreference delPref = new DeliveryPreference();
delPref.Address = "http://" + System.Environment.MachineName + ":8001/WorkItemChangedNotify";
delPref.Schedule = DeliverySchedule.Immediate;
delPref.Type = DeliveryType.Soap;


subscriptionId = eventService.SubscribeEvent(System.Environment.UserDomainName + "\\" + System.Environment.UserName, "WorkItemChangedEvent", "", delPref);
lblSubId.Text = subscriptionId.ToString();


}

private void cmdUnsubscribe_Click(object sender, EventArgs e)
{

TfsTeamProjectCollection tpc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(txtServerUrl.Text));

IEventService eventService = tpc.GetService(typeof(IEventService)) as IEventService;
eventService.UnsubscribeEvent(subscriptionId);
lblSubId.Text = "na";
}


}



Wednesday, January 20, 2010

Performance optimizing with the vs2010 profiler I

Optimizing existing code for performance is one of my favorit tasks. It is a work what gives immediate measurable feedback, always learns you something new, and is in many cases easier then you think, especially if you use the new profiler in vs2010.

Setup
The first thing you need to do is find some way to test that you still deliver the same functionality. Now days this tends to be less of a problem, as many projects tends to have some kind of unit tests (although in many cases it’s more automated Integration tests) . If not it time to write some 
Other ways to accomplish this is to agree/ write some testcases

Establish a base line
Once you have a way to test that you don’t (unknowingly) change the functionality its time to find and reproduce the performance issues. It is not reproducible in a new environment, it's in most cases better to try to find the bottlenecks in the environment before you go for optimizing the code.
After reproducing the performance issues, the next step is to establish a base line. By setting a baseline before you start changing the code you have something to compare your results against. Establishing a baseline is more a procedure and some extra thoughts on how to test and measure your progress. In practical it’s the first performance report you collect from your system saved away

Creating a performance session
Now its time to get started, simply create a new performance session, As before you can choose between sampling and instrumentation.Sampling providing a quick and good enough approach and instrumenting as an more exact and complex approach. I tend to end up doing the instrumenting, although I must admit sampling really is good enough for me, I guess I like the feeling of exactly knowing what’s going on. You can read at msdn: Walkthrough: Profiling Applications

All in one profiling
A new feature in the Vs2010 profiler is that it now collects data about calls to other tires like the database. Before I was most of the time forced to use both the Profiler and SQL server profiler to collect information about database interactions and where execution times. With VS2010 I now can collect basic database interaction information directly in the VS2010 profiler, showing you the queries executed and their execution time. This information is valuable enough to point me in the right direction witch query to look deeper into, although I still can see cases there I will use the SQL Server Profiler.

Better performance reports
One other great improvement from usability perspective is the new graphical presentation of your function, both showing a graphical representation of the time spent in the function, and the code annotated with execution times! The graphical representation of the function calls is also clickable. This makes it really simple and almost joyful to navigate around in your callstack looking at the code and the execution times, giving you a quick idea about what needs and can be improved.
Colin Thomsen has writen 2 post about the new performance reports VS2010: New Profiler Summary Page and VS2010: Investigating a sample profiling report (Function Details)

Tuesday, December 8, 2009

A new beginign for the TFS AdminTool

If I had to pick the most important tool for TFS, it would without a doubt be the TFS Admin tool. It has saved me (and many others) a lot of trouble and many, many hours. To be truly honest it has made me say some bad words to, from time to time.
This makes it extra joyful to read Ladislau’s
post on the release of a CTP for the new TFS Admin 2.0 version with TFS2010 Beta2 support.

New user interface

The CTP shows of a completely new user interface. New Features includes
• bulk edit of users
• pending changes window
• output/trace window.
The most important is that the new UI seems to be more reliable than the old one.

Completely redesigned and rewritten codebase

The new user interface is not the only thing the team behind the TFS admin tool has accomplished. The TFS Admin code has gone through a major reconstruction, separating the user interface from the logic and making the code more testable. The core management of user rights is package in a separate project/assembly making it very easy to use/and extend. It is now possible to write your own gui/process and relay on the TFS admin tool to take care of the boring and time consuming tasks. Great news for the TFS community!

Thursday, October 15, 2009

VSTS2010 as a modeling tool II

In my last post I wrote about VSTS 2010 as a modeling tool for more abstract levels like general UML illustrations and requirements modeling. In this post I will take a look at VSTS2010 as a modeling tool for more technical modeling.

UML at the concrete level

The next step after requirements is usually to do some kind of analyzing and/or basic design as the first step towards the destination - real code. After the initial design the next step is to complete the design into a fully technical specification. Now days many projects tend to skip formal analyzing and initial design and go direct to coding. For the people who do go all the way there is/has been tools like Rational Rose/XDE, Enterprise Architect and UModel who could even go the whole way to generate code from the design.

VSTS2010 as an analyzing and initial design tool
Doing analyzing and initial design requires more of your tool than just fancy images. You need some kind of repository to share and reuse your discovered elements. VSTS2010 handles this through the UML Model Explorer. The model explorer act as an in memory repository making it possible to browse through your defined elements. You can organize your elements hierarchy in the model explorer by creating package.
In VSTS2010 you can create class, activity and sequence diagrams and drag and drop elements from the model explorer to your diagrams. Changing objects, like renaming classes or operations, updates all diagrams. It is also possible to promote object and messages to classes and operations. Basically it got most of the capabilities needed for analyzing and initial design of a system.

VSTS2010 as an advanced design first tool
When it comes to doing model first design and more advanced scenarios this first version of Microsoft’s UML modeling tools hits its limitation. The first and most important limitation for using as a complete or advanced design tool is the lack of connection to the source code. Other tools like Enterprise Architect from SparxSystems or UModel from Altova can go the whole way and synchronize (round tripping) your design model and source code.

Even if I could model everything inside VSTS it is not much use going to that kind of detail, if I can’t reuse it to create code. Putting that much effort in a design model requires the ability to generate code and sync changes in code back to the model.

The second limitation or showstopper is the lack of import export features. Especially the capability to import models or code to the model makes it harder to start using it on an existing project. You basically have to create all your elements by hand if you want to use it on an existing project. It would be really useful to have the capability to import source code to generate classes in the model.

VSTS2010 features for understanding existing code
Even if VSTS2010 lack some vital capabilities as an advanced design first model tool, it has many advanced features for architecture and design. VTST2010 focus a lot on understanding your existing code, and the architectural explorer and the architectural tools provides good value and is worth a own post. When it comes to design and UML tools VSTS2010 has a great function for generating sequence diagrams from code, and beta2 promise to be even better, providing drilldown, comments and interactivity in the sequence diagrams making it even easier to understand the existing design.

Summary and reflections
I think that the VTST2010 modeling tools can provide a value in most projects, as a general UML tool or as a tool for analyzing and initial design. If you don't have a modeling tool today, or have an advanced tool you're not using to the full extent should definitely take a look at the model capabilities of VSTS2010. One good way is to stay tuned to Cameron Skinner's blog and his coming post on modeling in VSTS2010.

Tuesday, October 6, 2009

VSTS2010 as a modeling tool

Earlier this week I had the opportunity to take the VSTS beta out for a test drive as a modeling tool. My task was to suggest and describe possible scenarios of usage for an external vendor. Luckily I had a virtual environment with vsts2010 beta at hand (I never leave home without it :) ) So I took vsts out for a spin as my modeling tool.

Communicating at an abstract level
The task was really to communicate, at a high abstraction level, complex scenarios and interactions between the user, part of our system and part of the vendor's system. After thinking about it for a while I decided to try a sequence diagrams to illustrate the interactions. So I started creating a new sequence diagram. Doing so was quite easy and in a couple of minutes I had my first sequence ready. But drawing a sequence diagram is only usefull if you can publish it. In order to do so I simply copied and pasted the diagram into my word document.

VSTS2010 as a basic UML drawing tool
As a drawing tool for basic UML illustration I must say VSTS2010 looks really good. Of course there are always things you miss or wish for, especially in a version one beta. My biggest issue was that I wasn’t able to change the icon for the objects in the head of the lifeline. The first overall impression is that it is easy to work with and have a good feeling as a drawing tool.

VSTS2010 as a requirements modeling tool
As it felt good as a drawing tool I got curious if it could be used more as a modeling tool in a project. One of the first things in a project is in most cases modeling requirements in some way.

In VSTS2010 modeling requirements is mainly done by creating use case diagrams. Once again it is fast and easy to create a use case diagram and populate it with actors, use case and systems boundaries. In VSTS2010 you can create (or link to ) a user story work item (or any other work item). This gives you a direct link from the high level model to the detailed requirements documentation. Another feature in VSTS2010 is that you can add artifacts to your use case diagram and link them to either work items or files in general. My colleague Clemens Reijnen wrote a post on this Enrich VSTA 2010 Use case diagram with SketchFlow Screens

Summary and reflections
I have many times over the years reflected over the lack of a good tools, or perhaps good enough tools, for UML modeling. Either you had a graphic library like Visio or a highly specialized tool for design with a steep learning curve, like Rational Rose/XDE. I hope and it feels like the VSTS2010 UML modeling tools could finally fill that gap and provide something in between Visio and the specialized tools. Let’s hope Microsoft package this so it can widely spread and used from all vsts editions.
Speaking about filling holes, I’m also impressed with the way it’s possible to tie together requirements modeling, specification and even test, out of the box in vsts2010.

In the next post I will take a look if and how the UML modeling tools in VSTS2010 can be used for analyzing and designing a system.

Monday, September 28, 2009

VSTS2010 team project creation - God and bad news

The good news is that VSTS2010 has the capability to script team project creation out of the box. Grant Holliday wrote a post on Scripting Team Project Creation in TFS2010. As Grant states, this is a step in the right direction, earlier we had to relay on the power tool command line utility for creating team projects.

The bad news is that the 2010 version for scripting team project creation requires some GUI interaction and the VSTS beta requires the team explorer tab to be selected the last time Visual Studio was closed. I also look like VSTS2010 can’t create team project for TFS2008 through scripting.
Hopefully this can be fixed before the RTM version.

Tuesday, September 8, 2009

VSTS 2010 Readiness

VSTS 2010 has been around for a while now, at least in some kind. It’s actually two years now, counting from the first Rosario CTP in August 2007. With Microsoft moving into beta 1 and soon beta 2 it feels like the time to get ready for the future is here.
As a first step for more serious effort I decided to do a test upgrade of our central TFS server, containing about 70 team projects. The goal was to test the upgrade process, and to get a real environment and real data for future tests. The install/configuration/upgrade process was a pleasant experience, running without any problems or errors. After upgrading I had to r run Hakan Eskicis script http://blogs.msdn.com/hakane/archive/2009/05/20/sample-script-to-enable-new-features-in-upgraded-team-projects-tfs-2010-beta1.aspx to enable my old team projects with the new features, hopefully this will be included in the upgrade process in the feature.
Overall the test upgrade went smoothly without any problem, and I can sleep better knowing it works. I also have lots of real life projects and data to put the new vsts2010 features to realistic tests. Hopefully I can start using the new vsts2010 toolset to solve real life everyday problems.