与远程服务器通信时出现异常?

时间:2012-06-18 11:36:01

标签: c# exception remoting filenotfoundexception .net-remoting

我已经建立了与远程服务器的通信..远程服务器暴露了一个事件。当我尝试订阅事件时,我得到了跟踪堆栈跟踪的异常

    [System.Reflection.TargetInvocationException] 
    = {"Exception has been thrown by the target of an invocation."}

    [System.Reflection.TargetInvocationException] 
    = {"Exception has been thrown by the target of an invocation."}
    at System.Reflection.Assembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection)
    at System.Reflection.Assembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark,
    Boolean throwOnFileNotFound, Boolean forIntrospection)
    at System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
    at System.Reflection.Assembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)\r\n
    at System.Reflection.Assembly.Load(String assemblyString)
    at System.Reflection.MemberInfoSerializationHolder..ctor(SerializationInfo info, StreamingContext context)

    StackTrace          
    Server stack trace: 
    at System.RuntimeMethodHandle._SerializationInvoke(Object target, SignatureStruct& declaringTypeSig, SerializationInfo info, StreamingContext context)
    at System.Reflection.RuntimeConstructorInfo.SerializationInvoke(Object target, SerializationInfo info, StreamingContext context)
    at System.Runtime.Serialization.ObjectManager.CompleteISerializableObject(Object obj, SerializationInfo info, StreamingContext context) 
    at System.Runtime.Serialization.ObjectManager.FixupSpecialObject(ObjectHolder holder)
    at System.Runtime.Serialization.ObjectManager.DoFixups()
    at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
    at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
    at System.Runtime.Remoting.Channels.CoreChannel.DeserializeBinaryRequestMessage(String objectUri, Stream inputStream, Boolean bStrictBinding, TypeFilterLevel securityLevel)
    at System.Runtime.Remoting.Channels.BinaryServerFormatterSink.ProcessMessage(IServerChannelSinkStack sinkStack, IMessage requestMsg, ITransportHeaders requestHeaders, Stream requestStream, IMessage& responseMsg, ITransportHeaders& responseHeaders, Stream& responseStream)

    Exception rethrown at [0]: 
    at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
    at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
    at Mycode.Method(EventHandler value)
    at Mysource.Initialize()

我的代码块如下: 远程服务器的配置如下 //代码

TurnOffRemotingExceptionFiltering();

BinaryClientFormatterSinkProvider^ clientProvider = nullptr;
BinaryServerFormatterSinkProvider^ serverProvider = gcnew aryServerFormatterSinkProvider();
serverProvider->TypeFilterLevel = TypeFilterLevel::Full;
System::Collections::IDictionary^ props = gcnew Hashtable();
props["port"] = port;
String^ guid = System::Guid::NewGuid().ToString();
props["typeFilterLevel"] = TypeFilterLevel::Full;

//TcpChannel
channel = gcnew TcpChannel(props, clientProvider, serverProvider);
ChannelServices::RegisterChannel(channel, false);
props["name"] = guid;

void RemoteConnectionManager::TurnOffRemotingExceptionFiltering()
{
                 // Gets the assembly in which the 'System.Runtime.Remoting.RemotingConfiguration' is defined.
                Assembly^ remoting = Assembly::GetAssembly(RemotingConfiguration::typeid);

                // Gets the Type object of 'System.Runtime.Remoting.CustomErrorsModes' from assembly instance.
                Type^ customErrorsModes = remoting->GetType("System.Runtime.Remoting.CustomErrorsModes");

                // Gets the Type object of 'System.Runtime.Remoting.RemotingConfigHandler' from assembly instance.
                Type^ remotingConfigHandler = remoting->GetType("System.Runtime.Remoting.RemotingConfigHandler");

                // Gets details of '_errorMode' field.'_errorMode' indicates whether the server channels in 
                // this application domain return filtered or complete exception information to local or remote callers.
                FieldInfo^ errorMode = remotingConfigHandler->GetField("_errorMode",
                                        BindingFlags::Static | BindingFlags::NonPublic);

                // Gets details of 'Off' field.'System.Runtime.Remoting.CustomErrorsModes' is an Enumeration
                FieldInfo^ mode = customErrorsModes->GetField("Off");

                // Sets "Off" to get to get complete exception information 
                // from server channels to to local or remote callers.
                errorMode->SetValue(nullptr, mode->GetValue(nullptr));
            }

1 个答案:

答案 0 :(得分:0)

在分布式系统的情况下,我们需要一个包装类来与服务器和客户端进行通信。我们无法直接订阅没有包装类的事件。这个包装类充当与客户端进行通信的媒介。服务器