Select to view content in your preferred language

Custom Print Service fails to Print "Failed to create layer from service at "<Service_URL>" for Hosted Server

4150
15
08-12-2023 11:17 PM
ArchitSrivastava
Frequent Contributor

Hello,

We are working with a custom Print service created from ArcGIS Pro to allow printing of Secured service from different Servers. However, we encounter the issue when trying to print the secured services from the ArcGIS Server which on which the service is published.

We have configured the Print Service by following this "HOW TO" article and it works well with other server services.

As soon as I remove the service from that server everything work smoothly with any issues. We have tried several things, it still fails. Furthermore, if the service is created from ArcMap and then published everything works as expected.

We have also installed Print service patches for this version.

Environment details:

  • ArcGIS Enterprise 10.8.1
  • All components are installed on one machine except Web Adaptor

It would be great if I can some thoughts on this.

0 Kudos
15 Replies
StefanStamenković
Occasional Contributor

Hi,

I'm also struggling with this issue and I'm desperate right now...

I have published secured service from ArcGIS Pro 2.9.11 to ArcGIS Server 10.9.1 (as they should be compatible) where I have also installed ArcGIS Server Map and Feature Service Security 2023 Update 1 Patch (as there this bug should be fixed), included my credentials in script and everything, but still doesn't manage to print my secured service.

Tried it as local ArcGIS Pro tool and it works fine, but after publishing it as geoprocessing tool to my server I get common error:  Failed to create layer from service at https://.../arcgis/rest/services/.../MapServer

Is there anything else I can do about it?

0 Kudos
David_McRitchie
Esri Contributor

Hey Stefan,

I would recommend raising a Support Case with your Esri distributor for assistance, as print cases can be fairly tricky.

I would recommend checking other patches that are available, as there are some print specific patches for 10.9.1 such as the following which may assist.

For testing I would also ensure that your Web Map just contains a basemap and layers that are stored on the default datastore, as this can rule out a lot of permission errors that can arise when the ArcGIS Server account doesn't have internet access.

Hope that helps,

David

Esri UK -Technical Support Analyst
0 Kudos
StefanStamenković
Occasional Contributor

Hi David,

Thank you for your answer. Mentioned patch has also been already installed and it doesn't seem to help as problem still persists.

I'm testing print service with basemap and two services with data in Enterprise database, one secured and other one not. As I mentioned, services are printing fine while using tool in Toolbox. After publishing and running GP tool I'm getting Error although I'm using the same WebMapJson, but even if I unlock services and update WebMapJson to print unsecured services, I'm not getting the Error but services don't get printed.

If anyone has other advice, I'll be looking forward to try with it.

0 Kudos
ArchitSrivastava
Frequent Contributor

Hello @StefanStamenković ,

Hope you are keeping safe and well. Could you please share more details mentioned below:

  • What is the Authentication Mode we are working with "Web-tier authentication" or "Portal-tier authentication"?
  • Can e change the logging level of Geoprocessing service to "Info" and then check what information is generated in those logs? For more details : Enabling info-level geoprocessing messages
  • I am assuming that this runs perfectly fine on ArcGIS Pro, no waring messages at all?

Additionally, could you share a snippet of the .py file you have generated and published.

Hope it helps!

-Archit

0 Kudos
StefanStamenković
Occasional Contributor

Hi @ArchitSrivastava,

Sorry for delayed answer.

  • As we are using stand-alone server, I suppose it is web-tier authentication.
  • I managed to enable info-level messages for geoprocessing tool but it doesn't give me clearer insight what happens except it crashes while converting webmap to a map document and executes exception.
  • Yes, in ArcGIS Pro it executes without any errors, after publishing it as geoprocessing tool and running it I receive error: Failed to create layer from service at https://.../MapServer and it is secured serviceasdf

Please find below content of my .py file.

import sys
import os
import arcpy
import uuid

# constants
SERVER_PROD_NAME = 'Server'
PRO_PROD_NAME = 'ArcGISPro'
PAGX_FILE_EXT = "pagx"
MAP_ONLY = 'map_only'

# Specify credentials for secured services in the WebMap JSON
secure_server_connections = [r'path\to\ags\file.ags']

# Import credentials
importedConnections = arcpy.ImportCredentials(secure_server_connections)

# default location and current product name
#
_defTmpltFolder = os.path.join(arcpy.GetInstallInfo()['InstallDir'], r"Resources\ArcToolBox\Templates\ExportWebMapTemplates")
_prodName = arcpy.GetInstallInfo()['ProductName']
_isMapOnly = False

# export only map without any layout elements
def exportMap(result, outfile, outFormat):
    mapView = result.ArcGISProject.listMaps()[0].defaultView

    w = result.outputSizeWidth
    h = result.outputSizeHeight
    dpi = int(result.DPI) #a workaround for now for a bug

    try:
        if outFormat == "png8" or outFormat == "png32":
            if (outFormat == "png8"):
                colorMode = "8-BIT_ADAPTIVE_PALETTE"
            else:
                colorMode = "32-BIT_WITH_ALPHA"
            mapView.exportToPNG(outfile, w, h, dpi, None, colorMode)
        elif outFormat == "pdf":
            mapView.exportToPDF(outfile, w, h, dpi)
        elif outFormat == "jpg":
            mapView.exportToJPEG(outfile, w, h, dpi, None, '24-BIT_TRUE_COLOR', 100)
        elif outFormat == "gif":
            mapView.exportToGIF(outfile, w, h, dpi)
        elif outFormat == "eps":
            mapView.exportToEPS(outfile, w, h, dpi)
        elif outFormat == "svg":
            mapView.exportToSVG(outfile, w, h, dpi, False)
        elif outFormat == "svgz":
            mapView.exportToSVG(outfile, w, h, dpi, True)
        elif outFormat == "aix":
            mapView.exportToAIX(outfile, w, h, dpi)
        elif outFormat == "tiff":
            mapView.exportToTIFF(outfile, w, h, dpi, False, "32-BIT_WITH_ALPHA", "DEFLATE", True) #return geoTIFF_tags
    except Exception as err:
        arcpy.AddError("error raised..." + str(err))
        raise
    
# export layout

def exportLayout(result, outfile, outFormat):
    layout = result.ArcGISProject.listLayouts()[0]
    dpi = result.DPI

    try:
        if outFormat == "png8" or outFormat == "png32":
            if (outFormat == "png8"):
                colorMode = "8-BIT_ADAPTIVE_PALETTE"
            else:
                colorMode = "32-BIT_WITH_ALPHA"
            layout.exportToPNG(outfile, dpi, colorMode)
        elif outFormat == "pdf":
            layout.exportToPDF(outfile, dpi)
        elif outFormat == "jpg":
            layout.exportToJPEG(outfile, dpi)
        elif outFormat == "gif":
            layout.exportToGIF(outfile, dpi)
        elif outFormat == "eps":
            layout.exportToEPS(outfile, dpi)
        elif outFormat == "svg":
            layout.exportToSVG(outfile, dpi, False)
        elif outFormat == "svgz":
            layout.exportToSVG(outfile, dpi, True)
        elif outFormat == "aix":
            layout.exportToAIX(outfile, dpi)
        elif outFormat == "tiff":
            layout.exportToTIFF(outfile, dpi, "32-BIT_WITH_ALPHA", "DEFLATE")
    except Exception as err:
        arcpy.AddError("error raised..." + str(err))
        raise

# generating a unique name for each output file
def generateUniqueFileName(outFormat):
    guid = str(uuid.uuid1())
    fileName = ""
    fileExt = outFormat

    #changing the file extension for few instances
    if outFormat == "png8" or outFormat == "png32":
        fileExt = "png"
    elif outFormat == "tiff":
        fileExt = "tif"

    fileName = '{}.{}'.format(guid, fileExt)
    fullFileName = os.path.join(arcpy.env.scratchFolder, fileName)
    return fullFileName

# Main module
def main():
    # Get the value of the input parameter
    WebMap_as_JSON = arcpy.GetParameterAsText(0)
    outfilename = arcpy.GetParameterAsText(1)
    format = arcpy.GetParameterAsText(2).lower()
    layoutTemplatesFolder = arcpy.GetParameterAsText(3).strip()
    layoutTemplate = arcpy.GetParameterAsText(4).lower()

    if (layoutTemplate.lower() == MAP_ONLY):
        _isMapOnly = True
        layoutTemplate = None
    else:
        _isMapOnly = False

    # Special logic while being executed in ArcGIS Pro 
    # - so that a Geoprocessing result can be acquired without needing any json to begin to feed in
    # - this is to make the publishing experience easier
    if (WebMap_as_JSON.replace(' ', '') == '#'):
        WebMap_as_JSON = '#'
        if (_prodName == PRO_PROD_NAME):
            return
        elif (_prodName == SERVER_PROD_NAME):
            arcpy.AddIDMessage('ERROR', 590, 'WebMap_as_JSON')
        else:
            arcpy.AddIDMessage('ERROR', 120004, _prodName)

    # generate a new output filename when the output_filename parameter is empty or the script is running on server
    if outfilename.isspace() or _prodName == SERVER_PROD_NAME:
        outfilename = generateUniqueFileName(format)

    # constructing the full path for the layout file (.pagx)
    if not _isMapOnly:
        # use the default location when Layout_Templates_Folder parameter is not set
        tmpltFolder = _defTmpltFolder if not layoutTemplatesFolder else layoutTemplatesFolder
        layoutTemplate = os.path.join(tmpltFolder, '{}.{}'.format(layoutTemplate, PAGX_FILE_EXT))

    #Convert the webmap to a map document
    try:
        result = arcpy.mp.ConvertWebMapToArcGISProject(WebMap_as_JSON, layoutTemplate)
        
        #Export...
        if (_isMapOnly):
            if (result.outputSizeWidth == 0) or (result.outputSizeHeight == 0):
                arcpy.AddIDMessage('ERROR', 1305)
            exportMap(result, outfilename, format)
        else:
            exportLayout(result, outfilename, format)

        # Clear credentials
        arcpy.ClearCredentials(importedConnections)
        
    except Exception as err:
        arcpy.AddError(str(err))

    # Set output parameter
    #
    arcpy.SetParameterAsText(1, outfilename)

if __name__ == "__main__":
    main()

Thank you in advance!

 

0 Kudos
ArchitSrivastava
Frequent Contributor

Hello @StefanStamenković ,

Thanks for sharing the details. While I analyze the script you have shared as it seems a bit modified than usual. I would like to bring the following to attention and consideration:

  • Can you confirm that you are using Web-Tier to check that:
    • Navigate to ArcGIS Server Manager > Security > What you see under configuration settings
  • Confirm the Version of ArcGIS Server you are working with
  • Make Sure that ArcGIS Server Connection is "User Connection"
  • Can we try the basic script and steps recommended by ESRI in the following article, this will help us make sure if the modifications are causing any problems:

Here are some initial tests I ran:

  • Configured you script at my end with Sample Server 6 credentials ags file (as I do not have standalone server) (Made sure that the connection Type is USER)
  • Made some basic changes in script to help me understand things better and handle things better (mentioned below)
  • Used "Web Map as JSON" from ESRI article 
  • Published it as a Web Tool To Portal for ArcGIS (11.1)
  • Added a secured Hosted Service and a Secured service from Sample Server 6 and ran Print in Web App Builder Widget and it seems to work

Changes Made in the script (very-initial) (attached updated):

  • Wrapped some parts of code in 'try-except' to catch specific exceptions that might occur during execution.
  • Added exception handling for both map export and layout export functions

Additionally, I think "Web-Tier Authentication" could be the culprit here, Can you check this post about ArcGIS Server Windows Service account with IWA.

It would be great if you can try the attached script, publish it as geoprocessing service, Set the logging level for Geoprocessing service and try to run the tool and what happens.

Hope it helps!

-Archit

0 Kudos