public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            // uninstall the instance
            var serviceInstance = BaristaHelper.GetBaristaServiceInstance(SPFarm.Local);

            if (serviceInstance != null)
            {
                serviceInstance.Delete();
                SPServer.Local.ServiceInstances.Remove(serviceInstance.Id);
            }

            // uninstall the service proxy
            var serviceProxy = BaristaHelper.GetBaristaServiceProxy(SPFarm.Local);

            if (serviceProxy != null)
            {
                serviceProxy.Delete();
                SPFarm.Local.ServiceProxies.Remove(serviceProxy.Id);
            }

            // uninstall the service
            var service = BaristaHelper.GetBaristaService(SPFarm.Local);

            if (service == null)
            {
                return;
            }

            if (service.Instances.Count != 0)
            {
                foreach (var remainingInstance in service.Instances)
                {
                    SPServer.Local.ServiceInstances.Remove(remainingInstance.Id);
                }
            }
            service.Delete();
            SPFarm.Local.Services.Remove(service.Id);
        }
        public Message Status()
        {
            var webContext = WebOperationContext.Current;

            if (webContext == null)
            {
                throw new InvalidOperationException("Current WebOperationContext is null.");
            }

            var result = new JObject();

            var environment = new JObject
            {
                { "commandLine", Environment.CommandLine },
                { "currentDirectory", Environment.CurrentDirectory },
                { "machineName", Environment.MachineName },
                { "newLine", Environment.NewLine },
                {
                    "osVersion", new JObject
                    {
                        { "platform", Environment.OSVersion.Platform.ToString() },
                        { "servicePack", Environment.OSVersion.ServicePack },
                        { "version", Environment.OSVersion.Version.ToString() },
                        { "versionString", Environment.OSVersion.VersionString }
                    }
                },
                { "processorCount", Environment.ProcessorCount },
                { "systemDirectory", Environment.SystemDirectory },
                { "tickCount", Environment.TickCount },
                { "userDomainName", Environment.UserDomainName },
                { "userInteractive", Environment.UserInteractive },
                { "userName", Environment.UserName },
                { "version", Environment.Version.ToString() },
                { "workingSet", Environment.WorkingSet },
                { "currentThreadId", System.Threading.Thread.CurrentThread.ManagedThreadId }
            };

            //try
            //{
            //    var cpuCounter = new PerformanceCounter
            //    {
            //        CategoryName = "Processor",
            //        CounterName = "% Processor Time",
            //        InstanceName = "_Total"
            //    };

            //    var firstValue = cpuCounter.NextValue();
            //    System.Threading.Thread.Sleep(1000);
            //    environment.Add("processorPct", cpuCounter.NextValue());

            //    PerformanceCounter memoryCounter = new PerformanceCounter
            //    {
            //        CategoryName = "Memory",
            //        CounterName = "Available MBytes"
            //    };

            //    environment.Add("availableMBytes", memoryCounter.NextValue());
            //}
            //catch {/* Do Nothing */}

            result.Add("environment", environment);

            var webContextObject = new JObject();

            var queryParameters        = new JObject();
            var requestQueryParameters = webContext.IncomingRequest.UriTemplateMatch.QueryParameters;

            foreach (var key in requestQueryParameters.AllKeys)
            {
                queryParameters.Add(key, requestQueryParameters[key]);
            }
            webContextObject.Add("queryParameters", queryParameters);

            var headers        = new JObject();
            var requestHeaders = webContext.IncomingRequest.Headers;

            foreach (var key in requestHeaders.AllKeys)
            {
                headers.Add(key, requestHeaders[key]);
            }
            webContextObject.Add("headers", headers);

            result.Add("webContext", webContextObject);

            var sharepoint = new JObject();

            var servers = new JArray();

            foreach (var spServer in SPFarm.Local.Servers)
            {
                JObject server;
                try
                {
                    server = new JObject
                    {
                        { "id", spServer.Id },
                        { "address", spServer.Address },
                        { "name", spServer.Name },
                        { "needsUpgrade", spServer.NeedsUpgrade },
                        //{"needsUpgradeIncludeChildren", spServer.NeedsUpgradeIncludeChildren},
                        { "version", spServer.Version },
                        { "role", spServer.Role.ToString() }
                    };
                }
                catch (Exception ex)
                {
                    server = new JObject
                    {
                        { "id", spServer.Id },
                        { "address", spServer.Address },
                        { "name", spServer.Name },
                        { "exception", ex.Message },
                        { "stackTrace", ex.StackTrace }
                    };
                }

                servers.Add(server);
            }

            sharepoint.Add("serversInFarm", servers);

            try
            {
                sharepoint.Add("SPBuildVersion", SPFarm.Local.BuildVersion.ToString());
                var baristaService = BaristaHelper.GetBaristaService(SPFarm.Local);
                sharepoint.Add("baristaService", GetSharePointServiceRepresentation(baristaService));
            }
            catch
            {
                //Do Nothing
            }

            try
            {
                var baristaSearchService = BaristaHelper.GetBaristaSearchService(SPFarm.Local);
                sharepoint.Add("baristaSearchService", GetSharePointServiceRepresentation(baristaSearchService));
            }
            catch
            {
                //Do Nothing
            }

            result.Add("sharepoint", sharepoint);


            //trusted locations config

            var    codeValue = BaristaServiceRequestPipeline.Grind(null, false);
            string scriptPath;
            var    whatIf = new JObject();

            whatIf.Add("codeValue", codeValue);
            whatIf.Add("codeContents", BaristaServiceRequestPipeline.Tamp(codeValue, out scriptPath));
            whatIf.Add("finalScriptPath", scriptPath);
            result.Add("whatIf", whatIf);

            return(webContext.CreateStreamResponse(
                       stream =>
            {
                var js = new JsonSerializer
                {
                    Formatting = Formatting.Indented
                };

                using (var sw = new StreamWriter(stream))
                {
                    js.Serialize(sw, result);
                }
            },
                       "application/json"));
        }