Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call WCF client generated from Visual Studio from different AssemblyLoadContext causes exception #5688

Open
mcortellino opened this issue Nov 14, 2024 · 2 comments
Labels

Comments

@mcortellino
Copy link

mcortellino commented Nov 14, 2024

Describe the bug
When call the same WCF client generated from VS2022 (Add Service Reference) loaded by reflection
through 2 different AssemblyLoadContext the second call causes an InvalidCastException 'Unable to cast object of type 'generatedProxy_2' to type 'ServiceReference3.Service1Soap'.

To Reproduce
Steps to reproduce the behavior:
TestWcfClient.zip

AssemblyLoadContext lc1 = new AssemblyLoadContext("alc1");
 string clientAssembly1 = Path.GetFullPath("..\\..\\..\\..\\WcfClient\\bin\\Debug\\net8.0\\WcfClient.dll");
 var assembly1 = lc1.LoadFromAssemblyPath(clientAssembly1);
 var helper1 = assembly1.GetTypes().FirstOrDefault(x => x.FullName.Contains("WcfHelper"));
 var instance1 = (IWcfHelper)Activator.CreateInstance(helper1);
 string result1 = instance1.CallWcf();
 Console.WriteLine("Result 1: " + result1);


 AssemblyLoadContext lc2 = new AssemblyLoadContext("alc2");
 string clientAssembly2 = Path.GetFullPath("..\\..\\..\\..\\WcfClient\\bin\\Debug\\net8.0\\WcfClient.dll");
 var assembly2 = lc2.LoadFromAssemblyPath(clientAssembly2);
 var helper2 = assembly2.GetTypes().FirstOrDefault(x => x.FullName.Contains("WcfHelper"));
 var instance2 = (IWcfHelper)Activator.CreateInstance(helper2);
 string result2 = instance2.CallWcf(); // <-- throw InvalidCastException
 Console.WriteLine("Result 2: " + result2);

Check the attached sample project for the full code.

Expected behavior
The WCF client should be called from differents AssemblyLoadContext without exception.

Additional context
Framework: .NET8
VisualStudio 2022 Version 17.11.5

@mconnew
Copy link
Member

mconnew commented Nov 14, 2024

This is a known issue, you can track the progress/thumbs up this issue in the runtime repo to let them know this is impacting you. I have a potential workaround for you. You need to load the WCF assemblies into the load context. This way DispatchProxy will generate the proxy assembly in a separate AssemblyBuilder instance for your ALC. You can do that with code similar to this:

            var smAssemblyPath = typeof(ChannelFactory<>).Assembly.Location;
            alc.LoadFromAssemblyPath(smAssemblyPath);

You probably need to do the same for the type you are using for the binding, eg BasicHttpBinding, or if using a CustomBinding, for the transport binding element.

If you own WcfClient.dll, the least disruptive to consuming code mechanism you can do is to add a module initializer to do this at load time of WcfClient.dll. I put together this code, but I've never tested it. Let me know if this works. Again, you need to add similar code for the assembly containing the binding too.

using System;
using System.Runtime.CompilerServices;

public class ALCModuleInitializer
{
    [ModuleInitializer]
    public static void Initialize()
    {
        var thisAssembly = typeof(SomeTypeInMyAssembly).Assembly;
        var alc = AssemblyLoadContext.GetLoadContext(thisAssembly);
        if (alc != AssemblyLoadContext.Default)
        {
            string thisAssemblyPath = thisAssembly.Location;
            string appAssemblyPath = Path.GetDirectoryName(thisAssemblyPath);
            string SSMPrimitivesAssemblyPath = Path.Combine(appAssemblyPath, "System.ServiceModel.Primitives.dll");
            string SPrivateSMAssemblyPath = Path.Combine(appAssemblyPath, "System.Private.ServiceModel.dll");
            alc.LoadFromAssemblyPath(SSMPrimitivesAssemblyPath);
            if (File.Exists(SPrivateSMAssemblyPath)
            {
                alc.LoadFromAssemblyPath(SPrivateSMAssemblyPath);
            }
        }
    }
}

You might need to change the path code, it's presuming the application folder location for System.ServiceModel.Primitives.dll is beside WcfClient.dll.

@mcortellino
Copy link
Author

Thank you very much. I tested the first workaround and it works, actually I don't have ownership on the WcfClient because it'is inside a thirdly part pluggable component. So the best thing I can do is to load the requested Wcf assemblies inside the LC.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants