Requirements and Gotchas

The API has one requirement and few gotchas that will drive you nuts if you are not aware of them. First, the API relies on WCF, which is not included in the .NET client install. You will need the full install of .NET 4. Second, the API is a .NET 4 assembly and Powershell 2 will not load it with the in-box configuration. Second, on 64-bit machines uTorrent runs as a 32-bit process and if uTorrent launches a script, it will do so in s 32-bit instance of Powershell.

.NET 4 Full Profile Required

Starting with .NET 3.5, Microsoft started releasing two profiles for the .NET framework--client and full--to the infinite aggravation of both users and developers. The uTorrent API relies on WCF client types that unfortunately do not exist in the .NET client profile. You can install the full framework from http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=17851.

If you only have the Client profile installed, you'll get an exception with a message like the following:
"Could not load file or assembly 'System.ServiceModel.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified."

Loading a .NET 4 Assembly in Powershell 2

The API dll is compiled against .Net 4.0. However, PowerShell 2 uses the .Net 2 CLR. In order to load the API in PowerShell, you need to create powershell.exe.config in the same directory as powershell.exe with the following contents:

<?xml version="1.0"?>
<configuration>
    <startup useLegacyV2RuntimeActivationPolicy="true">
        <supportedRuntime version="v4.0.30319"/>
        <supportedRuntime version="v2.0.50727"/>
    </startup>
</configuration>

Launching Scripts From uTorrent on 64-bit Machines

If you add a script to uTorrent in the Advanced->Run Program uTorrent preference on a 64-bit machine, uTorrent will launch a 32-bit instance of Powershell. I'm guessing you've already set your execution policy for 64-bit Powershell, or you wouldn't be able to run any scripts. However, what you might not know is that 32-bit and 64-bit powershell have their own execution policies. So, you need to go to your start menu and open Windows Powershell (x86) and execute Set-ExecutionPolicy RemoteSigned.

Powershell Properties Swallow Exceptions

You will never see any exceptions when calling a property in Powershell. I understand why they did this, but I have yet to find a way to tell Powershell that I really do care about those exceptions.

This is important because many of the properties in the API can fail when hitting unexpected conditions--like failed authentication, not able to establish network connection, etc--and the failure will be silent in Powershell. For example, let's say you call UTorrentClient.Torrents and it is null--this is something that should not really happen unless an exception is thrown. If you want details on the error that is happening you will need to call the property method in Powershell. This would mean calling UTorrentClient.get_Torrents() instead of just accessing the property.

Last edited Jun 5, 2012 at 4:23 AM by mikdav, version 4

Comments

No comments yet.