This section describes how to develop and run a .NET C# client application that communicates with an EAServer EJB component.
Creating and deploying
an EJB
Create an EJB called EchoEJB and add it to EchoEJB.jar.
To deploy the EJB, run:
deploy.bat EchoEJB.jar
Generating the C# stubs
Verify that the C# compiler (csc.exe) is in your path.
Run NetCompiler:
netcc.bat ejbjar-echoejb
NetCompiler creates these C# source files in %DJC_HOME%\genfiles\cs\src:
echoejb.cs – interface file, which must be included to call the EJB.
echoejb_Stub.cs – stub file.
echoejbHelper.cs – helper classes for data marshalling and narrowing.
The source files are packaged in the output assembly %DJC_HOME%\deploy\assemblies\echoejb.client.dll.
You can use the C# stubs that are compiled
into assembly files from Visual Basic .NET clients.
Developing the C# sample
client
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 |
---|---|
|
Imports the stub classes that were generated by running netcc.bat. |
|
Acts as a name service. The code defines the URL (machine name and IIOP port number), user name, and password. |
|
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.
|
|
Gets the remote interface com.sybase.net.EchoRemote. |
|
Calls the remote interface business method hello. |
Compiling and running
the C# sample client
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.
com.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.
Run:
EchoClient.exe
The program output is:
Hello World!
Troubleshooting
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:
Copy the assembly files to the current directory, or
Create an XML configuration file to identify the location of the assembly files:
In the directory that contains EchoClient.exe, create an XML file called EchoClient.exe.config, and add this code:
<?xml version="1.0" encoding="utf-8"?> <configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <probing privatePath="lib"/> </assemblyBinding> </runtime> </configuration>
Copy these assembly files to the location identified by privatePath, which must be a subdirectory of the application’s base directory:
com.sybase.iiop.net.dll
com.sybase.ejb.net.dll
com.sybase.jms.net.dll