public IProcedure GetProcedure(Project project, Build build)
        {
            var assembly = GetAssembly(project, build);
            var packageName = project.DeployPackageName;

            var procedures = assembly.GetTypes().Where(x => typeof(IProcedure).IsAssignableFrom(x) && (x.Name == project.ProcedureName || x.FullName == project.ProcedureName)).ToList();
            if (!procedures.Any())
            {
                throw new CouldNotLoadProcedureException(string.Format("Error: Could not find any procedure named {0} in deploy package named {1}", project.ProcedureName, packageName));
            }

            if (procedures.Count > 1)
            {
                throw new CouldNotLoadProcedureException(string.Format("Error: Found more than one matching procedure in deploy package named {0}", packageName));
            }

            var procedureType = procedures.Single();

            var procedure = CreateProcedure(procedureType);
            if (procedure == null)
            {
                throw new CouldNotLoadProcedureException(string.Format("Error: Procedure found in deploy package named {0}, does not have parameterless constructor or a constructor taking an IMessenger", packageName));
            }

            return procedure;
        }
        public Assembly GetAssembly(Project project, Build build)
        {
            var packageName = project.DeployPackageName;

            var package = build.GetPackage(packageName);
            if (package == null)
            {
                throw new CouldNotLoadAssemblyException(string.Format("Could not find any deploy package named {0}", packageName));
            }

            var filesInPackage = Zip.Extract(File.Open(package.Path, FileMode.Open));
            var assemblyFileName = project.DeployPackageName + ".dll";
            var assemblyFile = filesInPackage.SingleOrDefault(x => x.Filename == assemblyFileName);
            if (assemblyFile == null)
            {
                throw new CouldNotLoadAssemblyException(string.Format("Could not find assemblyfile named {0} in package named {1}", assemblyFileName, packageName));
            }

            var assembly = Assembly.Load(assemblyFile.Data);
            if (assembly == null)
            {
                throw new CouldNotLoadAssemblyException(packageName);
            }

            return assembly;
        }
        string GetRawPackage(Build build)
        {
            string packageUrl = string.Format("{0}/downloadArtifacts.html?buildId={1}", teamcityUrl, build.Id);
            WebRequest request = WebRequest.Create(packageUrl);
            string authInfo = "larsudengaard:MetteP83";
            authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
            request.Headers["Authorization"] = "Basic " + authInfo;

            request.Credentials = new NetworkCredential("larsudengaard", "MetteP83");

            WebResponse response;
            try
            {
                response = request.GetResponse();
            }
            catch (WebException e)
            {
                return null;
            }

            Stream responseStream = response.GetResponseStream();
            if (response.ContentLength == 0 || responseStream == null)
                return null;

            string fileName = packagePath + build.Id + ".zip";
            using (FileStream outputFileStream = File.Open(fileName, FileMode.CreateNew))
            using (responseStream)
            {
                var buffer = new byte[1024];
                int bytesRead;
                do
                {
                    bytesRead = responseStream.Read(buffer, 0, 1024);
                    outputFileStream.Write(buffer, 0, bytesRead);
                } while (bytesRead > 0);
            }

            return fileName;
        }
        public Package GetPackage(Build build, string packageName)
        {
            string packagesPath = packagePath + build.Id;
            if (!Directory.Exists(packagesPath))
            {
                Directory.CreateDirectory(packagesPath);
                var packageFilename = GetRawPackage(build);
                Zip.Extract(File.Open(packageFilename, FileMode.Open), packagesPath);
            }

            string childPackageFilename = packagesPath + "\\" + packageName + ".zip";
            if (!File.Exists(childPackageFilename))
                return null;

            return new Package
            {
                Name = packageName,
                Path = childPackageFilename
            };
        }