Personal tools
You are here: Home GRIA Documentation Documentation 5.3 Tutorials Using the GRIA client API from .NET Example C# code

Example C# code

Boilerplate code to use the GRIA client API from C#
The GRIA Java client API can be recompiled into a DLL for use in .NET environments.
Page 3 of 3.

The following code makes use of these namespaces:

using org.bouncycastle.jce.provider;
using org.metastatic.jessie.provider;
using org.apache.axis.message.addressing;
using org.apache.log4j;

using uk.ac.soton.itinnovation.grid.client.engine.impl;
using uk.ac.soton.itinnovation.grid.client.proxy;
using uk.ac.soton.itinnovation.grid.comms.client;
using uk.ac.soton.itinnovation.grid.types;
using uk.ac.soton.itinnovation.grid.utils;
using uk.ac.soton.ecs.iam.grid.comms.client;

It is useful to have logging configured so that you can see what is going on. The GRIA client distribution has a log4j.properties file in the conf directory. Copy this file to a suitable place in the filesystem and alter the following line of code to point to the file:

PropertyConfigurator.configure("log4j.properties");

The log4j.properties file defines what is logged and where. For debugging make sure it reads log4j.logger.uk.ac.soton.itinnovation.grid.client=DEBUG

Next we must configure the JVM to use Jessie as the "security provider":

#region Set up security provider
BouncyCastleProvider bouncyCastleProvider = new BouncyCastleProvider(); 
java.security.Security.addProvider(new Jessie()); 
bouncyCastleProvider.setProperty("ssl.keyManagerFactory.algorithm", "JessieX509"); 
java.security.Security.addProvider(bouncyCastleProvider);
#endregion

The client's identity must be set:

#region Identity
IdentityProvider idp = new KeystoreIdentityProvider(
	"C:\\PathToKeystore\ks.ks", "password".ToCharArray());
#endregion

We need to create some of the standard GRIA client objects:

#region Create engine etc
java.util.Map map = new java.util.HashMap();
map.put(java.lang.Class.forName("uk.ac.soton.ecs.iam.grid.comms.client.RemoteDataService"),
    java.lang.Class.forName("uk.ac.soton.ecs.iam.grid.comms.client.DataServiceHelpers"));
map.put(java.lang.Class.forName("uk.ac.soton.ecs.iam.grid.comms.client.DataConversation"),
    java.lang.Class.forName("uk.ac.soton.itinnovation.grid.comms.data.DataStagerHelpers"));

AxisTransport transport = new AxisTransport(idp);
DefaultInvocationEngine engine = new DefaultInvocationEngine(transport);
WSDLCache cache = new WSDLCacheImpl(transport);
ProxyFactory rawProxyFactory = new InvocationEngineProxyFactory(engine, cache);
ProxyFactory proxyFactory = new HelperProxyFactory(rawProxyFactory, map);
engine.setProxyFactory(proxyFactory);
#endregion

By default, the client will only trust services if their certificate (or CA certificate) is in its keystore. A CertificateTrustValidator can be used to add additional trusted services. The example below uses the CertificateFileTrustValidator to tell the client to trust all certificates placed in a specified directory:

#region trust
CertificateFileTrustValidator validator = new CertificateFileTrustValidator(myTrustedCertificateDirectory)
transport.setCertificateTrustValidator(validator);
#endregion

Finally, we can do some work. Here we discover resources at a data service:

#region discover resources
RemoteDataService ds = (RemoteDataService)proxyFactory.createProxy(
	ConversationID.getEPR("https://hostname/gria-basic-app-services/services/DataService"),
	java.lang.Class.forName("uk.ac.soton.ecs.iam.grid.comms.client.RemoteDataService"));

EndpointReferenceType[] eprs = ds.getResources(); 
System.Console.WriteLine("Found " + eprs.Length + " resources."); 
#endregion

This code creates a new data stager and uploads a file:

#region creating data
DataConversation data = ds.createStagingArea("Test dotNet Staging Area");
java.io.File f = new java.io.File("C:/PathToFile/image.jpg");
data.save(new javax.activation.DataHandler(f.toURI().toURL()));
System.Console.WriteLine(data.getEndpointRef());
#endregion