wcf icon indicating copy to clipboard operation
wcf copied to clipboard

EndpointDispatcher and ClientRuntime

Open tapizquent opened this issue 4 years ago • 2 comments

Is there any alternative to accessing the ClientRuntime from EndpointDispatcher?

It seems that in .NET 5.0, using System.ServiceModel 4.8.1, EndpointDispatcher class is completely empty, only contains a single empty constructor.

We used to have some tests that would check that an IEndpointBehavior was properly added to the client using WCF.

var myEndpointBehavior = new MyEndpointBehavior();
var serviceEndpoint = new ServiceEndpoint(new ContractDescription("localhost"));
var dispatcher = new EndpointDispatcher(new EndpointAddress("http://localhost"), "", ""); // <--- Error because EndpointDispatcher class is totally empty
var clientRuntime = dispatcher.DispatchRuntime.CallbackClientRuntime; // <---- does not exist

clientRuntime.ClientMessageInspectors.Should().HaveCount(0);
myEndpointBehavior.ApplyClientBehavior(serviceEndpoint, clientRuntime);
clientRuntime.ClientMessageInspectors.Should().HaveCount(1);
clientRuntime.ClientMessageInspectors.First().Should().BeOfType<MyEndpointBehaviorMessageInspector>();

Is there any way to test this same behavior in .NET 5.0?

I have been trying to find a way to access the ClientRuntime but haven't really found anything

tapizquent avatar Sep 02 '21 20:09 tapizquent

In order to test this, I ended up using Activator to get access to the ClientRuntime's constructor. It is not an elegant solution, but at least it allows us to test the same behavior that we were testing with the .Net Framework version.

If there is a better way to do this, I would be open to suggestions. Thank you!

var myEndpointBehavior = new MyEndpointBehavior();
var serviceEndpoint = new ServiceEndpoint(new ContractDescription("localhost"));
var clientRuntime = (ClientRuntime) Activator.CreateInstance(typeof(ClientRuntime), BindingFlags.Instance | BindingFlags.NonPublic,
                null, new[] { serviceEndpoint.Contract.Name, serviceEndpoint.Contract.Namespace }, null, null);

clientRuntime.ClientMessageInspectors.Should().HaveCount(0);
myEndpointBehavior.ApplyClientBehavior(serviceEndpoint, clientRuntime);
clientRuntime.ClientMessageInspectors.Should().HaveCount(1);
clientRuntime.ClientMessageInspectors.First().Should().BeOfType<MyEndpointBehaviorMessageInspector>();

tapizquent avatar Sep 07 '21 16:09 tapizquent

Thanks for raising this with us!

@tapizquent your solution is stable, and these APIs will not change. We will look into the feasibility of supporting this in future releases..

HongGit avatar Sep 27 '21 23:09 HongGit