Developing and running a sample client

This section describes how to develop and run a .NET C# client application that communicates with an EAServer EJB component.

StepsCreating and deploying an EJB

  1. Create an EJB called EchoEJB and add it to EchoEJB.jar.

  2. To deploy the EJB, run:

    deploy.bat EchoEJB.jar
    

StepsGenerating the C# stubs

  1. Verify that the C# compiler (csc.exe) is in your path.

  2. Run NetCompiler:

    netcc.bat ejbjar-echoejb
    

NetCompiler creates these C# source files in %DJC_HOME%\genfiles\cs\src:

The source files are packaged in the output assembly %DJC_HOME%\deploy\assemblies\echoejb.client.dll.

NoteYou can use the C# stubs that are compiled into assembly files from Visual Basic .NET clients.

StepsDeveloping the C# sample client

  1. Create a C# file called EchoClient.cs, and add this code:

    using System;
    using System.Data;
    using System.Collections;
    using System.Net;
    using System.Net.Sockets;
    
    using com.sybase.ejb.net;
    using com.sybase.net;
    
    public class EchoClient
    {
       public static void Main()
       {
          EjbProvider ejbProvider = EjbProvider.GetInstance();
          ejbProvider.SetProviderURL("iiop://host:port");
          ejbProvider.SetUsername("admin@system");
          ejbProvider.SetPassword("sybase123");
    
          EjbConnection ejb = EjbConnection.GetInstance();
          EchoRemoteHome home = (EchoRemoteHome)
              ejb.LookupHome(typeof(EchoRemoteHome), "EchoEJB/EchoBean");
          EchoRemote echo = home.create();
          Console.WriteLine( echo.hello("World") );
       }
    }
    

    where:

    Code

    Description

    using com.sybase.net

    Imports the stub classes that were generated by running netcc.bat.

    EjbProvider

    Acts as a name service. The code defines the URL (machine name and IIOP port number), user name, and password.

    ejb.LookupHome

    Gets the home interface com.sybase.net.EchoRemoteHome, looks up the server’s naming context, and gets a stub from the server using the connection information from EjbProvider.

    NoteThe code for EjbProvider and EjbConnection is in com.sybase.ejb.net.dll.

    home.create

    Gets the remote interface com.sybase.net.EchoRemote.

    echo.hello

    Calls the remote interface business method hello.

StepsCompiling and running the C# sample client

  1. To compile EchoClient.cs, run:

    csc EchoClient.cs /r:%DJC_HOME%\lib\com.sybase.iiop.net.dll /r:%DJC_HOME%\lib\com.sybase.ejb.net.dll /r:%DJC_HOME%\deploy\assemblies\echoejb.client.dll
    

    Compiling the client creates EchoClient.exe, which you can run from the command line.

    Notecom.sybase.iiop.net.dll and com.sybase.ejb.net.dll are shipped with EAServer 6.1 in the lib subdirectory, and are the basic framework of the .NET client runtime support. echoejb.client.dll is created by netcc.bat when you generate the C# stubs for the EJB. The classes in echoejb.client.dll call methods in com.sybase.iiop.net.dll and com.sybase.ejb.net.dll.

  2. Run:

    EchoClient.exe
    

    The program output is:

    Hello World!
    

StepsTroubleshooting

  1. If you see the following error message when you run the client application, the necessary assembly files cannot be found:

    Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'com.sybase.ejb.net, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified. File name: 'com.sybase.ejb.net, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' at EchoClient.Main()
    

    To solve this problem, you can either: