没有处理.NET远程处理异常客户端 [英] .NET Remoting Exception not handled Client-Side

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

问题描述

我检查的远程处理问题的休息,而这种特定情况下似乎并没有得到解决。

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

服务器:

 公共BOOL MyEquals(GUID myGuid,字符串,字符串二)
{
    如果(CheckAuthentication(myGuid))
    {
        logger.Debug(请求\+ A +\等于(\+ B +\));
        返回a.Equals(B);
    }
    其他
    {
        抛出新的AuthenticationException(UserRegistryService.USER_NOT_REGISTERED_EXCEPTION_TEXT);
    }
}
 

客户端:

 尝试
{
    布尔结果= RemotedObject.MyEquals(myGuid,猫,狗);
}
赶上(Services.Exceptions.AuthenticationException E)
{
    Console.WriteLine(你没有执行该操作的权限);
}
 

当我打电话MyEquals一个GUID,这将导致CheckAuthentication返回false,.NET试图抛出异常,并说,是的AuthenticationException未处理。发生这种情况的服务器侧。唯一的例外是永远不会封转移到客户端,我想不通这是为什么。所有的我已经看过了异常的问题所处理的客户端地址的问题,但它不是定制异常,但一个基本类型。就我而言,我甚至不能得到任何的异常跨越远程处理边界的客户端。这里是的AuthenticationException的副本。它是在服务器和客户端之间的共享库。

  [Serializable接口]
公共类的AuthenticationException:ApplicationException的,ISerializable的
{

    公众的AuthenticationException(字符串消息)
        :碱(消息)
    {
    }

    公众的AuthenticationException(SerializationInfo中的信息,的StreamingContext上下文)
        :基地(信息,上下文)
    {
    }

    #地区ISerializable的成员

    无效ISerializable.GetObjectData(SerializationInfo中的信息,的StreamingContext上下文)
    {
        base.GetObjectData(信息,上下文);
    }

    #endregion
}
 

解决方案

首先,<一个href="http://stackoverflow.com/questions/52753/should-i-derive-custom-exceptions-from-exception-or-applicationexception-in-net">do不是从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远程处理异常客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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