删除应用程序分区会抛出对象不存在的异常

时间:2012-07-24 04:04:18

标签: c# active-directory

关注the guidance posted on MSDN as to how to create an application partition我在删除刚刚创建的分区时遇到异常。以下示例针对我的用户帐户具有完全管理权限的AD LDS实例运行。有趣的是,如果在使用测试夹具之前存在分区,它会第一次工作。

var connectionString = new Uri("LDAPS://localhost/DC=integrationtests,DC=contoso,DC=com");
var baseContainerName = connectionString.PathAndQuery.TrimStart('/');
var hostName = IPGlobalProperties.GetIPGlobalProperties().HostName;
var directoryContext = new DirectoryContext(DirectoryContextType.DirectoryServer, hostName);
var configurationSet = ConfigurationSet.GetConfigurationSet(directoryContext);

var existingPartition =
                configurationSet.ApplicationPartitions.Cast<ApplicationPartition>().FirstOrDefault(
                    partition => baseContainerName.Equals(partition.Name, StringComparison.OrdinalIgnoreCase));
if (existingPartition != null)
{
    existingPartition.Delete();
}

// DirectoryEntry requires that the scheme is uppercase (LDAP not ldap)
var authenticationOptions = AuthenticationTypes.FastBind | AuthenticationTypes.Delegation | AuthenticationTypes.Secure;
var ldapConnectionString = String.Format(CultureInfo.InvariantCulture, "LDAP://{0}", hostName);
if (connectionString.Scheme.Equals("LDAPS", StringComparison.OrdinalIgnoreCase))
{
    authenticationOptions = authenticationOptions | AuthenticationTypes.SecureSocketsLayer;
    ldapConnectionString = ldapConnectionString + ":" + (connectionString.Port <= 0 ? "636" : connectionString.Port.ToString(CultureInfo.InvariantCulture));
}
using (var parent = new DirectoryEntry(ldapConnectionString, null, null, authenticationOptions))
{
    var partition = parent.Children.Add(baseContainerName, "domainDns");
    partition.Properties["instanceType"].Value = 5;
    partition.Properties["description"].Value = Assembly.GetExecutingAssembly().GetName().Name;
    partition.CommitChanges();
}

var existingPartition2 =
                configurationSet.ApplicationPartitions.Cast<ApplicationPartition>().FirstOrDefault(
                    partition => baseContainerName.Equals(partition.Name, StringComparison.OrdinalIgnoreCase));
if (existingPartition2 != null)
{
    // Throws here
    existingPartition2.Delete();
}

对existingPartition2.Delete()的调用抛出:

System.DirectoryServices.ActiveDirectory.ActiveDirectoryOperationException was unhandled
HResult=-2146233088
Message=There is no such object on the server.

Source=System.DirectoryServices
ErrorCode=-2147016656
StackTrace:
        at System.DirectoryServices.ActiveDirectory.ApplicationPartition.Delete()
        at LdapTest.Program.Main(String[] args) in d:\Projects\LdapTest\LdapTest\Program.cs:line 58
        at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
        at System.AppDomain.nExecuteAssembly(RuntimeAssembly assembly, String[] args)
        at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
        at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
        at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
        at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
        at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
        at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
        at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.DirectoryServices.DirectoryServicesCOMException
        HResult=-2147016656
        Message=There is no such object on the server.

        Source=System.DirectoryServices
        ErrorCode=-2147016656
        ExtendedError=8333
        ExtendedErrorMessage=0000208D: NameErr: DSID-03100213, problem 2001 (NO_OBJECT), data 0, best match of:
    'CN=Partitions,CN=Configuration,CN={CB88D356-F030-4598-BD59-F810587A2C72}'

        StackTrace:
            at System.DirectoryServices.DirectoryEntries.Remove(DirectoryEntry entry)
            at System.DirectoryServices.ActiveDirectory.ApplicationPartition.Delete()

有关可能导致第二次投掷的原因的任何想法?我可以在调用之前使用ADSIEdit在适当的位置看到crossRef对象。

1 个答案:

答案 0 :(得分:0)

在解决了Microsoft的问题(PSS案例#112072417632086)后,发现ConfigurationSet类维护了一个缓存,如果您进行了更改,则需要刷新。不是很明显,我知道......在第二个分区存在之前,检查调用刷新它:

configurationSet = ConfigurationSet.GetConfigurationSet(directoryContext.Value);