Skip to main content
Skip table of contents

Writing a Greeter Client in C-Sharp

This page presents a step-by-step guide to writing the client-side of our C# Greeter application.

This client creates a proxy to a remote object that implements the Greeter interface and invokes the greet operation on this object.

You can find the complete source code for this example in the ice-demos repository.

Compile Slice File with Slice Compiler

The first step when writing a C# application with Ice is to compile the Slice definitions for this application with the Slice to C# compiler (slice2cs).

Here we compile the Greeter.ice Slice file we wrote earlier. We recommend that you include this Slice compilation step in your build project, like we demonstrate for the C# demo programs.

This will generate a single file named Greeter.cs which provides APIs that we’ll call in our client code, so it’s essential to generate this file at the beginning of the development process.

Client Implementation

Since all the code generated by slice2cs was generated into the VisitorCenter namespace, we start with a using directive to let us reference it without qualification:

C#
using VisitorCenter;

Then the rest of the client logic can be broken down into four pieces:

1. Create a Communicator

We create a Communicator with Ice.Util.initialize:

C#
await using Ice.Communicator communicator = Ice.Util.initialize(ref args);

The communicator is the main entry point into the Ice runtime. Its responsibilities include establishing connections to servers, caching these connections, and managing configuration properties. We also need a communicator to create a proxy (see next step).

Our client, like most Ice applications, creates a single communicator.

It is important to make sure that your communicator is properly disposed when no longer needed. This ensures that network connections are gracefully closed, threads are joined, and other important clean-up occurs. The easiest way to do this is with an await using like we do here.

2. Create a Greeter Proxy

Next, we need a way to call on a remote Greeter object. In Ice, this is done with proxies. Proxies are local constructs that represent remote Ice objects and provide methods to call operations on those objects.

We create a Greeter proxy by calling createProxy on the GreeterPrxHelper class generated by the Slice compiler. This returns a new instance of GreeterPrx (a Greeter proxy):

C#
GreeterPrx greeter = GreeterPrxHelper.createProxy(
    communicator, "greeter:tcp -h localhost -p 4061");

createProxy accepts our communicator and a “stringified proxy” with the address of the remote Ice object. Here, our stringified proxy says that the target Ice object is named “greeter” and can be reached via tcp on localhost on port 4061.

The name of the interface (Greeter) and the identity of the Ice object (greeter) are independent. The Ice objects hosted in the server could just as easily have identities like santa, bugsBunny, etc.

3. Make an Invocation

The third step is to call greet on the remote Ice object using our proxy and to print the greeting:

C#
string greeting = await greeter.GreetAsync(Environment.UserName);
Console.WriteLine(greeting);

The GreetAsync method does all the heavy lifting for us: the proxy creates a request with the username string, the communicator establishes a connection to localhost:4061, and the request is sent over it. When a response is received, the proxy will unmarshal its payload and finally return a string (the greeting).

Using async/await for this invocation offers a couple advantages:

  • The calling thread can continue doing other work while GreetAsync waits for I/O.

  • The await keyword signals to the reader that GreetAsync is a remote call that may take time to complete.

4. Cleanup

Finally, at the end of our logic, our communicator goes out of scope and is disposed (because we used await using), and then our application exits.

Running the Client

After building the client (see the demo’s README for instructions), you can run it with dotnet:

BASH
dotnet run

This client won’t work unless you’ve also launched a Greeter server.
Writing and running a Greeter server is covered on the next page.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.