Skip to content

Commit ce2894f

Browse files
New Invoke demo in C#
1 parent 122f9fe commit ce2894f

File tree

7 files changed

+197
-0
lines changed

7 files changed

+197
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<Project Sdk="Microsoft.NET.Sdk">
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<OutputType>Exe</OutputType>
6+
<Nullable>enable</Nullable>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<!-- Copy the PDBs from the NuGet packages to get file names and line numbers in stack traces. -->
9+
<CopyDebugSymbolFilesFromPackages>true</CopyDebugSymbolFilesFromPackages>
10+
</PropertyGroup>
11+
<ItemGroup>
12+
<PackageReference Include="zeroc.ice.net" Version="3.8.0-alpha0" />
13+
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556">
14+
<PrivateAssets>all</PrivateAssets>
15+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
16+
</PackageReference>
17+
</ItemGroup>
18+
</Project>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) ZeroC, Inc.
2+
3+
// Create an Ice communicator to initialize the Ice runtime.
4+
using Ice.Communicator communicator = Ice.Util.initialize(ref args);
5+
6+
// Create a plain proxy to the remote object.
7+
Ice.ObjectPrx greeter = Ice.ObjectPrxHelper.createProxy(communicator, "greeter:tcp -h localhost -p 4061");
8+
9+
// Create an encapsulation for the input parameter (the user name).
10+
var outputStream = new Ice.OutputStream();
11+
outputStream.startEncapsulation();
12+
outputStream.writeString(Environment.UserName);
13+
outputStream.endEncapsulation();
14+
15+
// Send a request using ice_invokeAsync and wait for the response. ice_invokeAsync can throws exceptions such as
16+
// Ice.ConnectionRefusedException (an Ice local exception), or Ice.ObjectNotExistException (another Ice local exception
17+
// reported by the server).
18+
(bool success, byte[] encapsulation) =
19+
await greeter.ice_invokeAsync(
20+
operation: "greet",
21+
mode: Ice.OperationMode.Normal, // as opposed to Idempotent
22+
inEncaps: outputStream.finished());
23+
24+
if (success)
25+
{
26+
// Unmarshal the encapsulation to get the greeting.
27+
var inputStream = new Ice.InputStream(communicator, encapsulation);
28+
_ = inputStream.startEncapsulation();
29+
string greeting = inputStream.readString();
30+
inputStream.endEncapsulation();
31+
32+
Console.WriteLine(greeting);
33+
}
34+
else
35+
{
36+
// success = false means the encapsulation holds a user exception. This should not happen since our implementation
37+
// does not throw/return any user exception.
38+
Console.WriteLine("greet failed with an unexpected user exception");
39+
}

csharp/Ice/Invoke2/Invoke.sln

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.4.33122.133
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Client", "Client\Client.csproj", "{BBF1199A-46A4-4AE9-AFFE-4D8DD59EB874}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Server", "Server\Server.csproj", "{527EEA4D-77B9-4252-A2CD-C641A25CAD53}"
9+
EndProject
10+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{FBDAF448-665A-4595-98D3-4538C56A666D}"
11+
ProjectSection(SolutionItems) = preProject
12+
README.md = README.md
13+
EndProjectSection
14+
EndProject
15+
Global
16+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
17+
Debug|Any CPU = Debug|Any CPU
18+
Release|Any CPU = Release|Any CPU
19+
EndGlobalSection
20+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
21+
{BBF1199A-46A4-4AE9-AFFE-4D8DD59EB874}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
22+
{BBF1199A-46A4-4AE9-AFFE-4D8DD59EB874}.Debug|Any CPU.Build.0 = Debug|Any CPU
23+
{BBF1199A-46A4-4AE9-AFFE-4D8DD59EB874}.Release|Any CPU.ActiveCfg = Release|Any CPU
24+
{BBF1199A-46A4-4AE9-AFFE-4D8DD59EB874}.Release|Any CPU.Build.0 = Release|Any CPU
25+
{527EEA4D-77B9-4252-A2CD-C641A25CAD53}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
26+
{527EEA4D-77B9-4252-A2CD-C641A25CAD53}.Debug|Any CPU.Build.0 = Debug|Any CPU
27+
{527EEA4D-77B9-4252-A2CD-C641A25CAD53}.Release|Any CPU.ActiveCfg = Release|Any CPU
28+
{527EEA4D-77B9-4252-A2CD-C641A25CAD53}.Release|Any CPU.Build.0 = Release|Any CPU
29+
EndGlobalSection
30+
GlobalSection(SolutionProperties) = preSolution
31+
HideSolutionNode = FALSE
32+
EndGlobalSection
33+
GlobalSection(ExtensibilityGlobals) = postSolution
34+
SolutionGuid = {0B36F1E1-0592-4A15-9981-67BC4A653EC4}
35+
EndGlobalSection
36+
EndGlobal

