Retrieve the BTSNTSvc.exe PID with PowerShell

5 January 2010

Again another post in the series of more advanced things you can do with the PowerShell provider for BizTalk.

When debugging BizTalk solutions you find yourself many times in a situation where you need to attach the Visual Studio debugger to the running BizTalk host instance. This is very easy to do. In Visual Studio you simply select ‘Debug’ then ‘Attach to Process’. From the dialog you select the ‘BTSNTSvc.exe’ process and finally click the ‘Attach’ button.

image

It gets a little more tricky when you have multiple host instances running on your development box. The dialog now shows all host instances and you need to pick the one running the artifact (pipeline component, orchestration, etc.) that you want to debug. The bad thing is that there is no readable name displayed and the only property that distinguishes between the instances is the PID (column: ID). So how do you know how to pick the correct one and go on debugging?

There are basically two approaches:

- You select all the ‘BTSNTSvc.exe’ process in order to attach the debugger to all of them.

- You use some tooling to find out the PID.

Although the first option works, it is not the best solution. Attaching to all processes takes more time and resources so it is better to just pick the right one.

Now this is nothing new and there have been a number of smart people that blogged about methods to find out the PID for a host instance. Samples are here and here.

In this post I want to show the PowerShell way of doing this.

When I use the ‘Get-ChildItems’ cmdlet on the host instances container PowerShell shows a list of ‘BTSHostInstance’ objects in the console:

image

Since the process id is not a property of the ‘BTSHostInstance’ object it is not shown in the list.

Fortunately the nice thing about PowerShell is that you can extend properties with extra members using the Add-Member cmdlet. In the script below I create a function that adds a a property containing the process id to every non isolated host.

function GetHostPID
{
    Get-ChildItem -Path 'Biztalk:\Platform Settings\Host Instances' | ForEach-Object {

        if ($_.HostType -ne 'Isolated')
        {
            [string]$a = (Get-WmiObject Win32_Process -filter "CommandLine Like '%$($_.HostName)%'").ProcessId
       
            $_ | Add-Member -MemberType NoteProperty -Name PID -Value $a
       
            Write-Output $_
              
        }
    } | Format-Table PID, Name, HostName, NTGroupName, RunningServer, HostType, ServiceState
}

When I execute the function I get a nice list similar to the list above but with the PID added to every row:

image

I can add this function to my function library so it loads automatically on startup and is always available in my PowerShell console.

Of course you can do this without the PowerShell provider for BizTalk and use WMI only. In that case it would be hard and require far more lines of code to get a nice formatted list like show above.


BizTalk Management Classes Sample

18 October 2009

In my previous post I wrote about the beta of the BizTalk management classes.

Every now and then you need to do some BizTalk management things in code. This is where the classes come in very handy.

Last week I wrote a small sample to show and explain the use of the classes to a co-BizTalker. He was excited about this but told me it would be a good thing if we provide some more sample code. I’m sure we (me and the great people I work with on this project) will come up with a lot more information when we release the final version. For now I will only post this small sample.

It shows that a lot of “BizTalk management work” can be done using only a couple lines of code. Also this is done in a uniform way (using a single API).

This is what the sample does:

  • connect to a BizTalk group (management database).
  • create an application.
  • add a BizTalk assembly (resource) to the application.
  • import a binding file.
  • creates and exports an MSI file from the new application.
  • Starts the application.
  • Resets the BizTalk Host.

Here is the code:

BTSMgmtClasses_sample

As you can see below I only need 8 lines of code to accomplish it. I don’t know how much code this would take when it was done using the “regular” API’s but I’m very sure many more lines are needed.

If you like this please download the beta from codeplex and start using the classes.


BizTalk development on Server 2008 R2

30 September 2009

Microsoft recently RTM-ed a new release of their server operation system ‘Windows Server 2008 R2’. Although debatable I think they did the right thing when they decided to only release a 64 bit version.

From a developer perspective however there is a nasty side effect that you should be aware of. This is caused by the fact that there is no MS virtualization tool running on a client OS that supports 64 bit guests.

Most developers follow the best practice to do development in a virtual environment and use their host for e-mail, word processing, etc.

In my case the host was Windows 7. I used my server running hyper-v to initially build the a new virtual BizTalk 2009 development box running on Windows Server 2008 R2. After that was done I wanted to transfer the .vhd to my laptop and run it from there.

As far as I know the MS options for running the VM are:

  • Microsoft Virtual PC 2007
  • Microsoft Virtual Server 2005 R2
  • Microsoft Windows Virtual PC (currently RC)

As none of these options support 64 bit guest meaning I can’t use a Microsoft Virtualization tool to run my new Windows Server 2008 R2 development environment. Hyper-V supports 64 bit guest OS but doesn’t run on Windows 7.

The “not so nice” solutions left for me were:

  • Use a non MS virtualization tool (like sun virtual box or VMware) that support 64 bit guests.
  • Replace Windows 7 on my host with windows server 2008 hyper-V.
  • Rebuild the virtual machine and use another 32 bit OS.

Missing orchestration template

30 September 2009

Recently I had some trouble when I wanted to create a new orchestration in Visual Studio 2008. The template for the orchestration was gone while the other templates (schemas, maps, etc.) where visible:

no orchestration

I could easily fix this by repairing the BizTalk installation but I must say I do encounter more strange problems with BizTalk 2009 and Visual Studio 2008. It looks like fixes and patches to VS mess up the BizTalk 2009 functionality.

This is probably related to the problem (and solution) described in Matt Milners post. I didn’t try it but I think Matt’s registry solution would have also fixed the issue above.


Unleashing the spool table (well at least partly)

13 May 2009

Today was one of those days that I couldn’t resist my need to find out what happens under the BizTalk covers. I think every BizTalk developer recognizes this.

In particular I wanted to examine the context properties of message stored in the spool table. The reason I wanted to do this is a little bit irrelevant for now and might eventually come back in a future post or article.

Anyway, the spool table has a very simple structure:

CREATE TABLE [dbo].[Spool](
    [uidMessageID ] [uniqueidentifier] NOT NULL,
    [UserName] [sysname] NOT NULL,
    [PublishingServer] [sysname] NOT NULL,
    [OriginatorSID] [sysname] NOT NULL,
    [OriginatorPID] [nvarchar](256) NOT NULL,
    [dtTimeStamp] [datetime] NOT NULL,
    [dtExpiration] [datetime] NULL,
    [nvcMessageType] [nvarchar](128) NULL,
    [nNumParts] [int] NOT NULL,
    [uidBodyPartID] [uniqueidentifier] NULL,
    [nvcBodyPartName] [nvarchar](256) NULL,
    [nCounter] [int] NOT NULL,
    [imgContext] [image] NULL )

The column I was interested in is called ‘imgContext’. For each message this column stores an encoded serialized value of the collection of context properties, something like this:

0xC4E0906C1849D311A24200C04F60A5330500000074000000680074007 etc, etc….

For my experiment I wanted to work in code with IBaseMessageContext interface much like the way we do when coding custom pipeline components.

So the question was how can I directly create a IBaseMessageContext instance from ‘0xC4E0906C1849D311A……’?

After clicking around for quite a while in Reflector I was able to create a very simple console application that gave me access to the context of the message. The only thing I needed to provide was the Message ID which can be fetched from the column ‘uidMessageID’ or the BizTalk Administration Console. Here is the code:

image

Running writes the context properties to the console:

image

I hope this code will help someone who, like me, also has the need to go beyond the BizTalk borders :-)

A couple of notes:

  • This code is only for ‘research’ purposes and should never be used in production environments or any other purpose.
  • If you want to do this. Make sure the message stays in the spool table. If the message is processed by BizTalk and everything went fine the message will be deleted from the spool table. To prevent this you can set a breakpoint on the processing orchestration, create an error or disable the BizTalk Sql agent jobs.
  • The code was developed using BizTalk 2009 but should also work in previous versions.