示例#1
0
        static void Main()
        {
            // Register a channel.
            TcpChannel myChannel = new TcpChannel();

            ChannelServices.RegisterChannel(myChannel);
            RemotingConfiguration.RegisterActivatedClientType(
                typeof(HelloService), "tcp://localhost:8085/");

            // Get the remote object.
            HelloService myService = new HelloService();

            // Get a sponsor for renewal of time.
            ClientSponsor mySponsor = new ClientSponsor();

            // Register the service with sponsor.
            mySponsor.Register(myService);

            // Set renewaltime.
            mySponsor.RenewalTime = TimeSpan.FromMinutes(2);

            // Renew the lease.
            ILease   myLease = (ILease)mySponsor.InitializeLifetimeService();
            TimeSpan myTime  = mySponsor.Renewal(myLease);

            Console.WriteLine("Renewed time in minutes is " + myTime.Minutes.ToString());

            // Call the remote method.
            Console.WriteLine(myService.HelloMethod("World"));

            // Unregister the channel.
            mySponsor.Unregister(myService);
            mySponsor.Close();
        }
        static void Main()
        {
// <Snippet3>
            // Register the channel.
            TcpChannel myChannel = new TcpChannel();

            ChannelServices.RegisterChannel(myChannel);
            RemotingConfiguration.RegisterActivatedClientType(
                typeof(HelloService), "Tcp://localhost:8085");

            TimeSpan myTimeSpan = TimeSpan.FromMinutes(10);

            // Create a remote object.
            HelloService myService = new HelloService();

            ILease myLease;

            myLease = (ILease)RemotingServices.GetLifetimeService(myService);
            if (myLease == null)
            {
                Console.WriteLine("Cannot lease.");
                return;
            }

            Console.WriteLine("Initial lease time is " + myLease.InitialLeaseTime);
            Console.WriteLine("Current lease time is " + myLease.CurrentLeaseTime);
            Console.WriteLine("Renew on call time is " + myLease.RenewOnCallTime);
            Console.WriteLine("Sponsorship timeout is " + myLease.SponsorshipTimeout);
            Console.WriteLine("Current lease state is " + myLease.CurrentState.ToString());
// </Snippet3>
            // Register with a sponser.
            ClientSponsor mySponsor = new ClientSponsor();

            myLease.Register(mySponsor);
            Console.WriteLine("Wait for lease to expire (approx. 15 seconds)...");
            System.Threading.Thread.Sleep(myLease.CurrentLeaseTime);
            Console.WriteLine("Current lease time before renewal is " + myLease.CurrentLeaseTime);

            // Renew the lease time.
            myLease.Renew(myTimeSpan);
            Console.WriteLine("Current lease time after renewal is " + myLease.CurrentLeaseTime);

            // Call the Remote method.
            Console.WriteLine("Remote method output is " + myService.HelloMethod("Microsoft"));

            myLease.Unregister(mySponsor);
            GC.Collect();
            GC.WaitForPendingFinalizers();

            // Register with lease time of 15 minutes.
            myLease.Register(mySponsor, TimeSpan.FromMinutes(15));
            Console.WriteLine("Registered client with lease time of 15 minutes.");
            Console.WriteLine("Current lease time is " + myLease.CurrentLeaseTime);

            // Call the Remote method.
            Console.WriteLine("Remote method output is " + myService.HelloMethod("Microsoft"));
            myLease.Unregister(mySponsor);
        }
示例#3
0
        static void Main()
        {
// <Snippet1>
            // Register the channel.
            TcpChannel myChannel = new TcpChannel();

            ChannelServices.RegisterChannel(myChannel);
            RemotingConfiguration.RegisterActivatedClientType(typeof(HelloService), "Tcp://localhost:8082");

            GenericIdentity          myIdentity  = new GenericIdentity("Bob");
            GenericPrincipal         myPrincipal = new GenericPrincipal(myIdentity, new string[] { "Level1" });
            MyLogicalCallContextData myData      = new MyLogicalCallContextData(myPrincipal);

            // Set DataSlot with name parameter.
            CallContext.SetData("test data", myData);

            // Create a remote object.
            HelloService myService = new HelloService();

            if (myService == null)
            {
                Console.WriteLine("Cannot locate server.");
                return;
            }

            // Call the Remote methods.
            Console.WriteLine("Remote method output is " + myService.HelloMethod("Microsoft"));

            MyLogicalCallContextData myReturnData =
                (MyLogicalCallContextData)CallContext.GetData("test data");

            if (myReturnData == null)
            {
                Console.WriteLine("Data is null.");
            }
            else
            {
                Console.WriteLine("Data is '{0}'", myReturnData.numOfAccesses);
            }

            // DataSlot with same Name Parameter which was Set is Freed.
            CallContext.FreeNamedDataSlot("test data");
            MyLogicalCallContextData myReturnData1 =
                (MyLogicalCallContextData)CallContext.GetData("test data");

            if (myReturnData1 == null)
            {
                Console.WriteLine("FreeNamedDataSlot Successful for test data");
            }
            else
            {
                Console.WriteLine("FreeNamedDataSlot Failed  for test data");
            }
// </Snippet1>

// <Snippet2>
            // Array of Headers with name and values initialized.
            Header[] myArrSetHeader = { new Header("Header0", "CallContextHeader0"),
                                        new Header("Header1", "CallContextHeader1") };

            // Pass the Header Array with method call.
            // Header will be set in the method by'CallContext.SetHeaders' method in remote object.

            Console.WriteLine("Remote HeaderMethod output is " +
                              myService.HeaderMethod("CallContextHeader", myArrSetHeader));

            Header[] myArrGetHeader;
            // Get Header Array.
            myArrGetHeader = CallContext.GetHeaders();
            if (null == myArrGetHeader)
            {
                Console.WriteLine("CallContext.GetHeaders Failed");
            }
            else
            {
                Console.WriteLine("Headers:");
            }
            foreach (Header myHeader in myArrGetHeader)
            {
                Console.WriteLine("Value in Header '{0}' is '{1}'.", myHeader.Name, myHeader.Value);
            }
// </Snippet2>
        }