Skip to content

Commit

Permalink
Service resolution and method search on the server should report erro…
Browse files Browse the repository at this point in the history
…rs back to the client.
  • Loading branch information
yallie committed Jun 29, 2024
1 parent 98e0c19 commit f0779f7
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 76 deletions.
32 changes: 29 additions & 3 deletions CoreRemoting.Tests/RpcTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,12 +350,13 @@ public void Enum_arguments_should_be_passed_correctly()
}

[Fact]
public void Missing_method_throws_MissingMethodException()
public void Missing_method_throws_RemoteInvocationException()
{
using var client = new RemotingClient(new ClientConfig()
{
ConnectionTimeout = 0,
InvocationTimeout = 0,
SendTimeout = 0,
MessageEncryption = false,
ServerPort = _serverFixture.Server.Config.NetworkPort
});
Expand All @@ -376,8 +377,33 @@ public void Missing_method_throws_MissingMethodException()
client.Connect();

var proxy = client.CreateProxy<ITestService>();
//var result = proxy.TestMethod(null);
//Assert.Fail();
var ex = Assert.Throws<RemoteInvocationException>(() => proxy.TestMethod(null));

// a localized message similar to "Method 'Missing method' not found"
Assert.NotNull(ex);
Assert.Contains("Missing Method", ex.Message);
}

[Fact]
public void Missing_service_throws_RemoteInvocationException()
{
using var client = new RemotingClient(new ClientConfig()
{
ConnectionTimeout = 0,
InvocationTimeout = 0,
SendTimeout = 0,
MessageEncryption = false,
ServerPort = _serverFixture.Server.Config.NetworkPort
});

client.Connect();

var proxy = client.CreateProxy<IDisposable>();
var ex = Assert.Throws<RemoteInvocationException>(() => proxy.Dispose());

// a localized message similar to "Service 'System.IDisposable' is not registered"
Assert.NotNull(ex);
Assert.Contains("IDisposable", ex.Message);
}
}
}
166 changes: 93 additions & 73 deletions CoreRemoting/RemotingSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -402,82 +402,38 @@ private void ProcessRpcMessage(WireMessage request)
Session = this
};

var serializedResult = new byte[] { };

var service = _server.ServiceRegistry.GetService(callMessage.ServiceName);
var serviceInterfaceType =
_server.ServiceRegistry.GetServiceInterfaceType(callMessage.ServiceName);

CallContext.RestoreFromSnapshot(callMessage.CallContextSnapshot);

serverRpcContext.ServiceInstance = service;

callMessage.UnwrapParametersFromDeserializedMethodCallMessage(
out var parameterValues,
out var parameterTypes);

parameterValues = MapArguments(parameterValues, parameterTypes);

var method = GetMethodInfo(callMessage, serviceInterfaceType, parameterTypes);
if (method == null)
throw new MissingMethodException(
className: callMessage.ServiceName,
methodName: callMessage.MethodName);

var oneWay = method.GetCustomAttribute<OneWayAttribute>() != null;

if (_server.Config.AuthenticationRequired && !_isAuthenticated)
throw new NetworkException("Session is not authenticated.");

object result = null;
var serializedResult = Array.Empty<byte>();
var method = default(MethodInfo);
var parameterValues = Array.Empty<object>();
var parameterTypes = Array.Empty<Type>();
var oneWay = false;

try
{
CurrentSession.Value = this;
var service = _server.ServiceRegistry.GetService(callMessage.ServiceName);
var serviceInterfaceType =
_server.ServiceRegistry.GetServiceInterfaceType(callMessage.ServiceName);

((RemotingServer)_server).OnBeforeCall(serverRpcContext);
CallContext.RestoreFromSnapshot(callMessage.CallContextSnapshot);

result = method.Invoke(service, parameterValues);
serverRpcContext.ServiceInstance = service;

var returnType = method.ReturnType;
callMessage.UnwrapParametersFromDeserializedMethodCallMessage(
out parameterValues,
out parameterTypes);

if (result != null)
{
// Wait for result value if result is a Task
if (typeof(Task).IsAssignableFrom(returnType))
{
var resultTask = (Task)result;
resultTask.Wait();
parameterValues = MapArguments(parameterValues, parameterTypes);

if (returnType.IsGenericType)
{
result = returnType.GetProperty("Result")?.GetValue(resultTask);
}
else // ordinary non-generic task
{
result = null;
}
}
else if (returnType.GetCustomAttribute<ReturnAsProxyAttribute>() != null)
{
var isRegisteredService =
returnType.IsInterface &&
_server.ServiceRegistry
.GetAllRegisteredTypes().Any(s =>
returnType.AssemblyQualifiedName != null &&
returnType.AssemblyQualifiedName.Equals(s.AssemblyQualifiedName));

if (!isRegisteredService)
{
throw new InvalidOperationException(
$"Type '{returnType.AssemblyQualifiedName}' is not a registered service.");
}
method = GetMethodInfo(callMessage, serviceInterfaceType, parameterTypes);
if (method == null)
throw new MissingMethodException(
className: callMessage.ServiceName,
methodName: callMessage.MethodName);

result = new ServiceReference(
serviceInterfaceTypeName: returnType.FullName + ", " + returnType.Assembly.GetName().Name,
serviceName: returnType.FullName);
}
}
oneWay = method.GetCustomAttribute<OneWayAttribute>() != null;

if (_server.Config.AuthenticationRequired && !_isAuthenticated)
throw new NetworkException("Session is not authenticated.");
}
catch (Exception ex)
{
Expand All @@ -486,21 +442,85 @@ private void ProcessRpcMessage(WireMessage request)
message: ex.Message,
innerEx: ex.GetType().IsSerializable ? ex : null);

((RemotingServer)_server).OnAfterCall(serverRpcContext);

if (oneWay)
return;

serializedResult =
_server.Serializer.Serialize(serverRpcContext.Exception);
}
finally
{
CurrentSession.Value = null;
}

object result = null;

if (serverRpcContext.Exception == null)
{
try
{
CurrentSession.Value = this;

((RemotingServer)_server).OnBeforeCall(serverRpcContext);

result = method.Invoke(serverRpcContext.ServiceInstance, parameterValues);

var returnType = method.ReturnType;

if (result != null)
{
// Wait for result value if result is a Task
if (typeof(Task).IsAssignableFrom(returnType))
{
var resultTask = (Task)result;
resultTask.Wait();

if (returnType.IsGenericType)
{
result = returnType.GetProperty("Result")?.GetValue(resultTask);
}
else // ordinary non-generic task
{
result = null;
}
}
else if (returnType.GetCustomAttribute<ReturnAsProxyAttribute>() != null)
{
var isRegisteredService =
returnType.IsInterface &&
_server.ServiceRegistry
.GetAllRegisteredTypes().Any(s =>
returnType.AssemblyQualifiedName != null &&
returnType.AssemblyQualifiedName.Equals(s.AssemblyQualifiedName));

if (!isRegisteredService)
{
throw new InvalidOperationException(
$"Type '{returnType.AssemblyQualifiedName}' is not a registered service.");
}

result = new ServiceReference(
serviceInterfaceTypeName: returnType.FullName + ", " + returnType.Assembly.GetName().Name,
serviceName: returnType.FullName);
}
}
}
catch (Exception ex)
{
serverRpcContext.Exception =
new RemoteInvocationException(
message: ex.Message,
innerEx: ex.GetType().IsSerializable ? ex : null);

((RemotingServer)_server).OnAfterCall(serverRpcContext);

if (oneWay)
return;

serializedResult =
_server.Serializer.Serialize(serverRpcContext.Exception);
}
finally
{
CurrentSession.Value = null;
}

if (!oneWay)
{
serverRpcContext.MethodCallResultMessage =
Expand Down

0 comments on commit f0779f7

Please sign in to comment.