[英]Net Core - unable to resolve dependency
I'm using GraphQL 2.4.0 with .NET Core 3.1 solution and Microsoft DI.我正在使用 GraphQL 2.4.0 和 .NET Core 3.1 解决方案和 Microsoft DI。 From within mutation, I need to perform additional processing (shown in MyMutation class below).
在突变内部,我需要执行额外的处理(如下面的 MyMutation class 所示)。
My Schema.cs
looks like:我的
Schema.cs
看起来像:
public class MySchema : GraphQL.Types.Schema
{
private ISchema _schema { get; set; }
public ISchema GraphQLSchema
{
get
{
return this._schema;
}
}
public MySchema()
{
this._schema = Schema.For(@"
type MyObj{
id: ID
name: String,
type: String,
createdAt: String
}
type Mutation {
objSync(type: String): MyObj
}
type Query {
myobj: MyObj
}
", _ =>
{
_.Types.Include<MyQuery>();
_.Types.Include<MyMutation>();
});
}
}
MyMutation.cs
looks like: MyMutation.cs
看起来像:
[GraphQLMetadata("MyMutation")]
public class MyMutation
{
private readonly ISomeType someType;
public MyMutation(ISomeType someType)
{
this.someType= someType;
}
[GraphQLMetadata("objSync")]
public Document SyncObj(string type)
{
byte[] _bytes = null;
_bytes = someType.Process(type).Result;
return new Obj { File = Encoding.UTF8.GetString(_bytes , 0, _bytes .Length) };
}
}
ISomeType
looks like: ISomeType
看起来像:
public interface ISomeType
{
Task<byte[]> Process(string type);
}
and SomeManager.cs
would be: SomeManager.cs
将是:
public class SomeManager: ISomeType
{
private readonly IConfiguration configuration;
private readonly Func<string, IOtherTypeManager> otherType;
public SomeManager(IConfiguration configuration, Func<string, IOtherTypeManager> otherType)
{
this.configuration = configuration;
this.otherType= otherType;
}
public async Task<byte[]> Process(string type)
{
...
}
}
As you may notice, I need to resolve ISomeType with DI.您可能会注意到,我需要使用 DI 解析 ISomeType。 So
Startup.cs
looks like:所以
Startup.cs
看起来像:
...
public static void AddServices(this IServiceCollection services)
{
services.AddSingleton<IDependencyResolver>(_ => new FuncDependencyResolver(_.GetRequiredService));
services.AddSingleton<IDocumentExecuter, DocumentExecuter>();
services.AddSingleton<IDocumentWriter, DocumentWriter>();
services.AddSingleton<ISomeType, SomeManager>();
...
}
...
When executed the program, it throws the following exception:当执行程序时,它会抛出以下异常:
Error trying to resolve objSync.
Failed to call Activator.CreateInstance. Type: MyNameSpace.Graphql.MyMutation
No parameterless constructor defined for type 'MyNameSpace.Graphql.MyMutation'.
at System.RuntimeType.CreateInstanceDefaultCtorSlow(Boolean publicOnly, Boolean wrapExceptions, Boolean fillCache)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, Boolean wrapExceptions)
at System.Activator.CreateInstance(Type type, Boolean nonPublic, Boolean wrapExceptions)
at System.Activator.CreateInstance(Type type)
at GraphQL.DefaultDependencyResolver.Resolve(Type type)
Do I need to resolve the ISomeType
differently?我需要以不同的方式解析
ISomeType
吗? Please guide.请指导。
Update更新
Getting rid of ISomeType
from MyMutation.cs
solves this problem... but I need it to call someType.Process(type).Result
.从
MyMutation.cs
ISomeType
了这个问题......但我需要它来调用someType.Process(type).Result
。
MyMutation class also need to be registered in DI container. MyMutation class 也需要在 DI 容器中注册。 Can you try this out?
你能试试这个吗?
services.AddSingleton<MyMutation>();
services.AddScoped<MyMutation>();
services.AddScoped<ISchema, MySchema>();
Basically I will follow this example: https://github.com/graphql-dotnet/examples/tree/master/src/StarWars基本上我会遵循这个例子: https://github.com/graphql-dotnet/examples/tree/master/src/StarWars
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.