I have a microservice structure where separate domains are loaded in dynamically by putting the dlls in the server, which hosts them. The communication between these domains is done by WCF-Channel-Proxies.
Each microservice is loaded into a different AssemblyLoadContext of the server.
If I have a service contract A, which is called by service B and C, each Project the services are in, includes the Interface DLL for service A. The implementation for that service is provided by the microservice A.
Now this works fine when only B calls A. But as soon as B calls A and after that C calls A (or the other way), Im getting the exception:
Unable to cast object of type 'generatedProxy_3' to type 'x.x.x.EventBroker.Interfaces.IEventReceiverService'. at System.Reflection.DispatchProxy.Create[T,TProxy]()
at System.ServiceModel.Channels.ServiceChannelProxy.CreateProxy[TChannel](MessageDirection direction, ServiceChannel serviceChannel)
at System.ServiceModel.Channels.ServiceChannelFactory.CreateProxy[TChannel](MessageDirection direction, ServiceChannel serviceChannel)
at System.ServiceModel.Channels.ServiceChannelFactory.CreateChannel[TChannel](EndpointAddress address, Uri via)
at System.ServiceModel.ChannelFactory`1.CreateChannel(EndpointAddress address, Uri via)
at System.ServiceModel.ChannelFactory`1.CreateChannel()
This exception is thrown when I call the following for getting the service type:
var channelFactory = new ChannelFactory<TContract>(binding, new EndpointAddress(baseUrl + address));
return channelFactory.CreateChannel();
TContract is the interface type which to call from assembly A.
It seems that the interface assembly from Assembly B is used for the call from B. But when C calls, the type from C is asked, but the assembly B is used for some reason. This results in the error above because generatedProxy_3 iy type from assembly B but its trying to cast this to the correct type from assembly C.
So my question is, does anyone know how I can avoid this problem? Is the assembly type from B somehow cached and thats why it is used even when the type from C is asked. Or does anyone have a better solution for my problem?