SolrNet / SolrNet

Solr client for .Net
https://github.com/SolrNet/SolrNet?tab=readme-ov-file#documentation-index
Apache License 2.0
941 stars 797 forks source link

Solr Net Cloud Unity exception #385

Closed Fatalityap closed 6 years ago

Fatalityap commented 6 years ago

Hi I am using Solr.Net.Cloud.Unity ver. 1.0.3, Solr.Net.Cloud.Core ver. 1.0.3, Unity ver. 5.0.0 in .NET Standard 2.0 Console Application. Here is code:

using (var container = new UnityContainer())
                {
                    var cont = new Unity.SolrNetCloudIntegration.SolrNetContainerConfiguration().ConfigureContainer("desktop-lmqi80k:9984", container);
                    var obj = cont.Resolve<ISolrOperations<Camera>>();
                    SolrQueryResults<Camera> cameras = obj.Query(new SolrQueryByField("Name", "Canon"));
                    Console.WriteLine("Product: " + cameras.FirstOrDefault().Name);

                    Console.WriteLine("Test");

                    Console.ReadKey();
                }

And I receive this error:

Exception occurred
Unity.Exceptions.ResolutionFailedException: Resolution of the dependency failed, type = 'SolrNet.ISolrOperations`1[GSec.Service.Solr.Camera]', name = '(none)'.
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The current type, SolrNet.ISolrOperations`1[GSec.Service.Solr.Camera], is an interface and cannot be constructed. Are you missing a type mapping?
-----------------------------------------------
At the time of the exception, the container was:
  Resolving SolrNet.ISolrOperations`1[GSec.Service.Solr.Camera],(none)
 ---> System.InvalidOperationException: The current type, SolrNet.ISolrOperations`1[GSec.Service.Solr.Camera], is an interface and cannot be constructed. Are you missing a type mapping?
   at Unity.ObjectBuilder.BuildPlan.DynamicMethod.Creation.DynamicMethodConstructorStrategy.ThrowForAttemptingToConstructInterface(IBuilderContext context) in C:\projects\unity\Container\src\ObjectBuilder\BuildPlan\DynamicMethod\Creation\DynamicMethodConstructorStrategy.cs:line 280
   at lambda_method(Closure , IBuilderContext )
   at Unity.ObjectBuilder.BuildPlan.DynamicMethod.DynamicBuildPlanGenerationContext.<>c__DisplayClass16_0.<GetBuildMethod>b__0(IBuilderContext context) in C:\projects\unity\Container\src\ObjectBuilder\BuildPlan\DynamicMethod\DynamicBuildPlanGenerationContext.cs:line 134
   at Unity.ObjectBuilder.BuildPlan.DynamicMethod.DynamicMethodBuildPlan.BuildUp(IBuilderContext context) in C:\projects\unity\Container\src\ObjectBuilder\BuildPlan\DynamicMethod\DynamicMethodBuildPlan.cs:line 36
   at Unity.ObjectBuilder.Strategies.BuildPlanStrategy.PreBuildUp(IBuilderContext context) in C:\projects\unity\Container\src\ObjectBuilder\Strategies\BuildPlanStrategy.cs:line 35
   at Unity.Container.StrategyChain.ExecuteBuildUp(IBuilderContext builderContext) in C:\projects\unity\Container\src\Container\StrategyChain.cs:line 56
   at Unity.UnityContainer.BuildUp(Type typeToBuild, Object existing, String name, ResolverOverride[] resolverOverrides) in C:\projects\unity\Container\src\UnityContainer.cs:line 211
   --- End of inner exception stack trace ---
   at Unity.UnityContainer.BuildUp(Type typeToBuild, Object existing, String name, ResolverOverride[] resolverOverrides) in C:\projects\unity\Container\src\UnityContainer.cs:line 215
   at Unity.UnityContainer.Resolve(Type type, String name, ResolverOverride[] resolverOverrides) in C:\projects\unity\Container\src\UnityContainer.cs:line 163
   at Unity.UnityContainerExtensions.Resolve[T](IUnityContainer container, ResolverOverride[] overrides) in C:\projects\unity\Abstractions\src\Utility\UnityContainerExtensions.cs:line 469
   at GSec.Service.Solr.MicroserviceRunner.<RunExtraAsync>d__8.MoveNext() in C:\Repos\GSECPOCS\POC\SolrExample\GSec.Service.Culture\GSec.Service.Culture\MicroserviceRunner.cs:line 71

Exception is thrown on this line:

var obj = cont.Resolve<ISolrOperations<Camera>>();

Where Camera is:

    public class Camera
    {
        [SolrField("Name")]
        public string Name { get; set; }

        [SolrField("UID")]
        public int UID { get; set; }

        [SolrField("id")]
        public string Id { get; set; }
    }

Can you tell me what is wrong?

I can provide additional info if you need some. Thanks!

Fatalityap commented 6 years ago

Anyway, maybe someone can post me example of working code that are uses Solr Net Cloud Unity component ? )

gjunge commented 6 years ago

I personally haven’t used SolrNet Cloud nor Unity, but I will see if I can debug this early next week.

Fatalityap commented 6 years ago

@gjunge Thanks a lot! I have successfully used Common Service Locator for Solr Net Cloud but I don't think that it will be acceptable for us because of it limitations.

gjunge commented 6 years ago

That is good to know, meaning SolrNet.Cloud itself works good, except for the Unity integration.

Fatalityap commented 6 years ago

@gjunge That is good to know, meaning SolrNet.Cloud itself works good, except for the Unity integration. - Yes. ) At least I can run simple query. )

gjunge commented 6 years ago

I can't seem to recreate it:

Here are tests dealing with SolrCloud with Unity: https://github.com/SolrNet/SolrNet/blob/issues/385/SolrNet.Cloud.Tests/UnityTests.cs I added your sample , but it works fine.

What might happen though is that some async error occurs in the SolrCloudProvider, could you try

var provider = new SolrCloudStateProvider("desktop-lmqi80k:9984");
await provider.InitAsync();
var state = provider.GetCloudState();
var colcount  = state.Collections.Count;

using (var container = new UnityContainer())
                {
                    var cont = new Unity.SolrNetCloudIntegration.SolrNetContainerConfiguration().ConfigureContainer(provider, container);
                    var obj = cont.Resolve<ISolrOperations<Camera>>();
                    SolrQueryResults<Camera> cameras = obj.Query(new SolrQueryByField("Name", "Canon"));
                    Console.WriteLine("Product: " + cameras.FirstOrDefault().Name);

                    Console.WriteLine("Test");

                    Console.ReadKey();
                }

And see if is all good.

Fatalityap commented 6 years ago

Hi, thanks for help, but error is stay the same. I have created simple project with this chunk of code. Can you look at it?

https://www.dropbox.com/s/bxfy1i7hyaifwgo/SolrPOC.zip?dl=0

gjunge commented 6 years ago

Great, I was able to reproduce the issue, I will now check what the problem is.

gjunge commented 6 years ago

I think actually you are not connected to the zookeeper. What value does the colcount variable have in the code snippet above, and what does the state variable show?

I added to version 1.0.5 of SolrNet , a check to see if we are connected to the zookeeper instance.

Fatalityap commented 6 years ago

Hi, thanks. It is working. For now exception is changed:

System.AggregateException
  HResult=0x80131500
  Message=One or more errors occurred. (Cannot connect to the Zookeeper instance desktop-lmqi80k:9984. Current state: CONNECTING )
  Source=System.Private.CoreLib
  StackTrace:
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Threading.Tasks.Task.Wait()
   at SolrPOC.Program.Main(String[] args) in D:\POC\SolrPOC\SolrPOC\Program.cs:line 27

Inner Exception 1:
SolrNetCloudConnectionException: Cannot connect to the Zookeeper instance desktop-lmqi80k:9984. Current state: CONNECTING 

I will try to fix error with Zookeeper, but for now it is more clear what is happening )

But will you update Nuget packages?

gjunge commented 6 years ago

Yes we will update the nuget packages. @xavier-morera 1.0.5 currently contains a small change and could be published.

xmorera commented 6 years ago

Thanks @Fatalityap and @gjunge for fixing this issue. I have updated NuGet to 1.0.5

Fatalityap commented 6 years ago

Thanks for help! ) All is working properly, at least Solr Cloud Unity in this case.