public static object CreateProxyForType(Type type)
        {
            // Called by the runtime when creating an instance of a type
            // that has been registered as remotely activated.

            // First of all check for remote activation. If the object is not remote, then
            // it may be contextbound.

            ActivatedClientTypeEntry activatedEntry = RemotingConfiguration.IsRemotelyActivatedClientType(type);

            if (activatedEntry != null)
            {
                return(RemotingServices.CreateClientProxy(activatedEntry, null));
            }

            WellKnownClientTypeEntry wellknownEntry = RemotingConfiguration.IsWellKnownClientType(type);

            if (wellknownEntry != null)
            {
                return(RemotingServices.CreateClientProxy(wellknownEntry));
            }

            if (type.IsContextful)
            {
                return(RemotingServices.CreateClientProxyForContextBound(type, null));
            }
#if !NET_2_1
            if (type.IsCOMObject)
            {
                return(RemotingServices.CreateClientProxyForComInterop(type));
            }
#endif
            return(null);
        }
Exemple #2
0
        public IPLCServiceObject Connect(string rootUri)
        {
            try
            {
                if (_Channel != null)
                {
                    ChannelServices.UnregisterChannel(_Channel);
                }

                _Channel = new IpcChannel();

                ChannelServices.RegisterChannel(_Channel, true);

                var remoteType = new WellKnownClientTypeEntry(
                    typeof(PLCServiceObject)
                    , $@"{rootUri}/{nameof(PLCServiceObject)}.rem");

                if (RemotingConfiguration.IsWellKnownClientType(remoteType.ObjectType) == null)
                {
                    RemotingConfiguration.RegisterWellKnownClientType(remoteType);
                }

                IPLCServiceObject serviceObject = new PLCServiceObject();
                serviceObject.Open();

                return(serviceObject);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
                return(null);
            }
        }
        private void RegisterWpiService()
        {
            lock (_lockRegisterWpiService)
            {
                try
                {
                    ChannelServices.RegisterChannel(new TcpChannel(), true);
                }
                catch (System.Exception)
                {
                    //ignor
                }

                if (null == RemotingConfiguration.IsWellKnownClientType(typeof(WPIServiceContract)))
                {
                    RemotingConfiguration.RegisterWellKnownClientType(typeof(WPIServiceContract), string.Format("tcp://localhost:{0}/WPIServiceContract", WPIServiceContract.PORT));
                }

                try
                {
                    WPIServiceContract client = new WPIServiceContract();
                    client.Ping();
                }
                catch (Exception)
                {
                    //unable to connect
                    //try to restart service
                    KillWpiService();
                    //StartWpiService();
                }
            }
        }
Exemple #4
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Initializes a new instance of the <see cref="T:CacheManager"/> class.
        /// </summary>
        /// ------------------------------------------------------------------------------------
        public CacheManager()
        {
            if (!Properties.Settings.Default.UseFileCache)
            {
                return;
            }

            // Register the remote class
            if (Properties.Settings.Default.UseRemoteCache)
            {
                if (RemotingConfiguration.IsWellKnownClientType(typeof(RemoteCacheManager)) == null)
                {
                    // save temporary config file to configure the type
                    string config = string.Format("<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
                                                  "<configuration><system.runtime.remoting><application><client>" +
                                                  "<wellknown  type=\"SIL.FieldWorks.Tools.FileCache.RemoteCacheManager, RemoteFileCache\" " +
                                                  "url=\"tcp://{0}:8700/FileCache.rem\"/></client>" +
                                                  "<channels><channel ref=\"tcp\" port=\"0\"><serverProviders><formatter ref=\"binary\" typeFilterLevel=\"Full\" />" +
                                                  "</serverProviders><clientProviders><formatter ref=\"binary\" typeFilterLevel=\"Full\"/></clientProviders>" +
                                                  "</channel></channels></application></system.runtime.remoting></configuration>",
                                                  Properties.Settings.Default.RemoteHost);
                    string       tempConfigFile = Path.GetTempFileName();
                    StreamWriter writer         = new StreamWriter(tempConfigFile);
                    writer.Write(config);
                    writer.Close();

                    RemotingConfiguration.Configure(tempConfigFile, true);

                    // now delete our temporary config file
                    File.Delete(tempConfigFile);
                }

                m_remoteCacheMgr = new RemoteCacheManager();
            }
        }
    public static void Main()
    {
        ChannelServices.RegisterChannel(new TcpChannel());
        // Register the 'MyServerImpl' object as well known type
        // at client end.
        RemotingConfiguration.RegisterWellKnownClientType(
            typeof(MyServerImpl), "tcp://localhost:8085/SayHello");
// <Snippet1>
        MyServerImpl myObject = new MyServerImpl();
        // Get the assembly for the 'MyServerImpl' object.
        Assembly     myAssembly = Assembly.GetAssembly(typeof(MyServerImpl));
        AssemblyName myName     = myAssembly.GetName();
        // Check whether the specified object type is registered as
        // well-known client type.
        WellKnownClientTypeEntry myWellKnownClientType =
            RemotingConfiguration.IsWellKnownClientType(
                (typeof(MyServerImpl)).FullName, myName.Name);

        Console.WriteLine("The Object type :"
                          + myWellKnownClientType.ObjectType);
        Console.WriteLine("The Object Uri :"
                          + myWellKnownClientType.ObjectUrl);
// </Snippet1>
        Console.WriteLine(myObject.MyMethod("Remote method is called."));
    }
