I am trying to write a batch geocoding application in ArcObjects/C# and keep getting the meaningless
"Error HRESULT E_FAIL has been returned from a call to a COM component"
error (error code -2147467259
), and I can't figure out what's causing it.
Any tips on how to figure out what the error is? I am running ArcGIS 9.3.1 SP 2
. Below is the code that I have. I get the error on the last line:
IRecordSet resultRecordSet = geocodeServer.GeocodeAddresses((IRecordSet)recordSetInit, addressProperties, null);
Here is the full code:
IGeocodeServer geocodeServer = null;
IGISServerConnection conn = null;
conn = new GISServerConnection();
conn.Connect("mandarin");
IServerObjectManager som = conn.ServerObjectManager;
IServerContext geocoderContext = som.CreateServerContext("StateCntyLoc", "GeocodeServer");
geocodeServer = (IGeocodeServer)geocoderContext.ServerObject;
IGeocodeServerObjects gom = (IGeocodeServerObjects)geocodeServer;
IGeocodingProperties locProp = (IGeocodingProperties)gom.AddressLocator;
locProp.SideOffset = 0;
locProp.EndOffset = 0;
IServerContext gdbContext = som.CreateServerContext("MF", "GeodataServer");
IGeoDataServer geoDataServer = (IGeoDataServer)gdbContext.ServerObject;
IGeoDataServerObjects gdsObjects = (IGeoDataServerObjects)geoDataServer;
IWorkspace gdsWS = gdsObjects.DefaultWorkingWorkspace;
IFeatureWorkspace featWS = (IFeatureWorkspace)gdsWS;
ITable addressTable = featWS.OpenTable("FACT_CRASH_INPUT");
IFields fields = geocodeServer.GetAddressFields();
// The Address Field Mapping is key = Locator Field Names and value = RecordSet Field Names
IPropertySet addressProperties = (IPropertySet)gdbContext.CreateObject("esriSystem.PropertySet");
addressProperties.SetProperty(fields.get_Field(0).Name, "INTERSECTION");
addressProperties.SetProperty(fields.get_Field(1).Name, "CNTY_CD");
IRecordSetInit recordSetInit = (IRecordSetInit)gdbContext.CreateObject("esriGeodatabase.RecordSet");
recordSetInit.SetSourceTable(addressTable, null);
IRecordSet resultRecordSet = geocodeServer.GeocodeAddresses((IRecordSet)recordSetInit, addressProperties, null);
Now if I put the table in a file geodatabase on my local machine and do the batch geocoding against ArcGS Online, I get back a record set.
IAoInitialize ao = new AoInitialize();
ao.Initialize(esriLicenseProductCode.esriLicenseProductCodeArcInfo);
IGeocodeServer geocodeServer = null;
IAGSServerConnection agsServerConnection = null;
IPropertySet connectionProperties = new PropertySet();
connectionProperties.SetProperty("Url", "http://tasks.arcgisonline.com/arcgis/services");
IAGSServerConnectionFactory serverConnectionFactory = new AGSServerConnectionFactory();
agsServerConnection = serverConnectionFactory.Open(connectionProperties, 0);
IAGSEnumServerObjectName serverObjectNames = agsServerConnection.ServerObjectNames;
serverObjectNames.Reset();
IName name;
IAGSServerObject serverObject = null;
IAGSServerObjectName serverObjectName = serverObjectNames.Next();
while (serverObjectName != null)
{
if (serverObjectName.Name == "Locators/TA_Address_NA" && serverObjectName.Type == "GeocodeServer")
{
name = serverObjectName as IName;
serverObject = name.Open() as IAGSServerObject;
if (serverObject as IGeocodeServer != null)
{
geocodeServer = serverObject as IGeocodeServer;
break;
}
}
serverObjectName = serverObjectNames.Next();
}
IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactory();
IWorkspace workspace = workspaceFactory.OpenFromFile(@"C:\Users\marni\Desktop\TestGC.gdb", 0);
IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace;
ITable addressTable = featureWorkspace.OpenTable("FACT_CRASH_INPUT");
IFields fields = geocodeServer.GetAddressFields();
// The Address Field Mapping is key = Locator Field Names and value = RecordSet Field Names
IPropertySet addressProperties = new PropertySet();
addressProperties.SetProperty(fields.get_Field(0).Name, "INTERSECTION");
addressProperties.SetProperty(fields.get_Field(1).Name, "CITY_CD");
addressProperties.SetProperty(fields.get_Field(2).Name, "CNTY_CD");
addressProperties.SetProperty(fields.get_Field(3).Name, DBNull.Value);
IRecordSetInit recordSetInit = (IRecordSetInit)new RecordSet();
recordSetInit.SetSourceTable(addressTable, null);
IRecordSet resultRecordSet = geocodeServer.GeocodeAddresses((IRecordSet)recordSetInit, addressProperties, null);