一、问题描述:异常来自HRESULT0x8004021F

这是同事在做GeoDatabase方面的开发时遇到的问题,我拿到他的源代码分析一下,发现程序代码逻辑上并没有什么Bug。用百度搜索下,有且仅只找到两条相关记录,都是求救的贴子,下文毫无解决办法,看了等于白看,心里稍微安慰的是知道以前有人也遇到过类似的问题,算是前有来者了,自己并不孤独。可是这还是无济于事,问题依然还是问题。

二、问题分析:

下面是自己参考ArcGIS开发帮助文档写的创建FeatureClass的代码实例,调试时还是会弹出前面所描述的错误,用IFeatureDataset创建要素类时弹出的好像是license权限不够,估计是license没有签出的缘故。     

1 /// <summary>
  2        /// 创建要素类
  3        /// </summary>
  4        /// <param name="pObject">IWorkspace或者IFeatureDataset对象</param>
  5        /// <param name="pName">要素类名称</param>
  6        /// <param name="pSpatialReference">空间参考</param>
  7        /// <param name="pFeatureType">要素类型</param>
  8        /// <param name="pGeometryType">几何类型</param>
  9        /// <param name="pFields">字段集</param>
 10        /// <param name="pUidClsId">CLSID值</param>
 11        /// <param name="pUidClsExt">EXTCLSID值</param>
 12        /// <param name="pConfigWord">配置信息关键词</param>
 13        /// <returns>返回IFeatureClass</returns>

 14        public static IFeatureClass CreateFeatureClass(object pObject, string pName, ISpatialReference pSpatialReference, esriFeatureType pFeatureType,
 15                                       esriGeometryType pGeometryType, IFields pFields, UID pUidClsId, UID pUidClsExt, string pConfigWord)
 16        {
 17            #region 错误检测
 18            if (pObject == null)
 19            {
 20                throw (new Exception("[pObject] 不能为空!"));
 21            }

 22            if (!((pObject is IFeatureWorkspace) || (pObject is IFeatureDataset)))
 23            {
 24                throw (new Exception("[pObject] 必须为IFeatureWorkspace 或者 IFeatureDataset"));
 25            }

 26            if (pName.Length == 0)
 27            {
 28                throw (new Exception("[pName] 不能为空!"));
 29            }

 30            if ((pObject is IWorkspace) && (pSpatialReference == null))
 31            {
 32                throw (new Exception("[pSpatialReference] 不能为空(对于单独的要素类)"));
 33            }

 34            #endregion

 35
 36            #region pUidClsID字段为空时
 37            if (pUidClsId == null)
 38            {
 39                pUidClsId = new UIDClass();
 40                switch (pFeatureType)
 41                {
 42                    case (esriFeatureType.esriFTSimple):
 43                        if (pGeometryType == esriGeometryType.esriGeometryLine)
 44                            pGeometryType = esriGeometryType.esriGeometryPolyline;
 45                        pUidClsId.Value = "{52353152-891A-11D0-BEC6-00805F7C4268}";
 46                        break;
 47                    case (esriFeatureType.esriFTSimpleJunction):
 48                        pGeometryType = esriGeometryType.esriGeometryPoint;
 49                        pUidClsId.Value = "{CEE8D6B8-55FE-11D1-AE55-0000F80372B4}";
 50                        break;
 51                    case (esriFeatureType.esriFTComplexJunction):
 52                        pUidClsId.Value = "{DF9D71F4-DA32-11D1-AEBA-0000F80372B4}";
 53                        break;
 54                    case (esriFeatureType.esriFTSimpleEdge):
 55                        pGeometryType = esriGeometryType.esriGeometryPolyline;
 56                        pUidClsId.Value = "{E7031C90-55FE-11D1-AE55-0000F80372B4}";
 57                        break;
 58                    case (esriFeatureType.esriFTComplexEdge):
 59                        pGeometryType = esriGeometryType.esriGeometryPolyline;
 60                        pUidClsId.Value = "{A30E8A2A-C50B-11D1-AEA9-0000F80372B4}";
 61                        break;
 62                    case (esriFeatureType.esriFTAnnotation):
 63                        pGeometryType = esriGeometryType.esriGeometryPolygon;
 64                        pUidClsId.Value = "{E3676993-C682-11D2-8A2A-006097AFF44E}";
 65                        break;
 66                    case (esriFeatureType.esriFTDimension):
 67                        pGeometryType = esriGeometryType.esriGeometryPolygon;
 68                        pUidClsId.Value = "{496764FC-E0C9-11D3-80CE-00C04F601565}";
 69                        break;
 70                }

 71            }

 72            #endregion

 73
 74            #region pUidClsExt字段为空时
 75            if (pUidClsExt == null)
 76            {
 77                switch (pFeatureType)
 78                {
 79                    case esriFeatureType.esriFTAnnotation:
 80                        pUidClsExt = new UIDClass();
 81                        pUidClsExt.Value = "{24429589-D711-11D2-9F41-00C04F6BC6A5}";
 82                        break;
 83                    case esriFeatureType.esriFTDimension:
 84                        pUidClsExt = new UIDClass();
 85                        pUidClsExt.Value = "{48F935E2-DA66-11D3-80CE-00C04F601565}";
 86                        break;
 87                }

 88            }

 89            #endregion

 90
 91            #region 字段集合为空时
 92            if (pFields == null)
 93            {
 94                //实倒化字段集合对象
 95                pFields = new FieldsClass();
 96                IFieldsEdit tFieldsEdit = (IFieldsEdit)pFields;
 97
 98                //创建几何对象字段定义
 99                IGeometryDef tGeometryDef = new GeometryDefClass();
100                IGeometryDefEdit tGeometryDefEdit = tGeometryDef as IGeometryDefEdit;
101
102                //指定几何对象字段属性值
103                tGeometryDefEdit.GeometryType_2 = pGeometryType;
104                tGeometryDefEdit.GridCount_2 = 1;
105                tGeometryDefEdit.set_GridSize(01000);
106                if (pObject is IWorkspace)
107                {
108                    tGeometryDefEdit.SpatialReference_2 = pSpatialReference;
109                }

110
111                //创建OID字段
112                IField fieldOID = new FieldClass();
113                IFieldEdit fieldEditOID = fieldOID as IFieldEdit;
114                fieldEditOID.Name_2 = "OBJECTID";
115                fieldEditOID.AliasName_2 = "OBJECTID";
116                fieldEditOID.Type_2 = esriFieldType.esriFieldTypeOID;
117                tFieldsEdit.AddField(fieldOID);
118
119                //创建几何字段
120                IField fieldShape = new FieldClass();
121                IFieldEdit fieldEditShape = fieldShape as IFieldEdit;
122                fieldEditShape.Name_2 = "SHAPE";
123                fieldEditShape.AliasName_2 = "SHAPE";
124                fieldEditShape.Type_2 = esriFieldType.esriFieldTypeGeometry;
125                fieldEditShape.GeometryDef_2 = tGeometryDef;
126                tFieldsEdit.AddField(fieldShape);
127            }

128            #endregion

129
130            //几何对象字段名称
131            string strShapeFieldName = "";
132            for (int i = 0; i < pFields.FieldCount; i++)
133            {
134                if (pFields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry)
135                {
136                    strShapeFieldName = pFields.get_Field(i).Name;
137                    break;
138                }

139            }

140
141            if (strShapeFieldName.Length == 0)
142            {
143                throw (new Exception("字段集中找不到几何对象定义"));
144            }

145
146            IFeatureClass tFeatureClass = null;
147            if (pObject is IWorkspace)
148            {
149                //创建独立的FeatureClass
150                IWorkspace tWorkspace = pObject as IWorkspace;
151                IFeatureWorkspace tFeatureWorkspace = tWorkspace as IFeatureWorkspace;
152                tFeatureClass = tFeatureWorkspace.CreateFeatureClass(pName, pFields, pUidClsId, pUidClsExt, pFeatureType, strShapeFieldName, pConfigWord);
153            }

154            else if (pObject is IFeatureDataset)
155            {
156                //在要素集中创建FeatureClass
157                IFeatureDataset tFeatureDataset = (IFeatureDataset)pObject;
158                tFeatureClass = tFeatureDataset.CreateFeatureClass(pName, pFields, pUidClsId, pUidClsExt, pFeatureType, strShapeFieldName, pConfigWord);
159            }

160
161            return tFeatureClass;
162        }

三、问题解决方法:

经过分析确定是license权限不够,因此我们只需将与此相关的licenseesriLicenseProductCode.esriLicenseProductCodeEngineGeoDB签出即可。修改license初始化代码,签出相关的license授权,即解决了问题。