csharp/Ice/Invoke2/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Invoke
2+
3+
The Invoke shows how to implement a Greeter client and server using [Dynamic Ice][1]. This demo does not use any
4+
Slice definition or generated code; the client and server are nevertheless compatible with the client and server from
5+
the Greeter demo.
6+
7+
You can build the client and server applications with:
8+
9+
``` shell
10+
dotnet build
11+
```
12+
13+
First start the Server program:
14+
15+
```shell
16+
cd Server
17+
dotnet run
18+
```
19+
20+
In a separate terminal, start the Client program:
21+
22+
```shell
23+
cd Client
24+
dotnet run
25+
```
26+
27+
[1]: https://doc.zeroc.com/ice/3.7/client-server-features/dynamic-ice
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) ZeroC, Inc.
2+
3+
using Ice;
4+
5+
namespace InvokeServer;
6+
7+
/// <summary>A Chatbot is an Ice servant that implements operation greet directly, without generated code.</summary>
8+
internal class Chatbot : Ice.Object
9+
{
10+
// Implements abstract method dispatchAsync from the Object interface. There is no synchronous version of dispatch.
11+
public ValueTask<OutgoingResponse> dispatchAsync(IncomingRequest request)
12+
{
13+
if (request.current.operation == "greet")
14+
{
15+
// Unmarshal the input parameter (the user name).
16+
request.inputStream.startEncapsulation();
17+
string name = request.inputStream.readString();
18+
request.inputStream.endEncapsulation();
19+
20+
Console.WriteLine($"Dispatching greet request {{ name = '{name}' }}");
21+
22+
// Create an Ice encapsulation for the return value (the greeting).
23+
var outputStream = request.current.startReplyStream(); // for reply status Ok
24+
outputStream.startEncapsulation();
25+
outputStream.writeString($"Hello, {name}!");
26+
outputStream.endEncapsulation();
27+
28+
// Return the response wrapped in a ValueTask.
29+
return new(new OutgoingResponse(outputStream));
30+
}
31+
else
32+
{
33+
// Chatbot only implements greet. It could also implement ice_ping, ice_isA and other ice_ operations, but
34+
// here we decided not to implement them.
35+
throw new OperationNotExistException();
36+
}
37+
}
38+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) ZeroC, Inc.
2+
3+
using InvokeServer;
4+
5+
// Create an Ice communicator to initialize the Ice runtime. The communicator is disposed before the program exits.
6+
using Ice.Communicator communicator = Ice.Util.initialize(ref args);
7+
8+
// Create an object adapter that listens for incoming requests and dispatches them to servants.
9+
Ice.ObjectAdapter adapter = communicator.createObjectAdapterWithEndpoints("GreeterAdapter", "tcp -p 4061");
10+
11+
// Register the Chatbot servant with the adapter.
12+
adapter.add(new Chatbot(), Ice.Util.stringToIdentity("greeter"));
13+
14+
// Start dispatching requests.
15+
adapter.activate();
16+
Console.WriteLine("Listening on port 4061...");
17+
18+
// Wait until the user presses Ctrl+C.
19+
await CancelKeyPressed;
20+
Console.WriteLine("Caught Ctrl+C, exiting...");
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<Project Sdk="Microsoft.NET.Sdk">
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<OutputType>Exe</OutputType>
6+
<Nullable>enable</Nullable>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<!-- Copy the PDBs from the NuGet packages to get file names and line numbers in stack traces. -->
9+
<CopyDebugSymbolFilesFromPackages>true</CopyDebugSymbolFilesFromPackages>
10+
</PropertyGroup>
11+
<ItemGroup>
12+
<PackageReference Include="zeroc.ice.net" Version="3.8.0-alpha0" />
13+
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556">
14+
<PrivateAssets>all</PrivateAssets>
15+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
16+
</PackageReference>
17+
<Compile Include="../../../Common/Program.CancelKeyPressed.cs" Link="Program.CancelKeyPressed.cs" />
18+
</ItemGroup>
19+
</Project>

0 commit comments

Comments
 (0)