I’m returning an EJB by value and I get a ClassCastException when I attempt to narrow the returned remote interface using PortableRemoteObject.narrow(). Why?

I'm returning an EJB by value and I get a ClassCastException when I attempt to narrow the returned remote interface using PortableRemoteObject.narrow(). Why?

When an EJB is returned by value, an independent copy is made on the .NET side, and the Java-side implementation object is no longer accessible. Depending on the application server being used, it may not be possible to recreate the implementation object and its class and perform the narrowing. In cases where EJBs are returned by value, one should not apply PortableRemoteObject.narrow(), but rather should simply apply a class cast directly to the returned EJB. For example, if the EJB being returned by value has remote interface RI, simply do on the .NET side:

RI valueEJB = (RI) home.create();

instead of

java.lang.Object theObj = home.create();
RI valueEJB = (RI) PortableRemoteObject(theObj,
  java.lang.Class.forName("RI"));