1

I have a DLL which is consumed by a 3rd party Application. I am trying to run a long running Task as Async which is failing due to error:-

"Unable to cast COM object of type 'System.__ComObject' to interface type 'IMSRelation.IJAssocRelation'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{609567C0-4ED3-11D1-B2B9-080036024603}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE))."

The code where it fails is:-

businessObject.Relationships

where businessObject is of type BusinessObject, code for which is

public class BusinessObject
{

    protected internal BusinessObject();

    protected internal BusinessObject(PrivateBusinessObject oBOP);

    public ReadOnlyCollection<RelationCollection> Relationships { get; }
}

Note:- This works fine if I don't use it inside (Async/Await or inside a new Thread) other processes. What I have looked till now is that the COM components needs to be marshaled before using them inside .NET Framework.

So can anyone suggest how to Marshal it, since BusinessObject Class does not implement any interface [As per my understanding Only interfaces can be Marshaled, Correct me if i am wrong].

Now obviously BusinessObject is Root Element and other classes implement it and those classes implement interfaces from where Relationships are accessed.

So do I need to get that particular interface from which I need to find the relationship and marshal it?? Please suggest a solution.

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
  • 1
    similar question asked here: a long long time ago...https://stackoverflow.com/questions/1233468/unable-to-cast-com-object-of-type-exception – jazb Mar 05 '19 at 05:54
  • I already saw that link but does that mean i have to create my own interface implementing/importing the COM interface and then calling the method?? – Shailesh Aggarwal Mar 05 '19 at 06:55
  • The c# you show is not directly relevant to the issue as it doesn't exhibit any COM object usage. What probably happens is there's a COM object (IMSRelation.IJAssocRelation), that was created (somewhere in your code) on a specific thread and its reference is used on another thread (the one implicitely invoked by async) which is incompatible in COM terms with the first one. You can try to create and use the COM object only one one thread. What you could also try is to use a custom SynchronizationContext for async task that uses STA threads as here: https://stackoverflow.com/a/14871827/403671 – Simon Mourier Mar 05 '19 at 08:33
  • 1. COM object (IMSRelation.IJAssocRelation), that was created (somewhere in your code) ---- This was not created by my code. It was created internally by calling "businessObject.Relationships". 2. I tried doing all my stuff in one thread which also results in the same problem. I can try SynchronizationContext thing but I have a feeling it wont work still I will give it a try. – Shailesh Aggarwal Mar 05 '19 at 09:08

0 Answers0