ApprenticeServer as Windows Service

ApprenticeServer as Windows Service

DynamicObjects
Advocate Advocate
2,013 Views
13 Replies
Message 1 of 14

ApprenticeServer as Windows Service

DynamicObjects
Advocate
Advocate

Hello Autodesk and forum

 

 

I am attempting to retrieve the thumbnails for part files like this ...

 

 

// use ApprenticeServerComponent to run In-Process to obtain thumbnail
   oApprenticeServerDoc = oApprenticeApp.Open(component.LocalFilenameAndPath);  

// obtain the thumbnail image
  Console.WriteLine("attempting to get thumbnail for " + oApprenticeServerDoc.FullFileName);
  stdole.IPictureDisp thumbnailIPD = oApprenticeServerDoc.Thumbnail;

 

The final line fails when running in the WindowsService. ...

 

The above code sits in a method that I use in a Standard Windows Forms App exe, and also in my process that is started and run as a windows service.

 

It works well for the WindowsFormsApp.

The final line above fails when running in the WindowsService with the exception ... 

 

Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED)) at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData) at Inventor.ApprenticeServerDocument.get_Thumbnail()

 

  

Is there an issue with running ApprenticeServer as a Windows Service --- can anyone confirm whether this is the likely cause of the above failure??

 

 

Regards

Dan

 

 

------------------------------------------------------------------------------------------------------------
Atkins Consultants Ltd UK
+19,000 staff (M&E, Civils, Structural, Geotechnical, Environmental and more)
0 Likes
Accepted solutions (2)
2,014 Views
13 Replies
Replies (13)
Message 2 of 14

YuhanZhang
Autodesk
Autodesk

Is that an out-of-process call when you run it in Windows Service? Microsoft does not allow to pass the IPictureDisp object across the processes. Below is a post demonstrates this:

 

http://modthemachine.typepad.com/my_weblog/2010/03/document-thumbnails-and-button-icons.html

 

So if your work is to call ApprenticeServer to get thumbnail in process it should work(otherwise there would be a problem), if you run it as an out-of-process then you need to find a workaround. Brian provided a workaround for this in the above post, please let me if that works for you.



If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

0 Likes
Message 3 of 14

DynamicObjects
Advocate
Advocate
It is always in-process in both our scenarios because we are using ApprenticeServerComponent - which runs in-process to our application. That is the whole point of using ApprenticeServerComponent 🙂
------------------------------------------------------------------------------------------------------------
Atkins Consultants Ltd UK
+19,000 staff (M&E, Civils, Structural, Geotechnical, Environmental and more)
0 Likes
Message 4 of 14

YuhanZhang
Autodesk
Autodesk

Then the failure happens to specific Inventor document or all documents? Can you share us reproducible steps so we can look into where the problem happens?



If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

0 Likes
Message 5 of 14

DynamicObjects
Advocate
Advocate

Hi Rocky,

 

I was in the process of making the smallest possible WindowsService to send to you to reproduce the problem when i found this error which makes me think it may not be related to whether the exe is a service or not.

 

I think i am not understanding something more fundamental with using ApprenticeServerComponent -- here is my short code and it stops on line 24 (line in red) with COM exception -- which i don't understand why it is using COM for ApprenticeServerComponent???  -- i thought the whole point was that ApprenticeServerComponent runs in-process to my app??

 

using System;
using InventorApprentice;

namespace SimpleThumbnailSaver
{
    public class Program
    {
        public static void Main(string[] args)
        {
            // do my stuff
            DoMyStuff();
        }

        public static void DoMyStuff()
        {
            // get ApprenticeServerComponent
            ApprenticeServerComponent apprentice = new ApprenticeServerComponent();

            // get the thumbnail from my test file
            string filename = @"C:\Temp\TestPart.ipt";
            ApprenticeServerDocument document = apprentice.Open(filename);
            Console.WriteLine("opened {0}",document.FullFileName);
            stdole.IPictureDisp thumbnail;
            thumbnail = document.Thumbnail;

            //// convert the thumbnail to an image
            //....
        }
    }
}

