Пример #1
0
    public static void Main()
    {
        GenericIdentity  ident = new GenericIdentity("Bob");
        GenericPrincipal prpal = new GenericPrincipal(ident,
                                                      new string[] { "Level1" });
        LogicalCallContextData data = new LogicalCallContextData(prpal);

        //Enter data into the CallContext
        CallContext.SetData("test data", data);


        Console.WriteLine(data.numOfAccesses);

        ChannelServices.RegisterChannel(new TcpChannel());

        RemotingConfiguration.RegisterActivatedClientType(typeof(HelloServiceClass),
                                                          "tcp://localhost:8082");

        HelloServiceClass service = new HelloServiceClass();

        if (service == null)
        {
            Console.WriteLine("Could not locate server.");
            return;
        }


        // call remote method
        Console.WriteLine();
        Console.WriteLine("Calling remote object");
        Console.WriteLine(service.HelloMethod("Caveman"));
        Console.WriteLine(service.HelloMethod("Spaceman"));
        Console.WriteLine(service.HelloMethod("Bob"));
        Console.WriteLine("Finished remote object call");
        Console.WriteLine();

        //Extract the returned data from the call context
        LogicalCallContextData returnedData =
            (LogicalCallContextData)CallContext.GetData("test data");

        Console.WriteLine(data.numOfAccesses);
        Console.WriteLine(returnedData.numOfAccesses);
    }
    public String HelloMethod(String name)
    {
        //Extract the call context data
        LogicalCallContextData data        = (LogicalCallContextData)CallContext.GetData("test data");
        IPrincipal             myPrincipal = data.Principal;

        //Check the user identity
        if (myPrincipal.Identity.Name == "Bob")
        {
            Console.WriteLine("\nHello {0}, you are identified!", myPrincipal.Identity.Name);
            Console.WriteLine(data.numOfAccesses);
        }
        else
        {
            Console.WriteLine("Go away! You are not identified!");
            return(String.Empty);
        }

        // calculate and return result to client
        return("Hi there " + name + ".");
    }