如何在visual studio 2010中为自定义签入策略创建PolicyEnvelope?

时间:2013-02-19 20:04:26

标签: tfs tfs2010 checkin-policy

我在visual studio 2010中为TFS 2010创建了一个自定义签入策略。该策略本身工作得很好。现在,我正在尝试从代码中应用自定义策略。以下是我的代码:

try
        {
            var projectCollectionUri = new Uri("https://mydomain.com/tfs");
            var projectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(projectCollectionUri, new UICredentialsProvider());
            projectCollection.EnsureAuthenticated();

            var vcs = projectCollection.GetService<VersionControlServer>();

            var teamProject = vcs.GetTeamProject("myProject");

            foreach (PolicyType policyType in Workstation.Current.InstalledPolicyTypes)
            {
                if (policyType.Name.ToLower().Contains("MyPolicyType"))
                {
                    //teamProject.SetCheckinPolicies(new PolicyEnvelope[] { new PolicyEnvelope(workItemPolicy, policyType) });
                    //break;
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

我被困在这一行:

//teamProject.SetCheckinPolicies(new PolicyEnvelope[] { new PolicyEnvelope(workItemPolicy, policyType) });

如何为自定义政策创建政策信封?我引用的代码来自以下位置: http://bartwullems.blogspot.com/2012/02/team-foundation-server-2010-enable.html

1 个答案:

答案 0 :(得分:1)

您只需将示例中的WorkItemPolicy替换为您的政策:

例如:

var myPolicy = new MyPolicy();

foreach (PolicyType policyType in Workstation.Current.InstalledPolicyTypes)
{
    if (policyType.Name == myPolicy.Type)
    {
        teamProject.SetCheckinPolicies(
            new PolicyEnvelope[]
            {
                new PolicyEnvelope(myPolicy, policyType)
            });
        break;
    }
}

如果您在控制台应用程序中没有包含策略的程序集,则可能需要添加对该程序集的引用。