Does this code run for you?  Or does it also stop at the red line with an error?

 

Regards

Dan.

 

------------------------------------------------------------------------------------------------------------
Atkins Consultants Ltd UK
+19,000 staff (M&E, Civils, Structural, Geotechnical, Environmental and more)
0 Likes
Message 6 of 14

DynamicObjects
Advocate
Advocate
I should add - that the method DoMyStuff() works fine in a Windows Form App, but fails above as described as a Windows Console App.
------------------------------------------------------------------------------------------------------------
Atkins Consultants Ltd UK
+19,000 staff (M&E, Civils, Structural, Geotechnical, Environmental and more)
0 Likes
Message 7 of 14

DynamicObjects
Advocate
Advocate
i should also clarify that if i transcribe the code to vb.net then i find that both the console app and the windows form app work... so i can only reproduce the error with c# when compiling as a console app or as a windows service. -- i am baffled !!
------------------------------------------------------------------------------------------------------------
Atkins Consultants Ltd UK
+19,000 staff (M&E, Civils, Structural, Geotechnical, Environmental and more)
0 Likes
Message 8 of 14

DynamicObjects
Advocate
Advocate

and here i've attached both the C# and VB versions of that very simple app...

 

 

------------------------------------------------------------------------------------------------------------
Atkins Consultants Ltd UK
+19,000 staff (M&E, Civils, Structural, Geotechnical, Environmental and more)
0 Likes
Message 9 of 14

chandra.shekar.g
Autodesk Support
Autodesk Support
Accepted solution

Hi @DynamicObjects,

 

As you mentioned in the previous post, C# code throws exception at thumbnail = document.Thumbnail; But, VB.net source code works fine and image will be saved.

 

So, a change request (INVGEN - 11322 - Failed at Document.Thumbnail using Apprentice Server in C#) is raised with engineering team.

 

Thanks and regards,

 

 


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 10 of 14

DynamicObjects
Advocate
Advocate
Thanks Chandra - could you add me to a list to be informed when the fix is available please?
------------------------------------------------------------------------------------------------------------
Atkins Consultants Ltd UK
+19,000 staff (M&E, Civils, Structural, Geotechnical, Environmental and more)
0 Likes
Message 11 of 14

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi @DynamicObjects,

 

Once fix is solved. I will let you know.

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 12 of 14

Anonymous
Not applicable

is this solved yet?

0 Likes
Message 13 of 14

chandra.shekar.g
Autodesk Support
Autodesk Support
Accepted solution

@Anonymous,

 

After checking change request - 11322, it seems that it may be fixed in the upcoming version of Inventor.

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 14 of 14

chandra.shekar.g
Autodesk Support
Autodesk Support

@Anonymous,

 

Issue has been fixed in Inventor 2020 with little changes in C# code as shown in below.

 

 

public class Program
    {
        // get ApprenticeServerComponent
        private static ApprenticeServerComponent apprentice = new ApprenticeServerComponent();
        [STAThread]
        private static void Main(string[] args)
        {
            // do my stuff
            DoMyStuff();
        }

        private static void DoMyStuff()
        {
            // get the thumbnail from my test file
            string filename = @"Path of file\Sample.ipt;
            ApprenticeServerDocument document = apprentice.Open(filename);
            Console.WriteLine("opened {0}",document.FullFileName);
            stdole.IPictureDisp thumbnail;
            thumbnail= document.Thumbnail;

            // convert the thumbnail to an image
            Image image = AxHostConverter.PictureDispToImage(thumbnail);

            // save the thumbnail image to the same folder using the next incremental number as a filename suffix
            filename = Path.GetDirectoryName(filename) + @"\" + Path.GetFileNameWithoutExtension(filename) + "_001.png" ;
            image.Save(filename, ImageFormat.Png);
            image.Dispose();
        }
    }

Note: Make sure that solution is built with x64 platform.

 

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes