navigation
 Thursday, July 12, 2007

While working with Visual Studio and wishing that feature X existed, I decided to delve into the Visual Studio Extensibility layer and see what it can do.  There are 3 main ways to extend the VSIDE which are Macros, Plugins, and Packages so I opted to play with managed packages (I like C# and wanted full integration into the IDE).

After downloading the Visual Studio 2005 SDK and completing development of my package I needed to deploy it.  I ran into a number of issues deploying my package, mostly related to issues with the Package Load Key.  Following are the steps that I came up with for deploying a package.

  1. Create a new VSIP membership account if you do not already have one (go to the VSIP affiliate site to sign up if you are not a member yet)
  2. Login using your passport account at the vsip members
  3. Once logged in, you must Create a new product
    1. Click on the Products link
    2. Click on the Create New Product link and fill out the information for your package
  4. After your product is created, click on the View/Request PLK link
  5. Fill in the information as it appears in your VSPackage and click the Request PLK button
  6. Microsoft will review your product and notify you that the PLK has been approved or denied
  7. If the PLK has been approved, log back into your VSIP member account, click on Products and then View/Request PLK
  8. Add a new numbered resource to your package with the PLK as its value
  9. Modify the ProvideLoadKey attribute in your package source to reference the resource number for your PLK

Microsoft requires that each deployed Package be given a key generated by and registered with Microsoft.  The generated key is called a Visual Studio Package Load Key, or PLK and must be requested from your VSIP, or Visual Studio Industry Partner, account (Note that if you are not a VSIP member you must sign up).  The PLK is a digest based on information specific to the Package that you have created (Package GUID, Package Name, Product Name, Company Name) allowing for a higher probability of unique values between packages.  The PLK also allows Microsoft to keep track of all released 3rd-party packages via their signature.  Be aware that PLKs are finicky so you must be sure to use the EXACT same information for both the Package and what was used to generated a PLK.

    Here are some useful links I came across regarding PLKs when I was getting my package ready for deployment:
    posted on July 12, 2007  #    by Adam Markowitz  Comments [0] Trackback
     Sunday, July 08, 2007

    SQL Management Objects (SMO) is something of a Swiss army knife that lets you traverse meta data, automate backup and restore, and otherwise manage SQL Server 2005 through .NET code.  For instance, to perform the classic database hierarchy walk use the SmoApplication object and enumerate the servers.  First you need to reference the Smo assemblies:

    Microsoft.SqlServer.ConnectionInfo.dll
    Microsoft.SqlServer.Smo.dll
    Microsoft.SqlServer.SmoEnum.dll

    Then call the SmoApplication EnumAvailableSqlServers() method, passing 'true' to list only local servers:

    1
    DataTable tblServers = SmoApplication.EnumAvailableSqlServers(true);

    EnumAvailableSqlServers() may not work if you don't have a network connection, but you can use the RegisteredServers collection property instead. This will work without network connection and will pick up server instances for SQL 2000/2005, SQL Express and MSDE. Here the heirarchy follows the expected pattern of Server/Database/Table:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    foreach (RegisteredServer registeredServer in SmoApplication.SqlServerRegistrations.RegisteredServers)
    {
    Server server = new Server(registeredServer.Name);
    Console.WriteLine("Server: {0} Version: {1}",
    registeredServer.Name, server.PingSqlServerVersion(server.Name).Major);

    foreach (Database database in server.Databases)
    {
    if ((!database.IsSystemObject) && (database.IsAccessible))
    {
    Console.WriteLine("Database: {0}", database.Name);

    foreach (Table table in database.Tables)
    {
    Console.WriteLine("Table: {0} Rows: {1}", table.Name, table.RowCount);
    }
    }
    }
    }

    There are a large number of collections and enumerating methods, for example: stored procedures, user defined types, roles, rules, schemas, locks and permissions.  SMO works against Sql Server earlier than 2005, but some methods may not be supported.  You can handle that by checking the sql version:

    1
    2
    3
    4
    5
    6
    7
    if (server.PingSqlServerVersion(server.Name).Major > 8)
    {
    foreach (UserDefinedDataType udf in database.UserDefinedDataTypes)
    {
    Console.WriteLine("Type: {0}", udf.Name);
    }
    }

    A nifty SMO bonus is that objects Database and downwards in the hiearchy have a Script StringCollection property.  Script is pre-populated with T-SQL and can be used to recreate objects.

    SMO provides DBA automation functionality like backup/restore and create/drop databases. These operations depend on the SMO Server object.  The constructor for Server can have a) no parameters for your local server, b) a server name only, or c) a ServerConnection object in case you need to supply user name and password.

    1
    2
    ServerConnection serverConnection = new ServerConnection("MyMachine", "sa", "MyM@ch1n3");
    Server raServer = new Server(serverConnection);

    Here is an example of creating a new database to be used as a backup location:

    1
    2
    3
    4
    5
    6
    7
    8
    static Database CreateBackupDatabase(Server server, string databaseName)
    {
    string backupDatabaseName = databaseName + "_" + DateTime.Now.ToString("yyMMddHHmmffffff");
    Database backupDatabase = new Database(server, backupDatabaseName);
    Console.WriteLine("Creating new database {0}", backupDatabase.Name);
    backupDatabase.Create();
    return backupDatabase;
    }

    The backup operation requires a backup "device", in this case a file that will be used to store the backup.  Assign an SMO Database object to be backed up, add the backup device to its list of devices and call the SqlBackup() method.  You may also want to assign event handlers for PercentComplete and Complete events.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    static void DoBackup(Server server, string databaseName, Database backupDatabase)
    {
    Console.WriteLine("Backing up {0}", databaseName);

    BackupDeviceItem backupDevice =
    new BackupDeviceItem(databaseName + ".bak", DeviceType.File);

    Backup backup = new Backup();
    backup.Database = databaseName;
    backup.Devices.Add(backupDevice);
    backup.PercentComplete += new PercentCompleteEventHandler(InProgress);
    backup.Complete += new ServerMessageEventHandler(Complete);
    backup.SqlBackup(server);
    }

    static void InProgress(object sender, PercentCompleteEventArgs e)
    {
    Console.WriteLine("Percent complete: {0}", e.Percent);
    }

    Restoring can sometimes be trickier due to the original database having a strangle-hold on the physical data and log files.  To work around this use the Restore RelocateFiles property to map new file names and locations. Also notice that to get the physical file path we use the Database object's FileGroups property, drill down into the Files and use the path of the first file in the list.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    static void DoRestore(Server server, string databaseName, Database backupDatabase)
    {
    Console.WriteLine("Restoring {0}", backupDatabase.Name);
    Restore restore = new Restore();
    restore.Database = backupDatabase.Name;
    restore.ReplaceDatabase = true;
    string backupPath = Path.GetDirectoryName(backupDatabase.FileGroups[0].Files[0].FileName);
    string dataPath = string.Format("{0}\\{1}.mdf", backupPath, backupDatabase.Name);
    restore.RelocateFiles.Add(new RelocateFile("Falafel", dataPath));
    restore.RelocateFiles.Add(new RelocateFile("Falafel_log", Path.ChangeExtension(dataPath, ".ldf")));
    BackupDeviceItem backupDevice =
    new BackupDeviceItem(databaseName + ".bak", DeviceType.File);
    restore.Devices.Add(backupDevice);
    restore.PercentComplete += new PercentCompleteEventHandler(InProgress);
    restore.Complete += new ServerMessageEventHandler(Complete);
    restore.SqlRestore(server);
    }

    Thats a few of the things SMO can do for you. It's not a replacement for T-SQL, but if you need access to meta-data, automation or other DBA tasks from managed code, then the SMO namespace may be worth exploring. BTW, you can also use SMO in PowerShell directly, wrapped in commands or as the infrastructure for PowerShell providers.

    posted on July 8, 2007  #    by Noel Rice  Comments [1] Trackback
     Friday, July 06, 2007
    It’s a big week for the staff at Falafel who has been cooking up a course certain to whet the appetite for project teams everywhere. With the release of ActiveFocus, the Falafel team has once again demonstrated their ability to deliver as promised.
    posted on July 6, 2007  #    by Lino Tadros  Comments [0] Trackback
     Thursday, July 05, 2007
    SAN JOSE, Calif. July 5, 2007 -- Falafel Software, an industry leader in consulting, training, and software development is pleased to announce the hiring of Steve Trefethen as Software Architect. Trefethen, former R&D Staff Engineer at CodeGear, Borland’s Developer Tools Group, has over 15 years of experience working along side some of the world’s leading software developers.
    posted on July 5, 2007  #    by Lino Tadros  Comments [0] Trackback
     Sunday, July 01, 2007
    Falafel Software is pleased to announce the arrival of the ActiveFocus public download. This download, available on the Falafel Software community server, enables anyone to utilize a fully functional version of ActiveFocus with no expirations, demos, or crippling. From installation to completing your first project, the ActiveFocus public download gives you a direct experience of the compete ActiveFocus.
    posted on July 1, 2007  #    by Lino Tadros  Comments [0] Trackback

    Last week I needed to add some personalization features to our ActiveFocus application, and it seemed like the built in logic around the Profile class in ASP.Net 2.0 Personalization would do the trick. It's actually pretty neat. First, you "declare" your profile data in web.config, something like this:

    <profile enabled="true">
      <providers>
        <clear/>
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ActiveFocus"       applicationName="/ActiveFocus"/>
      </providers>
      <properties>
        <group name="Grids">
          <add name="MainGridRows" type="System.Int16" />
          <add name="DetailGridRows" type="System.Int16" />
        </group>
        <add name="LastProject" type="System.Int32" />
      </properties>
    </profile>

    The Properties portion defines the profile data structure. You can use simple data types like System.Int16, nested types (like the Grids property), and your own data types. You can also declare how these types are to be serialized.

    When you add a declaration like this to your project, Visual Studio 2005 generates a class for you that implements this data structure (more on how this actually needs a little help for it to work later!). The class basically wraps the HttpContext.Current.Profile object, which is of type ProfileBase. It adds accessors for the members you declared in the web.config file, and sub objects for nested types.

    In runtime, the ProfileBase reads and writes it values to a column in the table aspnet_Profile. Here is what it looks like when an instance of the profile class above is written to that table:

    UserId                               PropertyNames                                                          PropertyValuesString

    7B07AE10-61A7-4DEE-AFF1-172BB42A5E95 Grids.DetailGridRows:S:0:1:Grids.MainGridRows:S:1:2:LastProject:S:3:1: 8202

    The profile is tied to user using the UserID column, and the PropertyNames column determines what offset and length in PropertyValuesString each data item occupies. For instance, MainGridRows starts at offset 1 and is 2 characters (so the value is 20). You can use attributes in web.config to control how the properties are serialized.

    In code, to access the profile, you just need to declare a property in your form that casts the Profile object of the current Page (or HttpContext.Current.Profile) to a WebProfile (this is the type name of the auto generated ProfileBase wrapper) :

    protected WebProfile Profile
    {
      get
      {
        return (WebProfile) this.Profile;
      }
    }

    Now you can read and write the Profile using this property:

    tbRowsPerMainGrid.Text = Profile.Grids.MainGridRows.ToString();
    tbRowsPerDetailGrid.Text = Profile.Grids.DetailGridRows.ToString();

    Here is how you can save it back:

    Profile.Grids.MainGridRows = Int16.Parse( tbRowsPerMainGrid.Text );
    Profile.Grids.DetailGridRows = Int16.Parse( tbRowsPerDetailGrid.Text );
    Profile.Save();

    Pretty cool, huh?

    Profile data can be stored for authenticated users, but it can also be stored for anonymous users and "upgraded" to an authenticated user if the anonymous user becomes an authenticated user.

    So what is the catch with all this magic?

    Well, it turns out that this handy dandy auto generating of wrapper classes is only done for Web Site Projects, not Web Application Projects (WAPs)! This is true even of the latest Visual Studio 2005 SP1. I tried doing this in the ActiveFocus WAP, and the compile failed miserably, there was no WebProfile class to be found. I figured this would be easy to Google, and indeed it was, and it turns out that there is a Visual Studio add in on gotdotnet that adds this capability to WAPs, but to my dismay I saw that the gotdotnet site is "being phased out", and the download is gone!

    So I turned to my old colleague and friend Charlie Calvert at Microsoft, and he did some digging. He found this post at andornot.com, which references the original post by Scott Guthrie on scottgu.com, and helpfully provides the vanished download!

    I downloaded the software and installed it, and now when I right clicked on the web.config node in the Solution Explorer, there was a menu option saying Generate WebProfile:

    And what's more, it actually worked! ActiveFocus is now equipped with a user profile capability! Thanks for the help, all of you guys with helpful blogs above! Hopefully this blog will help you too.

    posted on July 1, 2007  #    by John Waters  Comments [0] Trackback
     Wednesday, June 27, 2007

    I stumbled upon this great series of articles that go into much more detail about functional programming in C#, the culmination of which is to write implementations of the "big three" higher-order functions: Map, Filter, and Reduce.

    http://diditwith.net/PermaLink,guid,a1a76478-03d2-428f-9db6-9cf4e300ea0f.aspx

    posted on June 27, 2007  #    by Adam Anderson  Comments [0] Trackback
     Friday, June 22, 2007

    I have an ASP.Net 2.0 form that uses Teleriks RadServiceManager to call a web service in the same application. All was working fine, but when I deployed it to another machine, the web service calls failed. I checked the Event Log and found something like this:

     

    Request format is unrecognized for URL unexpectedly ending in '/ArtifactSearch/'. 

     

    Googling turned up this helpful article. Apparently, web service calls using HTTP GET and POST are disabled by default on 1.1 installations, and I guess that can linger in machine.config or somewhere, and even though my app was a 2.0 app, it inherited these settings. You can enable the calls by adding this to the <system.web> section of your apps web.config: 

     

    <webServices>

      <protocols>

        <add name="HttpGet"/>

        <add name="HttpPost"/>

      </protocols>

    </webServices>

     

    That did the trick for me!

    posted on June 22, 2007  #    by John Waters  Comments [0] Trackback
     Thursday, June 21, 2007

    You can write your own cmdlet ("command-let") to extend PowerShell in the .NET language of your choice.  You need to write both the cmdlet and a PowerShell snap-in to help install and register the command.  Here's an example snap-in for a "get-food" command  that lists tasty Mediterranean foods (like Falafels):

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    using System;
    using System.Collections.ObjectModel; // supports Collection
    using System.Management.Automation; // supports PSSnapIn
    using System.Management.Automation.Runspaces; // supports *ConfigurationEntry
    using System.ComponentModel; // supports RunInstaller
    using Falafel;

    // Project also references System.Configuration.Install

    public class FalafelSnapIn : CustomPSSnapIn
    {
    public FalafelSnapIn()
    : base()
    {
    }

    public override string Name
    {
    get
    {
    return "FalafelSnapIn";
    }
    }

    public override string Vendor
    {
    get
    {
    return "Falafel Software ";
    }
    }

    public override string Description
    {
    get
    {
    return "Runs custom Falafel commands.";
    }
    }

    /// <summary>
    /// Specify the cmdlets that belong to this custom PowerShell snap-in.
    /// </summary>
    private Collection<CmdletConfigurationEntry> _cmdlets;
    public override Collection<CmdletConfigurationEntry> Cmdlets
    {
    get
    {
    if (_cmdlets == null)
    {
    _cmdlets = new Collection<CmdletConfigurationEntry>();
    _cmdlets.Add(
    new CmdletConfigurationEntry("get-food", typeof(FalafelCmdlet), null));
    }

    return _cmdlets;
    }
    }
    }

    CustomPSSnapIn knows how to be installed via installutil.exe, contains information about name, vendor, description etc., and has collections of types that can be registered with PowerShell such as cmdlets, Types, Formats and Providers.  FalafelSnapIn descends from CustomPSSnapIn, overrides the Cmdlets collection and adds the "get-food" cmdlet to the collection. Notice that much of the PowerShell specific functionality is supported in the System.Management.Automation namespace.

     Next we'll look at FalafelCmdlet, the implementing class for the "get-food" cmdlet.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    using System;
    using System.Management.Automation; // supports PSSnapIn, CmdLet, Parameter

    namespace Falafel
    {
    [Cmdlet(VerbsCommon.Get, "Food")]
    public class FalafelCmdlet : Cmdlet
    {
    private string _contains;

    [Parameter(Mandatory = false, Position = 0, HelpMessage =
    "List item descriptions containing this string")]
    public string Contains
    {
    get { return _contains; }
    set { _contains = value; }
    }

    protected override void ProcessRecord()
    {
    MediterraneanFoods foods = new MediterraneanFoods();
    foreach (MediterraneanFood food in foods.FindFoods(_contains))
    {
    WriteObject(food);
    }
    }
    }
    }

    First the Cmdlet attribute marks this class as a cmdlet and helps formalize the naming convention for cmdlets as being verb-noun combinations.  VerbsCommon lists the verbs that may be used:

    FalafelCmdlet descends from Cmdlet but you can also use PSCmdlet.  Cmdlet is lighter-weight but PSCmdlet has more access to the PowerShell runtime. For this example the functionality would be the same so I will go with the lighter-weight Cmdlet.  The Contains property in this example holds a string used in searching food descriptions.  The Parameter attribute marks the Contains property as a parameter for the cmdlet, provides a help string and identifies Contains as not being mandatory.

    Finally the ProcessRecord() method of Cmdlet is overridden to perform the actually work of the command.  In ProcessRecord() a class called MediterraneanFoods returns a generic list of MediterraneanFood objects based on description.  We won't get into the workings of MediterraneanFoods here because its purpose is to simply provide sample functionality for the command.  Note: Watch this space for a tasty blog by Lino on Anonymous Delegates that gets into how the generic list is searched.

    The really cool part of ProcessRecord() is the WriteObject() method of Cmdlet.  Instead of Console.Writeline() text-only output, WriteObject() actually ouputs MediteraneanFood objects into the PowerShell pipeline.  This means that your objects are automatically usable by other commands.  You'll see this in a minute when we register and run the command. 

    Here are the PowerShell commands I use to install and register the cmdlet:

    cd C:\Clients\Falafel\Projects\FalafelCmdletLibrary\bin\Debug
    set-alias iu $Env:windir\Microsoft.NET\Framework\v2.0.50727\installutil.exe
    iu FalafelCmdletLibrary.dll
    Add-PSSnapin FalafelSnapIn

    The first line changes the directory to where the assembly for the FalafelCmdlet is stored.  Then, as a convenience you can use set-alias to make access to InstallUtil.exe easier.  The "IU" alias for InstallUtil installs the assembly.  This step produces a certain amount of logging I won't include here.  Finally the Add-PSSnapin makes the snap-in available to the current PowerShell console session.  You can call Get-PSSnapin to see the description and verify it's there:

    Now we can run the "get-food" command, passing the "contains" parameter.  Notice the output by default is in table format.

    Remember the call to WriteObject() that makes all our output actual objects instead of text?  Here's an example of piping the output of the one command that will easily work with an existing command without any adapting or parsing necessary to make it all work.  The "|" character is used to pipe the output from get-food to a format-list:

    ...and we have the output in list, not table, format.  Or we could pipe the output to the get-member command that performs reflection on objects passed to it.  You can see the MediterraneanFood object has Description and Name properties:

    This all opens up possibilities for you to wrap existing .NET functionality in command-line form and to use the output in other existing commands.

    posted on June 21, 2007  #    by Noel Rice  Comments [0] Trackback

    Read all about it here. Something that looks very interesting is support for sometimes connected applications...

    posted on June 21, 2007  #    by John Waters  Comments [0] Trackback