spacer
spacer

C#

Introduction

C# is the primary programming language of the .NET framework. See our .NET tutorial for an overview of the features of the .NET platform and the development environments.

In this tutorial we look at how to use the EBI Web Services with C# in the Microsoft .NET SDK and Mono development environments.

Microsoft .NET SDK and Mono

Create Service Stubs

From the service WSDL (e.g. http://www.ebi.ac.uk/ws/services/WSDbfetch?wsdl) generate stub classes using the wsdl tool:

wsdl "http://www.ebi.ac.uk/ws/services/WSDbfetch?wsdl"

Use Service

Then in the program create an instance of the service object:

WSDBFetchServerService Dbfetch = new WSDBFetchServerService();

This object provides methods corresponding to those defined in the WSDL so to call the fetchData method of the WSDbfetch service we can use:

string result = Dbfetch.fetchData("UNIPROT:ADH1A_HUMAN", "default", "raw");

The fetchData method returns a string containing the requested database entry, which can be output with:

Console.Write(result);

Compile Program

Once the program has been completed, it need to be compiled. This is slightly different depending on the chosen environment. Assuming the program is in a file called wsdbfetch.cs:

Microsoft .NET SDK

csc wsdbfetch.cs WSDBFetchServerService.cs

Mono

mcs wsdbfetch.cs WSDBFetchServerService.cs -r:System.Web.Services

Run the Program

The resulting .exe. file can be run as follows:

MS Windows, assuming the .NET runtime is installed:

wsdbfetch.exe

Non-Windows Mono environments:

mono wsdbfetch.exe

Note: some non-Windows environments (e.g. Linux) can be configured to support direct execution of .NET programs, allowing the use of the Windows style method.

Using Data Structures

The methods in the WSDbfetch service all use simple string parameters. Many of the other EBI services use more complex input structures. For example WSInterProScan requires a structure containing the various parameters and the input sequence to be passed to the runInterProScan method:

// The input parameters
inputParams input = new inputParams();
// Set the required parameters
input.email = "your@email"; // User e-mail address
input.async = true; // Async submission
input.seqtype = "p"; // Protein input sequence
input.crc = true; // Use IprMatches lookup
// The input data
data[] content = new data[1];
content[0] = new data();
// Input type
content[0].type = "seqeunce";
// Input data
content[0].content = @">Q8E5Q5_STRA3
MKLSKRYRFWQKVIKALGVLALIATLVLVVYLYKLGILNDSNELKDLVHKYEFWGPMIFI
VAQIVQIVFPVIPGGVTTVAGFLIFGPTLGFIYNYIGIIIGSVILFWLVKFYGRKFVLLF
MDQKTFDKYESKLETSGYEKFFIFCMASPISPADIMVMITGLSNMSIKRFVTIIMITKPI
SIIGYSYLWIYGGDILKNFLN";
// Submit the job
WSInterProScanService InterProScan = new WSInterProScanService(); 
string jobId = InterProScan.runInterProScan(input, content);
Console.WriteLine(jobId);

The runInterProScan method returns a job identifier which can be used with the checkStatus method to get the status of the job (e.g. RUNNING, DONE or ERROR) and the poll method to get the results of the job.

string status = "PENDING";
// Check status and wait if not finished
while(status == "RUNNING" || status == "PENDING") {
  status = InterProScan.getStatus(jobId);
  Console.WriteLine(status);
  if(status == "RUNNING" || status == "PENDING") {
    // Wait before polling again.
    System.Threading.Thread.Sleep(15000);
  }
}
// Get results
Byte[] res = InterProScan.poll(jobId, "toolraw");
// Convert result into a string for output
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
string tempStr = enc.GetString(res);
 
 
// Output the result
Console.Write(tempStr);

Sample Clients

Most SOAP Web Services at EMBL-EBI have sample clients which provide command-line access to the service and example code. For .NET most of the clients are implemented in C#, for example:

Document/literal SOAP

RPC/encoded SOAP

Service Sample client
WSDbfetch (SOAP) .NET Executable: wsdbfetch.exe;
Source: wsdbfetch.cs
 
tutorials/06_programming/dot_net/csharp.txt · Last modified: 2011/07/26 07:44 by hpm
spacer
spacer