Replies: 1 comment 6 replies
-
This works by implementing your own instance provider. It is discussed in #56. public class InstanceProviderBehavior<TContract> : IServiceBehavior
{
private readonly IServiceProvider _serviceProvider;
public InstanceProviderBehavior(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
{
}
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
var provider = new InstanceProvider<TContract>(_serviceProvider);
foreach (var channelDispatcher in serviceHostBase.ChannelDispatchers.OfType<ChannelDispatcher>())
{
foreach (var ed in channelDispatcher.Endpoints.Where(p => !p.IsSystemEndpoint))
{
ed.DispatchRuntime.InstanceProvider = provider;
}
}
}
public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
}
}
public class InstanceProvider<TContract> : IInstanceProvider
{
private readonly IServiceProvider _serviceProvider;
public InstanceProvider(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public object GetInstance(InstanceContext instanceContext, Message message) => _serviceProvider.GetService<TContract>();
public object GetInstance(InstanceContext instanceContext) => _serviceProvider.GetService<TContract>();
public void ReleaseInstance(InstanceContext instanceContext, object instance) { }
} and use it when you configure your service: app.UseServiceModel(builder =>
{
builder
.AddService<EchoService>()
.AddServiceEndpoint<EchoService, Contract.IEchoService>(new BasicHttpBinding(), "/basichttp")
.AddServiceEndpoint<EchoService, Contract.IEchoService>(new NetTcpBinding(), "/nettcp")
.ConfigureServiceHostBase<EchoService>(serviceHost =>
{
serviceHost.Description.Behaviors.Add(new InstanceProviderBehavior<Contract.IEchoService>(app.ApplicationServices));
});
}); Also InstanceContextMode cannot be |
Beta Was this translation helpful? Give feedback.
6 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi again
I was having an issue using the Autofac container whereby InstancePerLifeTime scopes were being resolved during bootstrapping causing the bootstrap to fail (nhibernate sessions and other such per call behaviour).
These container scopes work correctly in standard Core applications for Autofac so I decided to switch to the Core 3.1 DI infrastructure for the CoreWCF based application which worked up to a point.
The issue I found is with dependent objects being injected into the service constructor is not handled by the CoreWCF InstanceBehavior implementation.
My question is: does the CoreWCF only handle parameter-less constructors in the service?
Here is my services configuration for the service and the dependent object
My service is defined so that is Per Call e.g.:
And my ctor:
The instance behaviour in CoreWCF checks the following and returns null since the ctor has a parameter:
If I add a parameter-less ctor the service bootstraps but the dependent object is never injected. Am I missing something in the service configuration or does the underlying instance behaviour ignore ctors with parameters altogether.
Is this why Autofac is confusing instance per life time scopes on service bootstrapping ? - (apologies for the 2 for 1 question)
Any help is most welcome.
Beta Was this translation helpful? Give feedback.
All reactions