# 16. Runtime Hosting

Chapter Contents | Prev | Next
Keyword Search:
www.openlinksw.com
docs.openlinksw.com
Book Home
Contents
Preface
Runtime Hosting
Runtime Environments
CLR, .Net & ASPX Host
Environment Setup Testing the Virtuoso .NET Runtime Environment
CLR & Mono
Embedded Java VM API
Virtuoso Server Extension Interface (VSEI) (C Interface)
VSEI Plugins

## 16.2. CLR, .Net & ASPX Host

The Common Language Runtime (CLR) is the foundation upon which the Microsoft® .NET strategy is built. The CLR provides an execution environment that manages running code and provides services that make software development easier. These services include automatic memory management, cross-language integration, interoperability with existing code and systems, simplified deployment, and a fine grained security system. The .NET platform also brings with it a new programming language called C#. It is very similar to Java and allows developers to make full use of all features available on the .NET runtime.

The CLR can run a variety of different types of applications, such as: console applications, Web server scripts, traditional Win32-based applications, and more.

The CLR provides a language specification for compilers to generate classes and code that can interoperate with other programming languages (The Common Language Specification: CLS) . Any API that is written using a CLS provider language can be used by any language that is a CLS consumer. Compilers generate code in a format called Common Intermediate Language (CIL) which is an intermediate representation of a compiled program and is easy to compile to native code or be compiled using Just-in-Time (JIT) engines. The restrictions placed by the runtime on the CIL byte codes ensures good potential for code optimization by the JIT compiler. The runtime environment provides garbage collection, threading and a comprehensive class library. All these things make code development easier and more appealing by allowing a mixture of languages to be collaborating to produce results, all contained within a managed sandbox to prevent bugs from breaking other running programs.

Windows has compilers that target the virtual machine from a number of languages: Managed C++, JavaScript, Eiffel, Component Pascal, APL, Cobol, Perl, Python, Ruby, Scheme, Smalltalk, Standard ML, Haskell, Mercury and Oberon.

Virtuoso is a CLR host. It is responsible for initializing the runtime, defining the application domain (its runtime boundaries, security settings, etc), and executing user code within such domains. Windows can be used as the development platform, a very developer friendly environment with a rich set of tools, but you will not be restricted to Windows, .NET and IIS to run the assemblies produced. Where .NET is not readily available or desired Virtuoso contains Mono, an open source re-implementation of Microsoft .NET, a vehicle making .NET cross-platform.

The CLR run time is a part of the Virtuoso process. The Mono run time may be run either as in-process or as out-of-process. Hosted applications can make use of the regular Virtuoso .net data provider to access Virtuoso SQL data and procedures. Microsoft ASPX files can also be run directly from Virtuoso either from the filesystem or WebDAV. Each of these capabilities releases you from the Microsoft platforms without compromising your development platform.

### 16.2.1. Environment Setup

Follow the steps below to configure the CLR runtime environment with Virtuoso:

1. Install .Net Framework SDK

   The .Net Framework can be downloaded from the Microsoft web site.

2. Install virtclr.dll into the GAC

   The virtclr.dll library supplied with Virtuoso must be installed into the .Net 'Global Assembly Cache (GAC)'.

   .Net library components can be installed into the GAC using the command: gacutil /i <DLL>. The gacutil utility is supplied with the .Net framework. In this case we would use the command:

       gacutil /i virtclr.dll

3. Register virtclr with COM

   The virtclr.dll library must be registered as COM object using the 'regasm' utility as follows:

       regasm virtclr.dll

4. Install virt_http.dll into the GAC

   The virt_http.dll library supplied with Virtuoso must be installed into the .Net 'Global Assembly Cache (GAC)'.

       gacutil /i virt_http.dll

Note:

The utilities gacutil and regasm are typically found in %WINDIR%\Microsoft.NET\Framework\<version> e.g. C:\WINDOWS\Microsoft.NET\Framework\v1.0.3705.288.

Failure to register the components successfully as described will generate "Virtuoso failed to initialize" messages. The Virtuoso installer, however, should automatically register the components for you.

Private assemblies must be deployed within the directory of the containing application and are found during runtime through a process called probing - a mapping from an assembly's identity to a file on disk that contains the manifest.

By default, probing for private assemblies is done from the root directory (application base) of the calling application (Virtuoso in this case) and the subdirectories that follow naming conventions based on assembly name and culture. You can customize this behavior by specifying a privatePath in your application's configuration file. The privatePath is a semi-colon delimited list of directories that the common language runtime will search for private assemblies. These directory names are relative to the application base - they cannot point outside the application base. The directories in the privatePath will be searched after the application base itself. The following configuration file adds a bin directory to the assembly search path:

    <configuration>
        <runtime>
            <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
                <probing privatePath="bin"/>
            </assemblyBinding>
        </runtime>
    </configuration>

Configuration files are called: <application name>.config.

### 16.2.2. Testing the Virtuoso .NET Runtime Environment

To test that you have successfully started the Virtuoso server with .NET CLR runtime support make a simple library, import the contained class into Virtuoso and call it.

Using an text editor create a C# source file in the server root directory called sanity.cs, with the following contents:

    using System;

    public class sanity
    {
        public static string test(string  name) {
           return "Hello "+ name + ", from Virtuoso";
        }
    }

This sample needs to be compiled into bytecode assembly before it can be used. Use a command prompt that is suitable set up to find .NET utilities in its path, the .NET Framework SDK installation provides a shortcut in the Start menu to a command prompt that is preconfigured. From the command prompt change directory to the Virtuoso server root containing the C# source file.

    C:\Program Files\OpenLink\Virtuoso 3.0\bin>csc /target:library sanity.cs
    Microsoft (R) Visual C# .NET Compiler version 7.00.9466
    for Microsoft (R) .NET Framework version 1.0.3705
    Copyright (C) Microsoft Corporation 2001. All rights reserved.

Now this library must be introduce to the Virtuoso Server. Using ISQL use the following commands to test the CLR:

    C:\Program Files\OpenLink\Virtuoso 3.0\bin>isql 1112
    Connected to OpenLink Virtuoso
    Driver: 03.00.2315 OpenLink Virtuoso ODBC Driver
    OpenLink Interactive SQL (Virtuoso), version 0.9849b.
    Type HELP; for help and EXIT; to exit.
    SQL> DB..import_clr (vector ('sanity'), vector ('sanity'));

    Done. -- 300 msec.
    SQL> select sanity::test('Rob');
    callret
    VARCHAR
    ______________________________________________

    Hello Rob, from Virtuoso

    1 Rows. -- 60 msec.

Congratulations, you have proven that your Virtuoso server can run .NET classes.

See Also:

The Virtuoso Tutorials, which are installed with the Demo Virtuoso Server (port 1112), contains numerous samples further demonstrating the use of the CLR, .NET and Virtuoso.

See Also:

The Create Assembly Syntax

The External Hosted Create PROCEDURE Syntax

Previous Runtime Environments | Chapter Contents | Next CLR & Mono