0

I have an problem to declare dependency injection. I have to declare a WCF service and I did it like this:

services.AddTransient<IService, ServiceClient>();

As I will need to work with the WCF header I need to add a behavior to check the headers. Normally I would do it like this:

var client = new ServiceClient();
client.Endpoint.Behaviors.Add( new HeaderInspectionBehavior());

But I can't do it that way because I'm getting the IService injected in the constructor.

I tried to do it this way:

var client = new ServiceClient();
client.Endpoint.Behaviors.Add(new HeaderInspectionBehavior());
services.AddTransient<IService, ServiceClient>(sp => client);

But it didn't work, in the second WCF call it changes the state to "FAULT" or "CLOSED".

Does anyone know of another way to do this?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Diego Lobo
  • 156
  • 7

1 Answers1

1

Based in this i found a solution that looks like this:

ServiceClient Config(IServiceProvider _) => new ServiceClient(..., ...);
services.AddTransient<IService, ServiceClient>(Config);

It's work for me.

Diego Lobo
  • 156
  • 7