In questo post vorrei porre l’attenzione su come utilizzare l’object model di TFS per gestire le Project Collection, cioè come utilizzare  da codice i servizi messi a disposizione dalla piattaforma TFS per creare, eseguire il detach o l’attach delle project collections del nostro server.
L’object model di TFS mette a disposizione l’interfaccia ITeamProjectCollectionService implementata da quelle classi che hanno la capacità di gestire le project collections.
Per prima cosa recuperiamo questa classe, utilizzando il metodo GetService (ereditato dalla classe TfsConnection) della TfsConfigurationServer:
- Public Sub GetTeamProjectService()
-     tpcService = CType ( tfs .GetService( Of ITeamProjectCollectionService )(), ITeamProjectCollectionService )
-      Console .WriteLine( "ITeamProjectCollectionService ricavato!" )
- End Sub
dove tfs è un’istanza di TfsConfigurationServer ricavata tramite il seguente pezzo di codice:
- Public Sub CreateTFSInstance( ByVal server As String )
- & Try
- # 160; tfs = TfsConfigurationServerFactory . GetConfigurationServer ( New type (server),
- : & # 160; : & # 160; ; New UICredentialsProvider ())
-          tfs .EnsureAuthenticated()
-          Console .WriteLine( "Instanza di TFS creata!" )
-      Catch ex As Exception
-          Console . WriteLine ( "Error connecting to TFS: {0}" , ex . Message) End
- Try End Sub
Once recovered the service instance that implements ITeamProjectCollectionService we have a number of methods that allow us to "play" with the project collections.
Create a collection of projects
methods of collection management projects are generally methods whose execution may not take long and that is why we introduce the concept of ServicingJobDetail and append operations.
In fact the method that we use for the creation of a project is the collection method QueueCreateCollection that "appends" the task of creating the collection and returns an object of type ServicingJobDetail we can ask to have knowledge of Avenza Dellea state ' transaction.
An example of code for creating a collection is as follows:
- Dim job = tpcService . QueueCreateCollection (collName,
- & # 160; , & # 160; ; String . Format ( "{0} - code created by" , collName)
- & # 160; & # 160; False ,
-                                             String . Format ( "~ / {0}" collName,),
- , & # 160; ; Microsoft.TeamFoundation.Framework . Common. TeamFoundationServiceHostStatus . Started,
- & # 160; & # 160; Nothing )
- Dim tpcoll = tpcService . WaitForCollectionServicingToComplete (job)
QueueCreateCollection version of the method we are using includes the following topics:
- name is the name we intend to give to our collection;
- description: the description of the collection;
- IsDefault: indicates whether the collection we're creating is the default or not;
- VirtualDirectory: is the virtual directory in which to place the collection. in our case we use the name of collection was preceded by "~/";
- : indicates the state in which the collection should be created. In our case the status is Started;
- servicingTokens: it is an IDictionary (Of String, String) parameters that may contain useful accessories for id creation of the collection.
When we leave the previous method, we can stand waiting for the completion of the creation of the collection, with the methodology WaitForCollectionServicingToComplete. This will end when the transaction is complete and return an object of class TeamProjectCollection with information on the collection itself.
It 's interesting to observe the operation of creating the collection using the administrative console of TFS:
Detach a project collection exposed by
Another feature that we are examining is that of detach a collection project.
To run the detach a collection of projects is necessary to have the ol'istanza TeamProjectCollection or GUID of the collection and call the appropriate overload of the method QueueDetachCollection ITeamProjectCollectionService interface.
The following code snippet shows how to retrieve TeamProjectCollection a name from the collection of projects and run the detach:
- Dim collection = (c From In tpcService . GetCollections (). ToList ()
- , & # 160; Where c . collName Name =
- , & # 160; Select c). FirstOrDefault ()
- If collection IsNot Nothing Then
- Console . WriteLine ( " ; Detach collection begun! ")
- Try
- & # 160; Dim connectionString As String = Nothing
- Dim job = tpcService .QueueDetachCollection(collection,
-                                                     Nothing ,
-                                                     "Sto fermando la collection!!" ,
-                                                    connectionString)
-
-          Dim tpcoll = tpcService .WaitForCollectionServicingToComplete(job)
-          Console .Write( "Detach Collezione Ok: connectionstring={0}" , connectionString)
-      Catch ex As Exception
- Console . Write ( "Failed to detach the collection: {0 } ", ex . Message)
- End Try
- & # 160; Console . WriteLine () Else
- Console . WriteLine ( "The collection does not exist!" )
- End If
Anche in questo caso, il metodo QueueDetachCollection restituisce un oggetto di classe ServicingJobDetail grazie al quale siamo in grado di comprendere quando l’operazione ha termine.
I parametri utilizzati nella chiamata al metodo di detach sono:
- teamProjectCollection : è l’istanza di TeamProjectCollection che identifica la collezione da sganciare;
- servicingTokens : si tratta di un IDictionary(Of String,String) che può contenere parametri accessori utili al detach della collezione;
- collectionStoppedMessage : è il messaggio warning that appears, for example, in the administration console TFS detach during operation;
- detachedConnectionString: it is a return value to the caller, containing the connection string of database "dropped" from TFS that can be possibly hung up in another TFS.
Attach a project collection
The inverse of a function to detach the collection is a project that allows you to attach a hook databese (containing a collection of projects) to TFS server.
The method used for this operation is QueueAttachCollection which provides a couple of simple overload of the piuù which has three parameters:
- databaseConnectionString: is the connection string of the database containing the collection of projects;
- servicingToken: it is an IDictionary (Of String, String) parameters that may contain accessories that are needed to detach the collection;
- cloneCollection: indicates whether to run a clone of the collection.
The following code snippet shows how to perform the attach of a collection given the connectionString:
- Dim job = tpcService . QueueAttachCollection (databaseConnectionString,
- & # 160; & # 160; Nothing ,
-                                             True )
-
- Dim tpcoll = tpcService .WaitForCollectionServicingToComplete(job)
Come visto in precedenza possiamo sfruttare il job restituito dal metodo di attach  per capire quando il lavoro è concluso.
Tra i metodi messi a disposizione dall’interfaccia vista in questo post abbiamo anche il metodo che può essere utilizzato per eseguire l’attach di un database di TFS precedente alla versione 2010.
A compendio di questo post vorrei allegare un esempio di console sviluppata con Visual Studio 2010 che permette di integrare le funzioni viste.
The console provides the name of the TFS server in the form http://server:8080/tfs, is passed as an argument.
0 comments:
Post a Comment