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

亡梦爱人 提交于 2019-12-12 04:13:55

问题


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>>()));

回答1:


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);
        }
    }


来源:https://stackoverflow.com/questions/11816795/how-to-register-a-isolrfieldserializer-in-windsor-container-so-that-solrnet-can

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!