ImageServiceLayer - Initialize with secure URL (HTTPS) throws generic exception

2237
4
02-16-2011 08:04 AM
JamesNyberg
New Contributor II
I am trying to Initialize a new ImageServerLayer with a secure (HTTPS) URL, but it throws the generic exception: "Error HRESULT E_FAIL has been returned from a call to a COM component."

Below is the code I am using, and two different URLs, both services work, but only the unsecure (HTTP) one will connect.

Type shpWkspFactType = typeof(ImageServerLayerClass);
string typeClsID = shpWkspFactType.GUID.ToString("B");
IImageServerLayer imageserverlayer = (IImageServerLayer)pObjFactory.Create(typeClsID);
imageserverlayer.Initialize(serviceUrl);

DOES work:        "http://AGSServer/arcgis/services/ISName/ImageServer"
DOES NOT work:  "https://AGSServer/arcgis/services/ISName/ImageServer"

(again, both services work, and I can manually add them via GIS Server connection in ArcMap, both Internet and LAN connections work)

Does anyone know how to connect to a secure ImageServiceLayer in ArcObjects?
0 Kudos
4 Replies
BillMajor
Esri Contributor
Did you ever determine a solution?  We are faced with the exact same problem.
0 Kudos
JamesNyberg
New Contributor II
Did you ever determine a solution?  We are faced with the exact same problem.


No, I dont think we did.
0 Kudos
WenxueJu
Esri Contributor
Please try the following code, which works for both http and https.

         IImageServerLayer isLayer =  CreateSecuredISLayer("https://myserver/arcgis/services", "sampleImage");
         CreateSecuredLayerFile(isLayer);

         private static IImageServerLayer CreateSecuredISLayer(string agsUrl, string serviceName)
        {
            IName soname = GetImageServerName(agsUrl, serviceName, false);
            IImageServerLayer isLayer = new ImageServerLayerClass();

            IDataLayer data_islayer = (IDataLayer)isLayer;
            data_islayer.DataSourceName = soname;
            return isLayer;
        }
        private static void CreateSecuredLayerFile(IImageServerLayer isLayer)
        {
            ILayerFile layerFile = new LayerFileClass();
            layerFile.New(@"c:\temp\datasoure.lyr");
            layerFile.ReplaceContents((ILayer)isLayer);
            layerFile.Save();
        }
        private static IName GetImageServerName(string hostOrUrl, string serviceName, bool isLAN)
        {
            IName pName = null;

            //set connection propertyset; host for LAN, url for Internet
            IPropertySet propertySet = new PropertySetClass();
            if (isLAN)
                propertySet.SetProperty("machine", hostOrUrl);
            else
                propertySet.SetProperty("url", hostOrUrl);
            //propertySet.SetProperty("user", userName);
            //propertySet.SetProperty("password", password);
           
            //open AGS connection
            Type factoryType = Type.GetTypeFromProgID("esriGISClient.AGSServerConnectionFactory");
            IAGSServerConnectionFactory agsFactory = (IAGSServerConnectionFactory)Activator.CreateInstance(factoryType);
            IAGSServerConnection agsConnection = agsFactory.Open(propertySet, 0);

            //get the image server
            IAGSEnumServerObjectName agsServerObjectNames = agsConnection.ServerObjectNames;
            agsServerObjectNames.Reset();
            IAGSServerObjectName agsServerObjectName = agsServerObjectNames.Next();
            while (agsServerObjectName != null)
            {
                if ((agsServerObjectName.Name.ToLower() == serviceName.ToLower()) && (agsServerObjectName.Type == "ImageServer"))
                {
                    pName = (IName)agsServerObjectName;
                   
                    break;
                }
                agsServerObjectName = agsServerObjectNames.Next();
            }

            //return the image server object
            return pName;
        }
0 Kudos
WenxueJu
Esri Contributor
An enhanced approach to get image server name object without opening it up (without providing username/password when creating layer):

private static IName GetImageServerName(string hostOrUrl, string serviceName)
{
IPropertySet propSet = new PropertySetClass();
propSet.SetProperty("url", hostOrUrl);
propSet.SetProperty("ANONYMOUS", false);
//don't hardcode user/password
//propertySet.SetProperty("user", userName);
//propertySet.SetProperty("password", password);
IAGSServerConnectionName agsServerConnectName = new AGSServerConnectionNameClass();
agsServerConnectName.ConnectionProperties = propSet;
IAGSServerObjectName agsSOName = new AGSServerObjectNameClass();
agsSOName.AGSServerConnectionName = agsServerConnectName;
agsSOName.Name = serviceName;
agsSOName.Type = "ImageServer";
return (IName)agsSOName;
}
0 Kudos