C# Net核心-无法解析依赖关系
我将GraphQL 2.4.0与.NET Core 3.1解决方案和Microsoft DI一起使用。 在变异中,我需要执行额外的处理(如下面的MyMutation类所示) 我的C# Net核心-无法解析依赖关系,c#,.net,.net-core,dependency-injection,graphql,C#,.net,.net Core,Dependency Injection,Graphql,我将GraphQL 2.4.0与.NET Core 3.1解决方案和Microsoft DI一起使用。 在变异中,我需要执行额外的处理(如下面的MyMutation类所示) 我的Schema.cs看起来像: public class MySchema : GraphQL.Types.Schema { private ISchema _schema { get; set; } public ISchema GraphQLSchema { get
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>();
});
}
}
[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) };
}
}
public interface ISomeType
{
Task<byte[]> Process(string type);
}
...
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>();
...
}
...
ISomeType
看起来像:
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>();
});
}
}
[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) };
}
}
public interface ISomeType
{
Task<byte[]> Process(string type);
}
...
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>();
...
}
...
正如您可能注意到的,我需要用DI解析ISomeType。因此,Startup.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>();
});
}
}
[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) };
}
}
public interface ISomeType
{
Task<byte[]> Process(string type);
}
...
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>();
...
}
...
我是否需要以不同方式解析ISomeType
?请导游
更新
从
MyMutation.cs中去除isomtype
可以解决这个问题。。。但是我需要它调用someType.Process(type).Result
MyMutation类也需要在DI容器中注册。你能试试这个吗
services.AddSingleton<MyMutation>();
services.AddSingleton();
MyMutation类也需要在DI容器中注册。你能试试这个吗
services.AddSingleton<MyMutation>();
services.AddSingleton();
services.addScope();
services.addScope();
基本上,我将遵循以下示例:
services.addScope();
services.addScope();
基本上,我将遵循以下示例:
@valorad我看到了你不久前发布的类似问题。您能在此提供帮助吗?您好,可能是我的突变类也需要在DI容器中注册。@valorad我刚才看到了您发布的类似问题。你们能在这里帮忙吗?嗨,可能是我的变种类也需要在DI容器中注册。我已经尝试了这个更改。。但还是一样的错误。我添加了一个更新,如果我删除ISomeType
相关代码,它就会工作。所以我相信这并没有得到解决。我已经尝试过这种改变。。但还是一样的错误。我添加了一个更新,如果我删除ISomeType
相关代码,它就会工作。所以我相信这并没有得到解决。我已经尝试过这种改变。。但还是一样的错误。我添加了一个更新,如果我删除ISomeType
相关代码,它就会工作。所以我相信这并没有得到解决。你能展示一下你的ISomeType和SomeManager是什么样子吗?我认为问题在于在构造函数中添加Func otherType。你能把它拿开一会儿,看看它能不能用?我已经试过这个改变了。。但还是一样的错误。我添加了一个更新,如果我删除ISomeType
相关代码,它就会工作。所以我相信这并没有得到解决。你能展示一下你的ISomeType和SomeManager是什么样子吗?我认为问题在于在构造函数中添加Func otherType。你能把它拿开一会儿,看看它能不能用?