Beispiel #1
0
        // returns when invocation is started, but before it returns
        // end of invocation is signaled via TaskCompletionSourceProxy
        public void InvokeAsync(
            InvocationRequest.PortableInvocationRequest invocationRequest, SecurityZone securityZone
            )
        {
            if (invocationRequest == null)
            {
                throw new ArgumentNullException("invocationRequest");
            }

            lock (this.stateLock) {
                if (!this.isReady)
                {
                    throw new InvalidOperationException(
                              "Not ready: currently executing another task or cleaning up.");
                }
                this.isReady = false;


                var zoneEvidence = new Evidence();
                zoneEvidence.AddHostEvidence(new Zone(securityZone));
                var zonePermissions = SecurityManager.GetStandardSandbox(zoneEvidence);

                var taskDomainName = string.Format(CultureInfo.InvariantCulture, "{0}_{1}",
                                                   invocationRequest.MethodName, DateTimeOffset.UtcNow.ToUnixTimeMilliseconds());

                // ReSharper disable once AssignNullToNotNullAttribute
                this.activeRunnerDomain = AppDomain.CreateDomain(taskDomainName, AppDomain.CurrentDomain.Evidence,
                                                                 new AppDomainSetup {
                    ApplicationBase    = invocationRequest.ApplicationBase,
                    LoaderOptimization = LoaderOptimization.MultiDomainHost
                }, zonePermissions, typeof(WorkerServer).GetStrongNameOfAssemblyAsArray());

                this.activeRunner = (TaskRunner)this.activeRunnerDomain.CreateInstanceFromAndUnwrap(
                    typeof(TaskRunner).Assembly.Location, typeof(TaskRunner).FullName);
                this.activeRunner.Setup();

                this.proxyLifetimeSponsor.Register(this.activeRunner);
            }

            Task.Run(() => {
                // this invocation can fail by to ways:
                // 1. forced shutdown of appdomain - ignore silently
                // 2. serialization exception related to type of result - pass to callee
                try {
                    var result = this.activeRunner.InvokeSynchronously(invocationRequest);
                    this.OnRunnerDone(result);
                } catch (SerializationException e) {
                    this.OnRunnerDone(new TaskFailedEventArgs(e));
                } finally {
                    this.Cleanup(true);
                }
            });

            Console.WriteLine(string.Format(
                                  CultureInfo.InvariantCulture, "{0} Started executing invocation request.",
                                  this.serverId));
        }
            public EventArgs InvokeSynchronously(
                InvocationRequest.PortableInvocationRequest portableInvocationRequest
                )
            {
                // Important: Calling parent.OnRunner* causes the app domain executing the current runner to be unloaded
                try {
                    this.cts.Token.ThrowIfCancellationRequested();
                    var result = portableInvocationRequest.ToInvocationRequest().Invoke(this.cts.Token);
                    return(new TaskSucceededEventArgs(result));
                } catch (OperationCanceledException e) {
                    if (e.CancellationToken == this.cts.Token)
                    {
                        return(new TaskCanceledEventArgs(true));
                    }

                    return(new TaskFailedEventArgs(e));
                } catch (Exception e) {
                    return(new TaskFailedEventArgs(e));
                }
            }
Beispiel #3
0
 public void InvokeAsync(InvocationRequest.PortableInvocationRequest invocationRequest, SecurityZone securityZone)
 {
     this.remoteProxy.InvokeAsync(invocationRequest, securityZone);
 }