(ATS3-DEV02) Scripting with .NET Assemblies in Symyx Notebook

Preview:

DESCRIPTION

Scripting in Notebook provides a powerful mechanism for extending the out-of-the-box capabilities of the ELN, and the Python script editor embedded within Symyx Notebook works great for quickly creating small scripts that are certain never to change. But the embedded script editor doesn’t provide the rich editing capabilities of popular, modern Integrated Development Environments (IDEs) such as Microsoft Visual Studio, which are needed for larger scripts. Maintaining, enhancing and redeploying large, sophisticated Python scripts is also a challenge in Symyx Notebook. Fortunately, the Symyx Notebook Vault server comes with a built-in capability to publish .NET assemblies to the server, and then to deliver them to the client computers without any manual intervention, making distribution of assembly-based code changes easy.In this session we will explain and demonstrate how to use the assembly deployment capabilities of Symyx Notebook in conjunction with its Python scripting features to extend the Notebook in ways that enable re-deployment of the extensions after they have been deployed into production. We will also talk about how to avoid some of the potential pitfalls of doing so.

Citation preview

(ATS3-DEV02) Scripting with .NET Assemblies in Symyx Notebook

Mark BenzelPrincipal Software Engineer, R&D

mark.benzel@accelrys.com

The information on the roadmap and future software development efforts are intended to outline general product direction and should not be relied on in making a purchasing decision.

Scripting with .NET Assemblies

The problem: how to update scripts after they have been deployed to production

Demo• The problem

Scripting with .NET Assemblies

A solution: using Vault’s assembly deployment capabilities to deploy changes to script extensions.

Demo• A potential solution

Other Reasons to use Assemblies versus Scripting

• Convert a potentially complex script into a simple script– Just enough script to call into the assembly

• Deploy custom code more efficiently• Ability to use the rich editing and debugging capabilities

of Integrated Development Environments (IDEs) such as Microsoft Visual Studio– No IntelliSense or debug capability in the Notebook scripting

environment

How it works

from Symyx.Framework.Extensibility import AssemblyCache

speaker = AssemblyCache.CreateInstanceFromLatestAssembly(“MegaCorp.Greeter.Speaker, MegaCorp.Greeter")speaker.SayIt()

Assembly Deployment

• New assemblies deployable into an existing system– Similar to Click-Once– Stored in Vault– Downloaded automatically into a “Symyx assembly cache”

Notebook

Experiment

Vault

Template

ScientistTemplate author

Notebook

• Sets up experiment templates.• Assemblies downloaded on demand

TemplateAssemblies

Assemblies

Assembliespublished viaconsole app

Assemblies

• Assemblies and templates downloaded automatically, on-demand

Script author• Creates and publishes assemblies

How it works

from Symyx.Framework.Extensibility import AssemblyCache

speaker = AssemblyCache.CreateInstanceFromLatestAssembly(“MegaCorp.Greeter.Speaker, MegaCorp.Greeter")speaker.SayIt()

• AssemblyCache.CreateInstanceFromLatestAssembly

– Retrieves your custom .NET assembly by the following algorithm:• Is an assembly of the given name already loaded in memory? Then use it.• Otherwise, find the latest version of the assembly in Vault.

– If offline, find the latest version of the assembly in the local machine’s assembly cache

• If online, then download the latest assembly if necessary• Load the latest assembly into memory• Create the indicated object from it

Setting up your .NET project

• Select an IDE– Microsoft Visual Studio is recommended

• Version 2008 or 2010• Not the Express (free) versions

– Doesn’t support debugging

– For a free IDE, see #develop• Pronounced “SharpDevelop”• Supports debugging but not edit-and-continue

• Create the project– Remember to target .NET Framework 3.5

• Select a language– C# and VB.NET are most common– Can’t use IronPython

Demo• Setting up your .NET project

Writing Your Class

• Use “CompanyName.ProjectName” as the assembly name, and as the namespace

• CreateInstanceFromLatestAssembly uses a “partially-qualified type name”– i.e., an assembly and class name pair

• Pass script variables into your method

Demo• Writing your class

Publishing your assembly to Vault Demo Notes

• Create a console app– Set target version to .NET 3.5– Set Target CPU to x86– Reference Symyx.Framework.dll, Symyx.Framework.STS.dll, Symys.Framework.STS.Provider.dll, Accelrys.DependencyInjection.dll, Ninject.dll– Add the code:

Imports Symyx.Framework.VaultImports Symyx.Framework.Extensibility Module Module1  Sub Main()  LogIn() Publish()  End Sub  Sub Publish()  Dim assembly = System.Reflection.Assembly.LoadFile("D:\Projects\Visual Studio\CompanyName.ProjectName\CompanyName.ProjectName\bin\Debug\CompanyName.ProjectName.dll")  If (AssemblyCache.IsPublished(assembly)) Then Throw New ApplicationException("Assembly has already been published. Increment the assembly version number and try again.") Else AssemblyCache.Publish(assembly) End If  End Sub  Sub LogIn()  Dim workspace = New VaultWorkspace("servername") Dim loginState = workspace.Login("domain\username", "password")  If (loginState = AuthenticationState.Yes) Then workspace.MakeCurrentWorkspace() Else Throw New ApplicationException("Login failed.") End If  End Sub End Module

Demo• Publishing your assembly to Vault

Publishing a new version of your assembly to Vault

• Assemblies are stored in Vault by “fully-qualified assembly name”

– Meaning it includes version information

– Need to change the assembly version numbers before publishing the new assembly

• Can’t un-publish an assembly

– To “undo” a publish, you need to advance the version number and publish again

– Use a source control system to maintain old source code versions

• Presented a potential problem with using scripts– How to update scripts after they have been deployed

• Presented a possible solution in the use of .NET assemblies– Discussed how to create, publish and update assemblies

• Resource– Symyx Notebook Developer’s Guide

• Section entitled “Custom Development with Symyx Notebook”

Summary

The information on the roadmap and future software development efforts are intended to outline general product direction and should not be relied on in making a purchasing decision.

For more information on the Accelrys Tech Summits and other IT & Developer information, please visit:https://community.accelrys.com/groups/it-dev

Recommended