示例#1
0
        public Task <Tuple <TExtension, TExtensionInterface> > BindExtension <TExtension, TExtensionInterface>(Func <TExtension> newExtensionFunc)
            where TExtension : IGrainExtension
            where TExtensionInterface : IGrainExtension
        {
            // Hookup Extension.
            IAddressable currentGrain = RuntimeClient.Current.CurrentActivationData.GrainInstance;
            TExtension   extension;

            if (!TryGetExtensionHandler(out extension))
            {
                extension = newExtensionFunc();
                if (!TryAddExtension(extension))
                {
                    throw new OrleansException("Failed to register " + typeof(TExtension).Name);
                }
            }

            var factoryName       = String.Format("{0}.{1}Factory", typeof(TExtensionInterface).Namespace, typeof(TExtensionInterface).Name.Substring(1)); // skip the I
            var currentTypedGrain = (TExtensionInterface)GrainClient.InvokeStaticMethodThroughReflection(
                typeof(TExtensionInterface).Assembly.FullName,
                factoryName,
                "Cast",
                new Type[] { typeof(IAddressable) },
                new object[] { currentGrain });

            return(Task.FromResult(Tuple.Create(extension, currentTypedGrain)));
        }
示例#2
0
 private static IPubSubRendezvousGrain GetRendezvousGrain(StreamId streamId)
 {
     return((IPubSubRendezvousGrain)GrainClient.InvokeStaticMethodThroughReflection(
                "Orleans",
                "Orleans.Streams.PubSubRendezvousGrainFactory",
                "GetGrain",
                new Type[] { typeof(Guid), typeof(string) },
                new object[] { streamId.Guid, streamId.ProviderName + "_" + streamId.Namespace }));
 }
示例#3
0
        public async Task <Tuple <TExtension, TExtensionInterface> > BindExtension <TExtension, TExtensionInterface>(Func <TExtension> newExtensionFunc)
            where TExtension : IGrainExtension
            where TExtensionInterface : IGrainExtension
        {
            IAddressable addressable;
            TExtension   extension;

            // until we have a means to get the factory related to a grain interface, we have to search linearly for the factory.
            var factoryName = String.Format("{0}.{1}Factory", typeof(TExtensionInterface).Namespace, typeof(TExtensionInterface).Name.Substring(1)); // skip the I
            var factoryType = TypeUtils.ResolveType(factoryName);


            using (await lockable.LockAsync())
            {
                Tuple <IGrainExtension, IAddressable> entry;
                if (caoTable.TryGetValue(typeof(TExtensionInterface), out entry))
                {
                    extension   = (TExtension)entry.Item1;
                    addressable = entry.Item2;
                }
                else
                {
                    extension = newExtensionFunc();
                    var obj = factoryType.InvokeMember("CreateObjectReference",
                                                       BindingFlags.Default | BindingFlags.InvokeMethod,
                                                       null, null, new object[] { extension }, CultureInfo.InvariantCulture);
                    addressable = (IAddressable)await(Task <TExtensionInterface>) obj;

                    if (null == addressable)
                    {
                        throw new NullReferenceException("addressable");
                    }
                    entry = Tuple.Create((IGrainExtension)extension, addressable);
                    caoTable.Add(typeof(TExtensionInterface), entry);
                }
            }

            var typedAddressable = (TExtensionInterface)GrainClient.InvokeStaticMethodThroughReflection(
                typeof(TExtensionInterface).Assembly.FullName,
                factoryName,
                "Cast",
                new Type[] { typeof(IAddressable) },
                new object[] { addressable });

            // we have to return the extension as well as the IAddressable because the caller needs to root the extension
            // to prevent it from being collected (the IAddressable uses a weak reference).
            return(Tuple.Create(extension, typedAddressable));
        }