.NET Remoting异常未处理客户端 [英] .NET Remoting Exception not handled Client-Side

查看:110
本文介绍了.NET Remoting异常未处理客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我检查了其余的远程处理问题,这个具体案例似乎没有被解决。



我有一个.NET Remoting服务器/客户端设置。在服务器端,我有一个可以引发异常的方法的对象和一个尝试调用该方法的客户端。



服务器:

  public bool MyEquals(Guid myGuid,string a,string b)
{
if(CheckAuthentication(myGuid))
{
logger.Debug(Request for \+ a +\.Equals(\+ b +\));
返回a.Equals(b);
}
else
{
抛出新的AuthenticationException(UserRegistryService.USER_NOT_REGISTERED_EXCEPTION_TEXT);
}
}

客户端:

  try 
{
bool result = RemotedObject.MyEquals(myGuid,cat,dog);
}
catch(Services.Exceptions.AuthenticationException e)
{
Console.WriteLine(你没有执行该操作的权限);
}

当我使用导致CheckAuthentication返回false的Guid调用MyEquals时,.NET尝试抛出异常,并说AuthenticationException未被处理。这发生在服务器端。这个例外从来没有传遍客户端,我不知道为什么。我所看到的所有问题都解决了客户端处理异常的问题,但它不是自定义异常,而是基类型。在我的情况下,我甚至不能有任何异常来跨越远程边界到客户端。这是AuthenticationException的一个副本。它位于服务器和客户端之间的共享库中。

  [Serializable] 
public class AuthenticationException:ApplicationException,ISerializable
{

public AuthenticationException(string message)
:base(message)
{
}

public AuthenticationException(SerializationInfo info,StreamingContext context)
:base(info,context)
{
}

#region ISerializable成员

void ISerializable.GetObjectData (SerializationInfo info,StreamingContext context)
{
base.GetObjectData(info,context);
}

#endregion
}


解决方案

首先,不继承ApplicationException 。这个建议已经有一段时间了,我相信FxCop会自动生成一个消息。



接下来,你通常应该使用 [Serializable] 属性。我认为这是你的主要问题,因为我在方法调用中出现异常,称为AuthenticationException没有被标记为可序列化。


I checked the rest of the remoting questions, and this specific case did not seem to be addressed.

I have a .NET Remoting server/client set up. On the server side I have an object with a method that can throw an exception, and a client which will try to call that method.

Server:

public bool MyEquals(Guid myGuid, string a, string b)
{
    if (CheckAuthentication(myGuid))
    {
        logger.Debug("Request for \"" + a + "\".Equals(\"" + b + "\")");
        return a.Equals(b);
    }
    else
    {
        throw new AuthenticationException(UserRegistryService.USER_NOT_REGISTERED_EXCEPTION_TEXT);
    }
}

Client:

try
{
    bool result = RemotedObject.MyEquals(myGuid, "cat", "dog");
}
catch (Services.Exceptions.AuthenticationException e)
{
    Console.WriteLine("You do not have permission to execute that action");
}

When I call MyEquals with a Guid which causes CheckAuthentication to return false, .NET tries to throw the exception and says the AuthenticationException was unhandled. This happens server side. The exception is never marshaled over to the client-side, and I cannot figure out why. All of the questions I have looked at address the issue of an exception being handled client-side, but it isn't the custom exception but a base type. In my case, I can't even get any exception to cross the remoting boundary to the client. Here is a copy of AuthenticationException. It is in the shared library between both server and client.

[Serializable]
public class AuthenticationException : ApplicationException, ISerializable
{

    public AuthenticationException(string message)
        : base(message)
    {
    }

    public AuthenticationException(SerializationInfo info, StreamingContext context)
        : base(info, context)
    {
    }

    #region ISerializable Members

    void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
    {
        base.GetObjectData(info, context);
    }

    #endregion
}

解决方案

First and foremost, do not inherit from ApplicationException. This advice has been around for a while, and I believe FxCop will automatically generate a message around this.

Next, you should usually decorate your custom exception with the [Serializable] attribute. I think this is your main issue, as I get an exception on the method call saying AuthenticationException is not marked as serializable.

这篇关于.NET Remoting异常未处理客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