コード例 #1
0
 public void Dispose()
 {
     vssConnection?.Dispose();
     taskClient?.Dispose();
     vssConnection = null;
     taskClient    = null;
 }
コード例 #2
0
            public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
            {
                ContentResult result = new ContentResult();

                result.ContentType = "text/plain";

                if (context.HttpContext.Request.Headers.Any(t => t.Key == "Authorization"))
                {
                    KeyValuePair <string, StringValues> keyValue = context.HttpContext.Request.Headers.FirstOrDefault(t => t.Key == "Authorization");
                    string   token     = keyValue.Value;
                    string[] tokenData = token.Split(' ');
                    token = tokenData[1];
                    var      base64EncodedBytes = Convert.FromBase64String(token);
                    string   data  = Encoding.UTF8.GetString(base64EncodedBytes);
                    string[] parts = data.Split("|");

                    MyVssConnection vssConnection = new MyVssConnection(parts[0], parts[1], parts[2], parts[3], parts[4]);
                    VssConnection   connection    = vssConnection.GetConnection();
                    try
                    {
                        if (connection.AuthorizedIdentity.IsActive)
                        {
                            string format = "text/plain";
                            if (context.HttpContext.Request.Headers["Content-Type"] != "text/plain")
                            {
                                format = context.HttpContext.Request.Headers["Content-Type"];
                                if (format == null)
                                {
                                    format = "text/plain";
                                }
                            }
                            context.HttpContext.User = new TfsClaimsPrincipal(new TfsIdentity(vssConnection, tokenData[0], parts), format);
                            await next();

                            connection.Dispose();
                        }
                    } catch (Exception)
                    {
                        result.StatusCode = 401;
                        context.Result    = result;
                    }
                }
                else
                {
                    result.StatusCode = 401;
                    context.Result    = result;
                }
            }
コード例 #3
0
        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    projectHttpClient.Dispose();
                    projectHttpClient.Dispose();
                    vSSConnection.Dispose();
                }

                projectHttpClient = null;
                projectHttpClient = null;
                vSSConnection     = null;

                disposedValue = true;
            }
        }
コード例 #4
0
        public void Disconnect()
        {
            _url = null;
            _availableProjects = null;

            if (_client != null)
            {
                _client.Dispose();
                _client = null;
            }

            if (_connection != null)
            {
                _connection.Disconnect();
                _connection.Dispose();
                _connection = null;
            }

            Disconnected?.Invoke(this);
        }
コード例 #5
0
ファイル: RunnerServer.cs プロジェクト: Tryweirder/gha-runner
        // Refresh connection is best effort. it should never throw exception
        public async Task RefreshConnectionAsync(RunnerConnectionType connectionType, TimeSpan timeout)
        {
            Trace.Info($"Refresh {connectionType} VssConnection to get on a different AFD node.");
            VssConnection newConnection = null;

            switch (connectionType)
            {
            case RunnerConnectionType.MessageQueue:
                try
                {
                    _hasMessageConnection = false;
                    newConnection         = await EstablishVssConnection(_messageConnection.Uri, _messageConnection.Credentials, timeout);

                    var client = newConnection.GetClient <TaskAgentHttpClient>();
                    _messageConnection      = newConnection;
                    _messageTaskAgentClient = client;
                }
                catch (Exception ex)
                {
                    Trace.Error($"Catch exception during reset {connectionType} connection.");
                    Trace.Error(ex);
                    newConnection?.Dispose();
                }
                finally
                {
                    _hasMessageConnection = true;
                }
                break;

            case RunnerConnectionType.JobRequest:
                try
                {
                    _hasRequestConnection = false;
                    newConnection         = await EstablishVssConnection(_requestConnection.Uri, _requestConnection.Credentials, timeout);

                    var client = newConnection.GetClient <TaskAgentHttpClient>();
                    _requestConnection      = newConnection;
                    _requestTaskAgentClient = client;
                }
                catch (Exception ex)
                {
                    Trace.Error($"Catch exception during reset {connectionType} connection.");
                    Trace.Error(ex);
                    newConnection?.Dispose();
                }
                finally
                {
                    _hasRequestConnection = true;
                }
                break;

            case RunnerConnectionType.Generic:
                try
                {
                    _hasGenericConnection = false;
                    newConnection         = await EstablishVssConnection(_genericConnection.Uri, _genericConnection.Credentials, timeout);

                    var client = newConnection.GetClient <TaskAgentHttpClient>();
                    _genericConnection      = newConnection;
                    _genericTaskAgentClient = client;
                }
                catch (Exception ex)
                {
                    Trace.Error($"Catch exception during reset {connectionType} connection.");
                    Trace.Error(ex);
                    newConnection?.Dispose();
                }
                finally
                {
                    _hasGenericConnection = true;
                }
                break;

            default:
                Trace.Error($"Unexpected connection type: {connectionType}.");
                break;
            }
        }
コード例 #6
0
        public WorkItem ValidateWorkItem()
        {
            //Setup
            //In order for your wit to fail on validation, you need either set the field required property or create a conditional rule to make a field required

            //Scenerio
            //to determine the work item type you are creating can be saved with the fields set in the patch document
            //first: check to see if there are any requried fields
            //second: do a save using validateOnly flag. this will run the save against the rules and return any validation errors

            string projectName = "temp";

            // Get a client
            VssConnection connection = Context.Connection;
            WorkItemTrackingHttpClient workItemTrackingClient = connection.GetClient <WorkItemTrackingHttpClient>();

            //get the work item type
            WorkItemType workItemType = workItemTrackingClient.GetWorkItemTypeAsync(projectName, "Task").Result;

            //get a list of all of the required fields
            List <WorkItemTypeFieldInstance>        fields    = (List <WorkItemTypeFieldInstance>)workItemType.Fields;
            IEnumerable <WorkItemTypeFieldInstance> reqFields = fields.Where(x => x.AlwaysRequired == true && String.IsNullOrEmpty(x.DefaultValue));

            Console.WriteLine("Required Fields...");

            foreach (WorkItemTypeFieldInstance field in reqFields)
            {
                Console.WriteLine("  {0}", field.ReferenceName);
            }

            Console.WriteLine("");

            // Construct the object containing field values required for the new work item
            JsonPatchDocument patchDocument = new JsonPatchDocument();

            patchDocument.Add(
                new JsonPatchOperation()
            {
                Operation = Operation.Add,
                Path      = "/fields/System.Title",
                Value     = "Sample task 1"
            }
                );

            try
            {
                // validate the patch document when trying to create a work item
                WorkItem newWorkItem = workItemTrackingClient.CreateWorkItemAsync(patchDocument, projectName, "Task", true).Result;
            }
            //get the list of rule validation exceptions when there are only rule validation errors
            catch (Exception ruleValidationException) when(ruleValidationException.InnerException.Message.Contains("TF401320:"))
            {
                IEnumerable <RuleValidationException> ruleValidationErrors = ((RuleValidationException)ruleValidationException.InnerException).RuleValidationErrors;

                Console.WriteLine("Found the following validation errors...");

                foreach (RuleValidationException ruleValidationError in ruleValidationErrors)
                {
                    Console.WriteLine("  {0}", ruleValidationError.ErrorMessage);
                }
            }
            catch (Exception otherException)
            {
                Console.WriteLine("Other Exceptions Found:");
                Console.WriteLine(otherException.InnerException.Message);
            }

            patchDocument = null;
            connection.Dispose();
            connection = null;
            workItemTrackingClient.Dispose();
            workItemTrackingClient = null;

            return(null);
        }
コード例 #7
0
 public void Dispose()
 {
     vssConnection.Dispose();
 }