0

我正在尝试在我的 asp.net c# 应用程序中使用 Mongodb。我正在使用 Autofac 来解决依赖关系。当 autofac 尝试解析对象构造函数时出现以下错误。

json中的autofac模块详细信息:

  "properties": {
    "ConnectionString": "mongodb://localhost:27017",
    "DatabaseName": "jambov32"
  }


public BlogQueries(string connectionString, string databaseName)
{
    try
    {
        MongoClient mongoClient = new MongoClient(connectionString);
        this.database = mongoClient.GetDatabase(databaseName);
    }
    catch(Exception ex)
    {
        Console.WriteLine("Exception blogquer=="+ex);
    }
}

错误:

fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[0]
      An unhandled exception has occurred while executing the request
Autofac.Core.DependencyResolutionException: An error occurred during the activat
ion of a particular registration. See the inner exception for details. Registrat
ion: Activator = BlogQueries (ReflectionActivator), Services = [Jambo.Producer.A
pplication.Queries.IBlogQueries], Lifetime = Autofac.Core.Lifetime.RootScopeLife
time, Sharing = Shared, Ownership = OwnedByLifetimeScope ---> An exception was t
hrown while invoking the constructor 'Void .ctor(System.String, System.String)'
on type 'BlogQueries'. ---> Could not load file or assembly 'MongoDB.Driver, Ver
sion=2.7.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the
file specified. (See inner exception for details.) (See inner exception for deta
ils.) ---> Autofac.Core.DependencyResolutionException: An exception was thrown w
hile invoking the constructor 'Void .ctor(System.String, System.String)' on type
 'BlogQueries'. ---> Could not load file or assembly 'MongoDB.Driver, Version=2.
7.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file sp
ecified. (See inner exception for details.) ---> System.IO.FileNotFoundException
: Could not load file or assembly 'MongoDB.Driver, Version=2.7.0.0, Culture=neut
ral, PublicKeyToken=null'. The system cannot find the file specified.
   at Jambo.Producer.Infrastructure.Queries.BlogQueries..ctor(String connectionS
tring, String databaseName)
   at lambda_method(Closure , Object[] )
   at Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate
()
   --- End of inner exception stack trace ---
   at Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate
()
   at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IC
omponentContext context, IEnumerable`1 parameters)
   at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters)
   --- End of inner exception stack trace ---
   at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters)
   at Autofac.Core.Lifetime.LifetimeScope.GetOrCreateAndShare(Guid id, Func`1 cr
eator)
   at Autofac.Core.Resolving.InstanceLookup.Execute()
   at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifeti
meScope currentOperationScope, IComponentRegistration registration, IEnumerable`
1 parameters)
   at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration reg
istration, IEnumerable`1 parameters)
   at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context,
Service service, IEnumerable`1 parameters, Object& instance)
   at Autofac.ResolutionExtensions.ResolveOptionalService(IComponentContext cont
ext, Service service, IEnumerable`1 parameters)
   at Microsoft.Extensions.Internal.ActivatorUtilities.GetService(IServiceProvid
er sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)
   at lambda_method(Closure , IServiceProvider , Object[] )
   at Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider.<>c__Disp
layClass4_0.<CreateActivator>b__0(ControllerContext controllerContext)
   at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.<>c__Displa
yClass5_0.<CreateControllerFactory>g__CreateController0(ControllerContext contro
llerContext)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next
, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeInnerFilt
erAsync>d__14.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNot
ification(Task task)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeNextExceptionFilt
erAsync>d__23.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ExceptionContext
 context)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope&
 scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeNextResourceFilte
r>d__22.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecuted
Context context)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope&
 scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeFilterPipelineAsy
nc>d__17.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNot
ification(Task task)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeAsync>d__15.MoveN
ext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNot
ification(Task task)
   at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNot
ification(Task task)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.<Invoke>d__6.
MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNot
ification(Task task)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.<Invoke>
d__7.MoveNext()

谁能请我在这里做错了什么?

谢谢

4

0 回答 0