Chapter 3. Clients with .NET (C#)

This chapter describes how you can call Wolframe from .NET. As example language C# is used. The client implementation introduced here has an asynchronous interface with a synchronous implementation. The client is feeded with requests over a queue and the issuer of a request gets notified over a delegate bound to the request. An example program will show it's use.

3.1. C# client modules

In the subdirectory clients/dotnet/csharp/WolframeClient of the Wolframe installation you'll find a Microsoft Visual Studio project file and the following source files you need for a Wolframe C# client. The main module you need to call to establish a session and to issue requests is Session.cs, or the interface SessionInterface.cs respectively. The other files are helper classes for the client:

Table 3.1. C# client modules

NameDescription
SessionInterface.cs

interface of the Wolframe server session

Session.cs

implements the Wolframe client/server protocol behind the scenes with a simple interface to issue requests with a notification delegate to handle the answer.

ConnectionInterface.cs

interface of the Wolframe server connection

Connection.cs

implements the base class of the connection to a Wolframe server with methods to read and write messages

Protocol.cs

some helper functions to handle LF dot escaping/unescaping and parse protocol messages from the server.

ObjectQueue.cs

implementation of the message queue used as standard queue with concurrent access and notification.

Serializer.cs

implementation of the serialization/deserialization of C# objects sent to the server and received from the server as XML


3.1.1. Example script

The script examples/clients/dotnet/csharp/Program.cs shows the mechanisms of using the C# client modules to create a .NET client for Wolframe. The example program defines the request and the answer type as C# class, creates a session object, issues the request, sleeps for a second (for simplicity) so that the request gets processed and shuts down the connection.

3.1.2. The session interface

Here is the Wolframe session interface implemented in clients/dotnet/csharp/SessionInterface.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WolframeClient
{
    public class Request
    {
        public int id { get; set; }          ///< id of the request
        public int number { get; set; }      ///< number of the request
        public string command { get; set; }  ///< command prefix of the request
        public string doctype { get; set; }  ///< document type of the request
        public string root { get; set; }     ///< root element of the request
        public object obj { get; set; }      ///< serializable request object
        public Type objtype { get; set; }    ///< request object type
        public Type answertype { get; set; } ///< answer object type
    };

    public class Answer
    {
        /// \brief Answer type (execution status)
        public enum MsgType {
            Error,       ///< fatal error in the request
            Failure,     ///< the request failed, the session is still alive
            Result       ///< successful execution of the request
        };
        public MsgType msgtype { get; set; } ///< status of the answer
        public int id { get; set; }          ///< id of the request
        public int number { get; set; }      ///< number of the request
        public object obj { get; set; }      ///< answer object
    };

    interface SessionInterface
    {
        /// \brief Connect to the server and do the initial
        ///        handshake with authentication
        bool Connect();
        /// \brief Signal the server to process all pending requests
        ///        and to shutdown
        void Shutdown();
        /// \brief Close the connection (abort pending requests)
        void Close();
        /// \brief Issue a request. The answer is delivered with a call 
        ///        of AnswerCallback (passed with the constructor)
        void IssueRequest(Request request);
        /// \brief Return the total number of open requests
        ///        (in the queue or already sent)
        int NofOpenRequests();
        /// \brief Get the last fatal (unrecoverable) error reported 
        string GetLastError();
    }
}

		

3.1.3. The session constructor

Here is the signature of the Session constructor as defined in clients/dotnet/csharp/Session.cs:

namespace WolframeClient
{
    public class Session
        :SessionInterface
    {
        public class Configuration
            :Connection.Configuration
        {
            public string banner { get; set; }
            public string authmethod { get; set; }
            public string username { get; set; }
            public string password { get; set; }
            public string schemadir { get; set; }
            public string schemaext { get; set; }

            public Configuration()
            {
                banner = null;
                authmethod = null;
                username = null;
                password = null;
                schemadir = "http://www.wolframe.net";
                schemaext = "xsd";
            }
        };

        public delegate void AnswerCallback(Answer msg);

        public Session( Configuration config_, AnswerCallback answerCallback_);
    };
}