示例#1
0
        public static void RunClientSampleMethods(Uri connectionUrl, VssCredentials credentials, string area = null, string resource = null, DirectoryInfo outputPath = null)
        {
            if (area == "*")
            {
                area = null;
            }

            if (resource == "*")
            {
                resource = null;
            }

            Dictionary <ClientSample, IEnumerable <RunnableClientSampleMethod> > runnableMethodsBySample = GetRunnableClientSampleMethods(area, resource);

            if (!runnableMethodsBySample.Any())
            {
                Console.WriteLine("No samples found to run.");
            }
            else
            {
                ClientSampleContext context = new ClientSampleContext(connectionUrl, credentials);

                Console.WriteLine("Start running client samples...");
                Console.WriteLine("");
                Console.WriteLine("  URL     : {0}", connectionUrl);
                Console.WriteLine("  Area    : {0}", (area == null ? "(all)" : area));
                Console.WriteLine("  Resource: {0}", (resource == null ? "(all)" : resource));
                Console.WriteLine("  Output  : {0}", (outputPath == null ? "(disabled)" : outputPath.FullName));
                Console.WriteLine("");

                // Make sure we can connect before running the samples
                context.Connection.ConnectAsync().SyncResult();

                context.SetValue <DirectoryInfo>(ClientSampleHttpLogger.PropertyOutputFilePath, outputPath);

                foreach (var item in runnableMethodsBySample)
                {
                    ClientSample clientSample = item.Key;
                    clientSample.Context = context;

                    foreach (var runnableMethod in item.Value)
                    {
                        try
                        {
                            context.Log("+------------------------------------------------------------------------------+");
                            context.Log("| {0} |", String.Format("{0}/{1}", runnableMethod.MethodBase.Name, runnableMethod.MethodBase.DeclaringType.Name).PadRight(76));
                            context.Log("|                                                                              |");
                            context.Log("| API: {0} |", String.Format("{0}/{1}", runnableMethod.Area, runnableMethod.Resource).PadRight(71));
                            context.Log("+------------------------------------------------------------------------------+");
                            context.Log("");

                            // Set these so the HTTP logger has access to them when it needs to write the output
                            ClientSampleContext.CurrentRunnableMethod = runnableMethod;
                            ClientSampleContext.CurrentContext        = context;

                            // Reset suppression and operation name (don't want these values carrying over to the next sample method call)
                            ClientSampleHttpLogger.SetSuppressOutput(context, false);
                            ClientSampleHttpLogger.ResetOperationName(context);

                            object result = runnableMethod.MethodBase.Invoke(clientSample, null);
                            // if the runnable method is async, then wait for the completion
                            if (result is Task resultTask)
                            {
                                resultTask.SyncResult();
                            }
                        }
                        catch (Exception ex)
                        {
                            //the innermost exception is the interesting one
                            while (ex.InnerException != null)
                            {
                                ex = ex.InnerException;
                            }
                            Console.WriteLine("FAILED! With exception: " + ex.Message);
                        }
                        finally
                        {
                            context.Log("");
                        }
                    }
                }
            }
        }
 public void Dispose()
 {
     ClientSampleHttpLogger.SetSuppressOutput(ClientSampleContext.CurrentContext, OriginalSuppressValue);
 }