//private ProjectState State;

        //private Task WriteStateAsync()
        //{
        //	return TaskDone.Done;
        //}

        //private Task ClearStateAsync()
        //{
        //	return TaskDone.Done;
        //}

        public override async Task OnActivateAsync()
        {
            //this.State = new ProjectState();

            await StatsHelper.RegisterActivation("ProjectCodeProviderGrain", this.GrainFactory);

            Logger.OrleansLogger = this.GetLogger();
            Logger.LogInfo(this.GetLogger(), "ProjectGrain", "OnActivate", "Enter");

            // Logger.LogWarning(this.GetLogger(), "ProjectGrain", "OnActivate", "Entering Project: {0}", this.GetPrimaryKeyString());

            this.observers          = new ObserverSubscriptionManager <IEntityGrainObserverNotifications>();
            this.State.AssemblyName = this.GetPrimaryKeyString();

            //Task.Run(async () =>
            //await Task.Factory.StartNew(async () =>
            //{
            try
            {
                this.RaiseStateChangedEvent(EntityGrainStatus.Busy);

                if (!string.IsNullOrEmpty(this.State.ProjectPath))
                {
                    this.projectCodeProvider = await OrleansProjectCodeProvider.CreateFromProjectAsync(this.GrainFactory, this.State.ProjectPath);
                }
                else if (!string.IsNullOrEmpty(this.State.Source) && !String.IsNullOrEmpty(this.State.AssemblyName))
                {
                    this.projectCodeProvider = await OrleansProjectCodeProvider.CreateFromSourceAsync(this.GrainFactory, this.State.Source, this.State.AssemblyName);
                }
                else if (!string.IsNullOrEmpty(this.State.TestName) && !String.IsNullOrEmpty(this.State.AssemblyName))
                {
                    this.projectCodeProvider = await OrleansProjectCodeProvider.CreateFromTestAsync(this.GrainFactory, this.State.TestName, this.State.AssemblyName);
                }
                else if (this.State.AssemblyName.Equals("DUMMY"))
                {
                    this.projectCodeProvider = new OrleansDummyProjectCodeProvider(this.GrainFactory);

                    await this.WriteStateAsync();
                }

                this.RaiseStateChangedEvent(EntityGrainStatus.Ready);
            }
            catch (Exception ex)
            {
                var inner = ex;
                while (inner is AggregateException)
                {
                    inner = inner.InnerException;
                }

                Logger.LogError(this.GetLogger(), "ProjectGrain", "OnActivate", "Error:\n{0}\nInner:\n{1}", ex, inner);
                throw ex;
            }
            //});

            Logger.LogInfo(this.GetLogger(), "ProjectGrain", "OnActivate", "Exit");
        }
        public static async Task <OrleansProjectCodeProvider> CreateFromTestAsync(IGrainFactory grainFactory, string testName, string assemblyName)
        {
            var solutionGrain = OrleansSolutionManager.GetSolutionGrain(grainFactory);
            var rtaGain       = OrleansRtaManager.GetRtaGrain(grainFactory);
            var provider      = new OrleansProjectCodeProvider(grainFactory, solutionGrain, rtaGain);
            await provider.LoadTestAsync(testName, assemblyName);

            return(provider);
        }
        public static async Task <OrleansProjectCodeProvider> CreateFromProjectAsync(IGrainFactory grainFactory, string projectPath)
        {
            var solutionGrain = OrleansSolutionManager.GetSolutionGrain(grainFactory);
            var rtaGain       = OrleansRtaManager.GetRtaGrain(grainFactory);
            var provider      = new OrleansProjectCodeProvider(grainFactory, solutionGrain, rtaGain);
            await provider.LoadProjectAsync(projectPath);

            return(provider);
        }
        private IProjectCodeProvider GetDummyProjectCodeProvider()
        {
            // TODO: In the future we may want to have a different project code provider
            // for each unknown project in the solution instead of having only one
            // representing all of them.
            var provider = OrleansProjectCodeProvider.GetProjectGrain(grainFactory, "DUMMY");

            this.Assemblies.Add("DUMMY");
            return(provider);
        }
        public async Task ForceDeactivationOfProjects()
        {
            var tasks = new List <Task>();

            foreach (var assemblyName in this.Assemblies)
            {
                var provider = OrleansProjectCodeProvider.GetProjectGrain(grainFactory, assemblyName);
                var task     = provider.ForceDeactivationAsync();
                //await task;
                tasks.Add(task);
            }

            await Task.WhenAll(tasks);
        }
        public override Task <IProjectCodeProvider> GetProjectCodeProviderAsync(AssemblyName assemblyName)
        {
            IProjectCodeProvider provider = null;
            var isExistingProject         = this.Assemblies.Contains(assemblyName);

            if (isExistingProject)
            {
                provider = OrleansProjectCodeProvider.GetProjectGrain(grainFactory, assemblyName);
            }
            else
            {
                provider = this.GetDummyProjectCodeProvider();
                //this.ProjectProviders.Add(assemblyName);
            }

            return(Task.FromResult(provider));
        }
        public async Task SetProjectPathAsync(string fullPath)
        {
            //await StatsHelper.RegisterMsg("ProjectGrain::SetProjectPath", this.GrainFactory);
            await StatsHelper.RegisterMsg("ProjectGrain::SetProjectPath:" + fullPath, this.GrainFactory);

            Logger.LogInfo(this.GetLogger(), "ProjectGrain", "SetProjectPath", "Enter:" + fullPath);

            this.State.ProjectPath  = fullPath;
            this.State.AssemblyName = null;
            this.State.Source       = null;
            this.State.TestName     = null;

            await this.WriteStateAsync();

            //Task.Run(async () =>
            //await Task.Factory.StartNew(async () =>
            //{
            try
            {
                this.RaiseStateChangedEvent(EntityGrainStatus.Busy);

                this.projectCodeProvider = await OrleansProjectCodeProvider.CreateFromProjectAsync(this.GrainFactory, this.State.ProjectPath);

                this.RaiseStateChangedEvent(EntityGrainStatus.Ready);
            }
            catch (Exception ex)
            {
                var inner = ex;
                while (inner is AggregateException)
                {
                    inner = inner.InnerException;
                }

                Logger.LogError(this.GetLogger(), "ProjectGrain", "SetProjectPath", "Error:\n{0}\nInner:\n{1}", ex, inner);
                throw ex;
            }
            //});

            Logger.LogInfo(this.GetLogger(), "ProjectGrain", "SetProjectPath", "Exit");
        }