Exemple #6
0
        /// <summary>
        /// 默认的构造函数
        /// </summary>
        public Client(IPEndPoint ipep, string serviceName = null)
        {
            if (string.IsNullOrEmpty(serviceName))
            {
                string svr = ConfigurationManager.AppSettings["service"];
                if (!string.IsNullOrEmpty(svr))
                {
                    serviceName = svr;
                }
                else
                {
                    serviceName = typeof(T).Name;
                }
            }

            this.ipep = ipep;

            //BinaryServerFormatterSinkProvider serverProv = new BinaryServerFormatterSinkProvider();
            //serverProv.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
            BinaryClientFormatterSinkProvider clientProv = new BinaryClientFormatterSinkProvider();
            //IDictionary props = new Hashtable();
            //props["port"] = 0;
            TcpClientChannel channel = new TcpClientChannel(serviceName + DateTime.Now.Ticks, clientProv);

            //channel = new TcpClientChannel();
            try
            {
                ChannelServices.RegisterChannel(channel, false);

                string fullUri = "tcp://" + ipep.Address.ToString() + ":" + ipep.Port + "/" + serviceName;
                Type   t       = typeof(T);
                if (RemotingConfiguration.IsWellKnownClientType(t) == null)
                {
                    RemotingConfiguration.RegisterWellKnownClientType(t, fullUri);
                }
                sharedObject = (T)Activator.GetObject(t, fullUri);
                this.uri     = fullUri;
                //RemotingConfiguration.RegisterActivatedClientType(typeof(T), fullUri);//按值传送
                //sharedObject = Activator.CreateInstance(typeof(T)) as T;//按值传送
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        public static object CreateProxyForType(Type type)
        {
            ActivatedClientTypeEntry activatedClientTypeEntry = RemotingConfiguration.IsRemotelyActivatedClientType(type);

            if (activatedClientTypeEntry != null)
            {
                return(RemotingServices.CreateClientProxy(activatedClientTypeEntry, null));
            }
            WellKnownClientTypeEntry wellKnownClientTypeEntry = RemotingConfiguration.IsWellKnownClientType(type);

            if (wellKnownClientTypeEntry != null)
            {
                return(RemotingServices.CreateClientProxy(wellKnownClientTypeEntry));
            }
            if (type.IsContextful)
            {
                return(RemotingServices.CreateClientProxyForContextBound(type, null));
            }
            return(null);
        }
    public static void Main()
    {
        ChannelServices.RegisterChannel(new TcpChannel());
        // Register the 'MyServerImpl' object as well known type
        // at client end.
        RemotingConfiguration.RegisterWellKnownClientType(typeof(MyServerImpl),
                                                          "tcp://localhost:8085/SayHello");
        MyServerImpl myObject = new MyServerImpl();

        Console.WriteLine(myObject.MyMethod("ClientString"));
// <Snippet1>
        // Check whether the specified object type is registered as
        // well known client type or not.
        WellKnownClientTypeEntry myWellKnownClientType =
            RemotingConfiguration.IsWellKnownClientType(typeof(MyServerImpl));

        Console.WriteLine("The Object type is "
                          + myWellKnownClientType.ObjectType);
        Console.WriteLine("The Object Url is "
                          + myWellKnownClientType.ObjectUrl);
// </Snippet1>
    }
Exemple #9
0
        private bool removeFile(string ip, string port, string name)
        {
            //HttpChannel c = new HttpChannel();
            //ChannelServices.RegisterChannel(c, true);
            Type   ServerType = typeof(FileSystem.FileManager);
            string url        = ip + ":" + port + "/Object";

            if (RemotingConfiguration.IsWellKnownClientType(ServerType) == null)
            {
                RemotingConfiguration.RegisterWellKnownClientType(ServerType, url);
            }

            FileManager vfs = new FileManager();

            try
            {
                return(vfs.remove(name));
            }
            catch (Exception)
            {
                return(false);
            }
        }
Exemple #10
0
        public override MarshalByRefObject CreateInstance(Type serverType)
        {
            // Platform.Assert(Platform.W2K, "ServicedComponent");

            RealProxy          rp = null;
            MarshalByRefObject mo = null;

            DBG.Info(DBG.SC, "SCPA.CreateInstance(" + serverType + ") for managed request");


            ServicedComponentProxy.CleanupQueues(true);

            // First check if the type is configured to be activated remotely or is a well
            // known remote type.
            if ((null != RemotingConfiguration.IsWellKnownClientType(serverType)) ||
                (null != RemotingConfiguration.IsRemotelyActivatedClientType(serverType)))
            {
                // It is configured for remote activation. Ask remoting services to do the
                // job of creating a remoting proxy and returning it.
                mo = base.CreateInstance(serverType);
                rp = RemotingServices.GetRealProxy(mo);
            }
            else
            {
                bool   bIsAnotherProcess = false;
                string uri         = "";
                bool   bEventClass = ServicedComponentInfo.IsTypeEventSource(serverType);
                IntPtr pUnk        = Thunk.Proxy.CoCreateObject(serverType, !bEventClass, ref bIsAnotherProcess, ref uri);
                if (pUnk != IntPtr.Zero)
                {
                    try {
                        // TODO:  Get rid of this useless foreknowledge requirement
                        // Is there a way we can tell by QI'ing this guy if he's
                        // an event class (or something else we need to artificially
                        // wrap?
                        if (bEventClass)
                        {
                            // events and queued components use RemoteServicedComponentProxy
                            // set up a TP & Remote ServicedComponentProxy pair
                            rp = new RemoteServicedComponentProxy(serverType, pUnk, true);
                            mo = (MarshalByRefObject)rp.GetTransparentProxy();
                        }
                        else
                        {
                            if (bIsAnotherProcess)              // a-ha, we know it should be a RSCP now !!!!
                            {
                                FastRSCPObjRef oref = new FastRSCPObjRef(pUnk, serverType, uri);
                                mo = (MarshalByRefObject)RemotingServices.Unmarshal(oref);

                                DBG.Assert(mo != null, "RemotingServices.Unmarshal returned null!");
                                DBG.Assert(RemotingServices.GetRealProxy(mo) is RemoteServicedComponentProxy, "RemotingServices.Unmarshal did not return an RSCP!");
                            }
                            else        // bummer, this will give us back a SCP
                            {
                                mo = (MarshalByRefObject)Marshal.GetObjectForIUnknown(pUnk);

                                DBG.Info(DBG.SC, "ret = " + mo.GetType());
                                DBG.Info(DBG.SC, "st = " + serverType);
                                DBG.Info(DBG.SC, "rt == sc = " + (mo.GetType() == serverType));
                                DBG.Info(DBG.SC, "instanceof = " + serverType.IsInstanceOfType(mo));

                                if (!(serverType.IsInstanceOfType(mo)))
                                {
                                    throw new InvalidCastException(Resource.FormatString("ServicedComponentException_UnexpectedType", serverType, mo.GetType()));
                                }

                                rp = RemotingServices.GetRealProxy(mo);
                                if (!(rp is ServicedComponentProxy) && !(rp is RemoteServicedComponentProxy))
                                {
                                    // in cross-appdomain scenario, we get back a RemotingProxy, SetCOMIUnknown has not been made on our server!
                                    ServicedComponent sc = (ServicedComponent)mo;
                                    sc.DoSetCOMIUnknown(pUnk);
                                }
                            }
                        }
                    }
                    finally
                    {
                        Marshal.Release(pUnk);
                    }
                }
            }

            if (rp is ServicedComponentProxy)
            {
                // Here, we tell the server proxy that it needs to filter out
                // constructor calls:  We only need to do this if
                // the proxy lives in the same context as the caller,
                // otherwise we'll get an Invoke call and can do the
                // filtering automagically:
                ServicedComponentProxy scp = (ServicedComponentProxy)rp;
                if (scp.HomeToken == Thunk.Proxy.GetCurrentContextToken())
                {
                    scp.FilterConstructors();
                }
            }

            DBG.Assert(mo is ServicedComponent, " CoCI returned an invalid object type");
            DBG.Info(DBG.SC, "SCPA.CreateInstance done.");
            return(mo);
        }
Exemple #11
0
        /// <summary>
        /// 指定服务的构造函数
        /// </summary>
        public Client(string serviceName = null)
        {
            if (string.IsNullOrEmpty(serviceName))
            {
                string svr = ConfigurationManager.AppSettings["service"];
                if (!string.IsNullOrEmpty(svr))
                {
                    serviceName = svr;
                }
                else
                {
                    serviceName = typeof(T).Name;
                }
            }
            //BinaryServerFormatterSinkProvider serverProv = new BinaryServerFormatterSinkProvider();
            //serverProv.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
            BinaryClientFormatterSinkProvider clientProv = new BinaryClientFormatterSinkProvider();
            //IDictionary props = new Hashtable();
            //props["port"] = 0;
            TcpClientChannel channel = new TcpClientChannel(serviceName + DateTime.Now.Ticks, clientProv);

            //channel = new TcpClientChannel();
            try
            {
                ChannelServices.RegisterChannel(channel, true);

                string    serverStr = ConfigurationManager.AppSettings["server"];
                IPAddress RETIP;
                if (!string.IsNullOrEmpty(serverStr) && IPAddress.TryParse(serverStr, out RETIP))
                {
                    server = RETIP;
                }
                else
                {
                    IPAddress        ip  = IPAddress.Loopback;
                    List <IPAddress> ips = KellMQ.Common.GetIPsV4();
                    if (ips.Count > 0)
                    {
                        ip = ips[0];
                    }
                    server = ip;
                }

                int    port    = 8888;
                string portStr = ConfigurationManager.AppSettings["port"];
                int    RET;
                if (!string.IsNullOrEmpty(portStr) && int.TryParse(portStr, out RET))
                {
                    port = RET;
                }

                string fullUri = "tcp://" + server.ToString() + ":" + port + "/" + serviceName;
                Type   t       = typeof(T);
                if (RemotingConfiguration.IsWellKnownClientType(t) == null)
                {
                    RemotingConfiguration.RegisterWellKnownClientType(t, fullUri);
                }
                sharedObject = (T)Activator.GetObject(t, fullUri);
                this.uri     = fullUri;
                //RemotingConfiguration.RegisterActivatedClientType(typeof(T), fullUri);//按值传送
                //sharedObject = Activator.CreateInstance(typeof(T)) as T;//按值传送
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        public override MarshalByRefObject CreateInstance(Type serverType)
        {
            RealProxy          realProxy        = null;
            MarshalByRefObject transparentProxy = null;

            ServicedComponentProxy.CleanupQueues(false);
            if ((RemotingConfiguration.IsWellKnownClientType(serverType) != null) || (RemotingConfiguration.IsRemotelyActivatedClientType(serverType) != null))
            {
                transparentProxy = base.CreateInstance(serverType);
                realProxy        = RemotingServices.GetRealProxy(transparentProxy);
            }
            else
            {
                bool   bIsAnotherProcess = false;
                string uri   = "";
                bool   flag2 = ServicedComponentInfo.IsTypeEventSource(serverType);
                IntPtr pUnk  = Proxy.CoCreateObject(serverType, !flag2, ref bIsAnotherProcess, ref uri);
                if (pUnk != IntPtr.Zero)
                {
                    try
                    {
                        if (flag2)
                        {
                            realProxy        = new RemoteServicedComponentProxy(serverType, pUnk, true);
                            transparentProxy = (MarshalByRefObject)realProxy.GetTransparentProxy();
                        }
                        else
                        {
                            bool flag3 = (RemotingConfiguration.IsWellKnownClientType(serverType) != null) || (null != RemotingConfiguration.IsRemotelyActivatedClientType(serverType));
                            if (bIsAnotherProcess && !flag3)
                            {
                                FastRSCPObjRef objectRef = new FastRSCPObjRef(pUnk, serverType, uri);
                                transparentProxy = (MarshalByRefObject)RemotingServices.Unmarshal(objectRef);
                            }
                            else
                            {
                                transparentProxy = (MarshalByRefObject)Marshal.GetObjectForIUnknown(pUnk);
                                if (!serverType.IsInstanceOfType(transparentProxy))
                                {
                                    throw new InvalidCastException(Resource.FormatString("ServicedComponentException_UnexpectedType", serverType, transparentProxy.GetType()));
                                }
                                realProxy = RemotingServices.GetRealProxy(transparentProxy);
                                if ((!bIsAnotherProcess && !(realProxy is ServicedComponentProxy)) && !(realProxy is RemoteServicedComponentProxy))
                                {
                                    ((ServicedComponent)transparentProxy).DoSetCOMIUnknown(pUnk);
                                }
                            }
                        }
                    }
                    finally
                    {
                        Marshal.Release(pUnk);
                    }
                }
            }
            if (realProxy is ServicedComponentProxy)
            {
                ServicedComponentProxy proxy2 = (ServicedComponentProxy)realProxy;
                if (proxy2.HomeToken == Proxy.GetCurrentContextToken())
                {
                    proxy2.FilterConstructors();
                }
            }
            return(transparentProxy);
        }