Ninject manual initialization with constructor injection - ninject

How do I do constructor injection when I'm manually initializing the class?
public class ApiKeyHandler : DelegatingHandler
{
private IApiService apiService;
public ApiKeyHandler(IApiService apiService)
{
this.apiService = apiService;
}
}
Initializing:
var apiKey = new ApiKeyHandler(/*inject here */);
How do I accomplish this? My bindings and everything is already setup.

You want to do something like this:
var apiKey = new ApiKeyHandler(kernel.Get<IApiService>());
However, why not inject the ApiKeyHandler itself?
var apiKey = kernel.Get<ApiKeyHandler>();
Here is an article about Ninject:
You basically want to set this up at the beginning of your code and have it available globally:
public IKernel kernel = new StandardKernel();
...
kernel.Bind<IApiService>()
.To<SomeConcreteApiService>();

Related

How to config certificate with httpClientFactory

I am using httpClientFactory to request a Wechat Pay API, but the Wechat Pay API needs a certificate. How can I configure httpClientFactory to use the certificate?
If you look at the Default factory returned in DI, it inherits from two interfaces, and when injecting you chose to only use one:
internal class DefaultHttpClientFactory : IHttpClientFactory, IHttpMessageHandlerFactory
Knowing this you can take a sneaky workaround:
_handlerFactory = _clientFactory as IHttpMessageHandlerFactory;
Now if you look at the handler factory, it has a method CreateHandler with the same string argument as the CreateClient method. Actually if you look at the implementation of CreateClient, you can see it uses this method which then caches the handler.
So forget about Startup: Cast your client factory to handler factory, create a handler for your specific user, verify all properties on the handler (like is the certificate there or do you need to add it since it's a new instance) and then call CreateClient as usual.
Edit: In startup you may still need to add ConfigurePrimaryHttpMessageHandler to actually create a HttpClientHandler, so you may have to make a consession of only registering one named HttpClient and just swapping the certificate. It still gives you the advantages of having the factory though.
Edit 2: know that the handler is wrapped a bunch of times in decorators, so you need to dig deep. The trick I use is:
var handler = _handlerFactory.CreateHandler("customer");
while (handler as DelegatingHandler != null)
{
handler = (handler as DelegatingHandler).InnerHandler;
}
var clientHandler = handler as HttpClientHandler;
First, you need register you HttpClient:
services.AddHttpClient("signed")
.ConfigurePrimaryMessageHandler(() =>
{
var handler = new HttpClientHandler();
var certificate = new X509Certificate2(filename: "foo.pfx", password: "123");
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
handler.ClientCertificates.Add(certificate);
});
In the example the file foo.pfx contains a certificate with the password 123.
signed is the name of HttpClient.
Second, you'll call IHttpClientFactory.CreateClient to create the HttpClient instance.
public class PayClient
{
private readonly IHttpClientFactory _httpClientFactory;
public PayClient(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory;
}
public async Task SomePayMethodAsync()
{
using (httpClient = _httpClientFactory.CreateClient("signed"))
{
// use httpClient
}
}
}
You need use same name signed as the parameter of CreateClient.
LATER UPDATE
If you write SAAS with many certificates you may create and configure an HttpClient manually every time you need it. It's the simplest way.
public class PayClient
{
private readonly ICurrentUserProvider _currentUserProvider;
public PayClient(ICurrentUserProvider _currentUserProvider)
{
_currentUserProvider = currentUserProvider;
}
private HttpClient CreateHttpClient()
{
var currentUser = _currentUserProvider.CurrentUser;
var filename = currentUser.CertificateFilename;
var password = currentUser.CertificatePassword;
var handler = new HttpClientHandler();
var certificate = new X509Certificate2(filename, password);
handler.ClientCertificateOptions = ClientCertificateOptions.Manual;
handler.ClientCertificates.Add(certificat);
return new HttpClient(handler);
}
public async Task SomePayMethodAsync()
{
using (httpClient = CreateHttpClient())
{
// use httpClient
}
}
}
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-3.1
services.AddHttpClient("configured-disable-automatic-cookies")
.ConfigurePrimaryHttpMessageHandler(() =>
{
return new SocketsHttpHandler()
{
UseCookies = false,
};
});

Activator.CreateInstance and Ninject on asp.net mvc 4

I am trying to use reflection and ninject on the same project. Here is my code :
Type type = Type.GetType("MySolution.Project.Web.App_Code.DataClass");
MethodInfo theMethod = type.GetMethod("Events_ListAll");
object classInstance = Activator.CreateInstance(type, null);
And here is my class that contains that method:
public class DataClass
{
private IEventService eventService;
public DataClass(IEventService eventService)
{
this.eventService = eventService;
}
public String Events_ListAll()
{
List<Event> lstEvents = eventService.GetEvents().ToList<Event>();
return "";
}
}
I get an error saying that there is no constructor found. The solution to that would be to ad an empty default constructor, but that wont inject class I want. Is there any workaround to solve this?
You will need a concrete instance of IEventService to pass as parameter to ctor of DataClass, like this Activator.CreateInstance(type, instance);, so you got many approaches to do that, see 2 of :
1st - class has a concrete IEventService
That class where you doing the reflection has a concrete instance of IEventService and then you just pass as param to the Activator:
public class Foo
{
public Foo(IEventService eventService)
{
Type type = Type.GetType("MySolution.Project.Web.App_Code.DataClass");
MethodInfo theMethod = type.GetMethod("Events_ListAll");
object classInstance = Activator.CreateInstance(type, eventService);
}
}
2nd - Get IKernel implementation of Ninject
If you are using NinjectWebCommom you can just change the bootstrapper prop to public and get the kernel like this NinjectWebCommom.bootstrapper.Kernel.get<IEventService>()
Type type = Type.GetType("MySolution.Project.Web.App_Code.DataClass");
MethodInfo theMethod = type.GetMethod("Events_ListAll");
object classInstance = Activator.CreateInstance(type, Kernel.Get<IEventService>());

#RabbitListener Not receiving messages from queue

I am using #RabbitListner annotation to recieve messages from a RabbitMq queue.
Although I have done all steps required to do this (i.e. Add #EnableRabbit annotation in my config class) and declare SimpleRabbitListenerContainerFactory as a bean , still my method is not recieving messages from the queue . Can anybody suggest what I am missing :
I am using Spring Boot to launch my application
My launch class
#Configuration
#EnableAutoConfiguration
#EnableRabbit
#EnableConfigurationProperties
#EntityScan("persistence.mysql.domain")
#EnableJpaRepositories("persistence.mysql.dao")
#ComponentScan(excludeFilters = { #ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = ApiAuthenticationFilter.class),#ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = ApiVersionValidationFilter.class)},basePackages = {"common", "mqclient","apache", "dispatcher" })
public class Application {
public static void main(final String[] args) {
final SpringApplicationBuilder appBuilder = new SpringApplicationBuilder(
Application.class);
appBuilder.application().setWebEnvironment(false);
appBuilder.profiles("common", "common_mysql_db", "common_rabbitmq")
.run(args);
}
#Bean
#Primary
#ConfigurationProperties(prefix = "spring.datasource")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
}
Here is my Bean to define SimpleRabbitListenerContainerFactory inside a component class
#Component(value = "inputQueueManager")
public class InputQueueManagerImpl extends AbstractQueueManagerImpl {
..///..
#Bean(name = "inputListenerContainerFactory")
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory()
{
SimpleRabbitListenerContainerFactory factory = new
SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(this.rabbitConnectionFactory);
factory.setConcurrentConsumers(Integer.parseInt(this.concurrentConsumers));
factory.setMaxConcurrentConsumers(Integer.parseInt(this.maxConcurrentConsumers));
factory.setMessageConverter(new Jackson2JsonMessageConverter());
return factory;
}
}
And finally my Listener inside another Controller component
#Controller
public class RabbitListner{
#RabbitListener(queues = "Storm1", containerFactory = "inputListenerContainerFactory")
#Override
public void processMessage(QueueMessage message) {
String topic = message.getTopic();
String payload = message.getPayload();
dispatcher.bean.EventBean eventBean = new dispatcher.bean.EventBean();
System.out.println("Data read from the queue");
Unfortunately , I am sending the messages to the queue but the code inside processMessage is not getting executed ever.
I am not sure what is the problem here . Can anybody help ??
By default, the Json message converter requires hints in the message properties as to what type of object to create.
If your producer does not set those properties, it won't be able to do the conversion without some help.
You can inject a ClassMapper into the converter.
The framework provides a DefaultClassMapper which can be customized - either to look at a different message property than the default __TypeId__ property.
If you always want to convert the json to the same object, you can simply set the default type:
DefaultClassMapper classMapper = newDefaultClassMapper();
classMapper.setDefaultType(QueueMessage.class);
Jackson2JsonMessageConverter converter = new Jackson2JsonMessageConverter();
converter.setClassMapper(classMapper);
factory.setMessageConverter(new Jackson2JsonMessageConverter());
The documentation already shows how to configure this.

How to register a ISolrFieldSerializer in Windsor container so that SolrNet can pick it up

I am trying to get an enum to serialize to it's int value when posting to Solr.
So I have implemented a ISolrFieldSerializer to do this, As suggested here. But I can seem to register it within the Windsor container in a way that it then gets used by SolrNet
Here is what I have:
This works fine apart from the serializer does not get used, although it appears in the containers components list. Any ideas?
container.Register(Component.For<ISolrFieldSerializer>().ImplementedBy<SolrEnumSerializer>());
Startup.Init<SearchBox>("http://10.10.10.10:0000/solr/boxes");
container.Register(Component.For<ISolrOperations<SearchBox>>()
.UsingFactoryMethod(k => ServiceLocator.Current.GetInstance<ISolrOperations<SearchBox>>()));
I sorted this by removing the default implementation and replace it with a custom one:
Startup.Container.Remove<ISolrFieldSerializer>();
var fieldSerializer = new CustomSerializer();
Startup.Container.Register<ISolrFieldSerializer>(c => fieldSerializer);
Custom Sertializer:
public class CustomSerializer : ISolrFieldSerializer
{
private readonly AggregateFieldSerializer _serializer;
public CustomSerializer()
{
_serializer = new AggregateFieldSerializer(new ISolrFieldSerializer[]
{
new MyCustom1Serializer(),
new MyCustom2Serializer(),
new CollectionFieldSerializer(this),
new GenericDictionaryFieldSerializer(this),
new NullableFieldSerializer(new BoolFieldSerializer()),
new NullableFieldSerializer(new DateTimeFieldSerializer()),
//new MoneyFieldSerializer(),
new FormattableFieldSerializer(),
new TypeConvertingFieldSerializer(),
});
}
public bool CanHandleType(Type t)
{
return _serializer.CanHandleType(t);
}
public IEnumerable<PropertyNode> Serialize(object obj)
{
return _serializer.Serialize(obj);
}
}

composing MEF parts in C# like a simple Funq container

In Funq and probably most other IoC containers I can simply do this to configure a type:
container.Register<ISomeThing>(c => new SomeThing());
How could I quickly extend MEF (or use existing MEF functionality) to do the same without using attributes.
Here is how I thought I could do it:
var container = new CompositionContainer();
var batch = new CompositionBatch();
batch.AddExport<ISomeThing>(() => new SomeThing());
batch.AddExportedValue(batch);
container.Compose(batch);
With this extension method for CompositionBatch:
public static ComposablePart AddExport<TKey>(this CompositionBatch batch, Func<object> func)
{
var typeString = typeof(TKey).ToString();
return batch.AddExport(
new Export(
new ExportDefinition(
typeString,
new Dictionary<string, object>() { { "ExportTypeIdentity", typeString } }),
func));
}
If I later do:
var a = container.GetExport<ISomeThing>().Value;
var b = container.GetExport<ISomeThing>().Value;
Both instance are the same. How can I force (configure) them to be different instances?
If this is not the way to go, how would I do this in MEF?
I would imagine the key is to add the delegate to the container, e.g.:
container.AddExportedValue<Func<ISomething>>(() => new Something());
That way you can grab the delegate and execute it:
var factory = container.GetExport<Func<ISomething>>();
ISomething something = factory();
Of course, MEF (Silverlight) does provide a native ExportFactory<T> (and ExportFactory<T,TMetadata> type that supports the creation of new instances for each call to import. You can add support for this by downloading Glen Block's ExportFactory for .NET 4.0 (Desktop) library.
If you don't want to use attributes, you can use this trick (based on Mark Seemann's blogpost).
First, create a generic class like this:
[PartCreationPolicy(CreationPolicy.NonShared)]
public class MefAdapter<T> where T : new()
{
private readonly T export;
public MefAdapter()
{
this.export = new T();
}
[Export]
public virtual T Export
{
get { return this.export; }
}
}
Now you can register any class you want in the container, like this:
var registeredTypesCatalog = new TypeCatalog(
typeof(MefAdapter<Foo>),
typeof(MefAdapter<Bar>),
...);
var container = new CompositionContainer(catalog);
Alternatively, you could implement your own export provider derived from ExportProvider, which allows you to pretty much duplicate Funq's way of working:
var provider = new FunqyExportProvider();
provider.Register<IFoo>(context => new Foo());
var container = new CompositionContainer(provider);
Both instance are the same. How can I force (configure) them to be different instances?
Simply mark the SomeThing class like this:
[Export(typeof(ISomeThing)]
[PartCreationPolicy(CreationPolicy.NonShared]
public class SomeThing : ISomeThing
{
...
}
And then you will get different instances wherever you import ISomeThing.
Alternatively, you can also set a required creation policy on an import:
[Export(typeof(IFoo))]
public class Foo : IFoo
{
[Import(typeof(ISomeThing),
RequiredCreationPolicy = CreationPolicy.NonShared)]
public ISomething SomeThing { private get; set; }
}
In Glen Block's Skydrive directory linked to in Matthew Abbott's answer I found something that seems simple and lightweight: A FuncCatalog. Download it here: FuncCatalogExtension.
Using the few little classes from that project I could now do this:
var funcCatalog = new FuncCatalog();
funcCatalog.AddPart<ISomeThing>(ep => new SomeThing());
var container = new CompositionContainer(funcCatalog);
var batch = new CompositionBatch();
batch.AddExportedObject<ExportProvider>(container);
container.Compose(batch);
var a = container.GetExportedObject<ISomeThing>();
var b = container.GetExportedObject<ISomeThing>();