Unable to resolve service for type 'GraphQL.Http.IDocumentWriter'











up vote
0
down vote

favorite












I used following only controller in my project out of entire following project.



https://github.com/graphql-dotnet/examples/blob/master/src/AspNetWebApi/Example/Controllers/GraphQLController.cs



Here Controller file is as below.



using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using GraphQL.Http;
using GraphQL.Instrumentation;
using GraphQL.Types;
using GraphQL.Validation.Complexity;

namespace GraphQL.GraphiQL.Controllers
{
public class GraphQLController : ApiController
{
private readonly ISchema _schema;
private readonly IDocumentExecuter _executer;
private readonly IDocumentWriter _writer;
private readonly IDictionary<string, string> _namedQueries;

public GraphQLController(
IDocumentExecuter executer,
IDocumentWriter writer,
ISchema schema)
{
_executer = executer;
_writer = writer;
_schema = schema;

_namedQueries = new Dictionary<string, string>
{
["a-query"] = @"query foo { hero { name } }"
};
}

// This will display an example error
[HttpGet]
public Task<HttpResponseMessage> GetAsync(HttpRequestMessage request)
{
return PostAsync(request, new GraphQLQuery { Query = "query foo { hero }", Variables = null });
}

[HttpPost]
public async Task<HttpResponseMessage> PostAsync(HttpRequestMessage request, GraphQLQuery query)
{
var inputs = query.Variables.ToInputs();
var queryToExecute = query.Query;

if (!string.IsNullOrWhiteSpace(query.NamedQuery))
{
queryToExecute = _namedQueries[query.NamedQuery];
}

var result = await _executer.ExecuteAsync(_ =>
{
_.Schema = _schema;
_.Query = queryToExecute;
_.OperationName = query.OperationName;
_.Inputs = inputs;

_.ComplexityConfiguration = new ComplexityConfiguration { MaxDepth = 15 };
_.FieldMiddleware.Use<InstrumentFieldsMiddleware>();

}).ConfigureAwait(false);

var httpResult = result.Errors?.Count > 0
? HttpStatusCode.BadRequest
: HttpStatusCode.OK;

var json = _writer.Write(result);

var response = request.CreateResponse(httpResult);
response.Content = new StringContent(json, Encoding.UTF8, "application/json");

return response;
}
}

public class GraphQLQuery
{
public string OperationName { get; set; }
public string NamedQuery { get; set; }
public string Query { get; set; }
public Newtonsoft.Json.Linq.JObject Variables { get; set; }
}
}


When I try to call this controller, it gives me following error.



InvalidOperationException: Unable to resolve service for type 'GraphQL.Http.IDocumentWriter' while attempting to activate 'MyWebAppNamespace.Controllers.GraphQLController'.


Do I need any startup configuration or I can use this standalone controller? What am I missing?










share|improve this question


















  • 1




    Note that ApiController class (not attribute) is from previous version of the framework yet the question is tagged as asp.net-core
    – Nkosi
    Nov 12 at 10:01






  • 1




    I would suggest you recheck the startup flow
    – Nkosi
    Nov 12 at 10:05















up vote
0
down vote

favorite












I used following only controller in my project out of entire following project.



https://github.com/graphql-dotnet/examples/blob/master/src/AspNetWebApi/Example/Controllers/GraphQLController.cs



Here Controller file is as below.



using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using GraphQL.Http;
using GraphQL.Instrumentation;
using GraphQL.Types;
using GraphQL.Validation.Complexity;

namespace GraphQL.GraphiQL.Controllers
{
public class GraphQLController : ApiController
{
private readonly ISchema _schema;
private readonly IDocumentExecuter _executer;
private readonly IDocumentWriter _writer;
private readonly IDictionary<string, string> _namedQueries;

public GraphQLController(
IDocumentExecuter executer,
IDocumentWriter writer,
ISchema schema)
{
_executer = executer;
_writer = writer;
_schema = schema;

_namedQueries = new Dictionary<string, string>
{
["a-query"] = @"query foo { hero { name } }"
};
}

// This will display an example error
[HttpGet]
public Task<HttpResponseMessage> GetAsync(HttpRequestMessage request)
{
return PostAsync(request, new GraphQLQuery { Query = "query foo { hero }", Variables = null });
}

[HttpPost]
public async Task<HttpResponseMessage> PostAsync(HttpRequestMessage request, GraphQLQuery query)
{
var inputs = query.Variables.ToInputs();
var queryToExecute = query.Query;

if (!string.IsNullOrWhiteSpace(query.NamedQuery))
{
queryToExecute = _namedQueries[query.NamedQuery];
}

var result = await _executer.ExecuteAsync(_ =>
{
_.Schema = _schema;
_.Query = queryToExecute;
_.OperationName = query.OperationName;
_.Inputs = inputs;

_.ComplexityConfiguration = new ComplexityConfiguration { MaxDepth = 15 };
_.FieldMiddleware.Use<InstrumentFieldsMiddleware>();

}).ConfigureAwait(false);

var httpResult = result.Errors?.Count > 0
? HttpStatusCode.BadRequest
: HttpStatusCode.OK;

var json = _writer.Write(result);

var response = request.CreateResponse(httpResult);
response.Content = new StringContent(json, Encoding.UTF8, "application/json");

return response;
}
}

public class GraphQLQuery
{
public string OperationName { get; set; }
public string NamedQuery { get; set; }
public string Query { get; set; }
public Newtonsoft.Json.Linq.JObject Variables { get; set; }
}
}


When I try to call this controller, it gives me following error.



InvalidOperationException: Unable to resolve service for type 'GraphQL.Http.IDocumentWriter' while attempting to activate 'MyWebAppNamespace.Controllers.GraphQLController'.


Do I need any startup configuration or I can use this standalone controller? What am I missing?










share|improve this question


















  • 1




    Note that ApiController class (not attribute) is from previous version of the framework yet the question is tagged as asp.net-core
    – Nkosi
    Nov 12 at 10:01






  • 1




    I would suggest you recheck the startup flow
    – Nkosi
    Nov 12 at 10:05













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I used following only controller in my project out of entire following project.



https://github.com/graphql-dotnet/examples/blob/master/src/AspNetWebApi/Example/Controllers/GraphQLController.cs



Here Controller file is as below.



using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using GraphQL.Http;
using GraphQL.Instrumentation;
using GraphQL.Types;
using GraphQL.Validation.Complexity;

namespace GraphQL.GraphiQL.Controllers
{
public class GraphQLController : ApiController
{
private readonly ISchema _schema;
private readonly IDocumentExecuter _executer;
private readonly IDocumentWriter _writer;
private readonly IDictionary<string, string> _namedQueries;

public GraphQLController(
IDocumentExecuter executer,
IDocumentWriter writer,
ISchema schema)
{
_executer = executer;
_writer = writer;
_schema = schema;

_namedQueries = new Dictionary<string, string>
{
["a-query"] = @"query foo { hero { name } }"
};
}

// This will display an example error
[HttpGet]
public Task<HttpResponseMessage> GetAsync(HttpRequestMessage request)
{
return PostAsync(request, new GraphQLQuery { Query = "query foo { hero }", Variables = null });
}

[HttpPost]
public async Task<HttpResponseMessage> PostAsync(HttpRequestMessage request, GraphQLQuery query)
{
var inputs = query.Variables.ToInputs();
var queryToExecute = query.Query;

if (!string.IsNullOrWhiteSpace(query.NamedQuery))
{
queryToExecute = _namedQueries[query.NamedQuery];
}

var result = await _executer.ExecuteAsync(_ =>
{
_.Schema = _schema;
_.Query = queryToExecute;
_.OperationName = query.OperationName;
_.Inputs = inputs;

_.ComplexityConfiguration = new ComplexityConfiguration { MaxDepth = 15 };
_.FieldMiddleware.Use<InstrumentFieldsMiddleware>();

}).ConfigureAwait(false);

var httpResult = result.Errors?.Count > 0
? HttpStatusCode.BadRequest
: HttpStatusCode.OK;

var json = _writer.Write(result);

var response = request.CreateResponse(httpResult);
response.Content = new StringContent(json, Encoding.UTF8, "application/json");

return response;
}
}

public class GraphQLQuery
{
public string OperationName { get; set; }
public string NamedQuery { get; set; }
public string Query { get; set; }
public Newtonsoft.Json.Linq.JObject Variables { get; set; }
}
}


When I try to call this controller, it gives me following error.



InvalidOperationException: Unable to resolve service for type 'GraphQL.Http.IDocumentWriter' while attempting to activate 'MyWebAppNamespace.Controllers.GraphQLController'.


Do I need any startup configuration or I can use this standalone controller? What am I missing?










share|improve this question













I used following only controller in my project out of entire following project.



https://github.com/graphql-dotnet/examples/blob/master/src/AspNetWebApi/Example/Controllers/GraphQLController.cs



Here Controller file is as below.



using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using GraphQL.Http;
using GraphQL.Instrumentation;
using GraphQL.Types;
using GraphQL.Validation.Complexity;

namespace GraphQL.GraphiQL.Controllers
{
public class GraphQLController : ApiController
{
private readonly ISchema _schema;
private readonly IDocumentExecuter _executer;
private readonly IDocumentWriter _writer;
private readonly IDictionary<string, string> _namedQueries;

public GraphQLController(
IDocumentExecuter executer,
IDocumentWriter writer,
ISchema schema)
{
_executer = executer;
_writer = writer;
_schema = schema;

_namedQueries = new Dictionary<string, string>
{
["a-query"] = @"query foo { hero { name } }"
};
}

// This will display an example error
[HttpGet]
public Task<HttpResponseMessage> GetAsync(HttpRequestMessage request)
{
return PostAsync(request, new GraphQLQuery { Query = "query foo { hero }", Variables = null });
}

[HttpPost]
public async Task<HttpResponseMessage> PostAsync(HttpRequestMessage request, GraphQLQuery query)
{
var inputs = query.Variables.ToInputs();
var queryToExecute = query.Query;

if (!string.IsNullOrWhiteSpace(query.NamedQuery))
{
queryToExecute = _namedQueries[query.NamedQuery];
}

var result = await _executer.ExecuteAsync(_ =>
{
_.Schema = _schema;
_.Query = queryToExecute;
_.OperationName = query.OperationName;
_.Inputs = inputs;

_.ComplexityConfiguration = new ComplexityConfiguration { MaxDepth = 15 };
_.FieldMiddleware.Use<InstrumentFieldsMiddleware>();

}).ConfigureAwait(false);

var httpResult = result.Errors?.Count > 0
? HttpStatusCode.BadRequest
: HttpStatusCode.OK;

var json = _writer.Write(result);

var response = request.CreateResponse(httpResult);
response.Content = new StringContent(json, Encoding.UTF8, "application/json");

return response;
}
}

public class GraphQLQuery
{
public string OperationName { get; set; }
public string NamedQuery { get; set; }
public string Query { get; set; }
public Newtonsoft.Json.Linq.JObject Variables { get; set; }
}
}


When I try to call this controller, it gives me following error.



InvalidOperationException: Unable to resolve service for type 'GraphQL.Http.IDocumentWriter' while attempting to activate 'MyWebAppNamespace.Controllers.GraphQLController'.


Do I need any startup configuration or I can use this standalone controller? What am I missing?







asp.net-core-mvc graphql asp.net-core-webapi






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 12:31









Anonymous Creator

11011




11011








  • 1




    Note that ApiController class (not attribute) is from previous version of the framework yet the question is tagged as asp.net-core
    – Nkosi
    Nov 12 at 10:01






  • 1




    I would suggest you recheck the startup flow
    – Nkosi
    Nov 12 at 10:05














  • 1




    Note that ApiController class (not attribute) is from previous version of the framework yet the question is tagged as asp.net-core
    – Nkosi
    Nov 12 at 10:01






  • 1




    I would suggest you recheck the startup flow
    – Nkosi
    Nov 12 at 10:05








1




1




Note that ApiController class (not attribute) is from previous version of the framework yet the question is tagged as asp.net-core
– Nkosi
Nov 12 at 10:01




Note that ApiController class (not attribute) is from previous version of the framework yet the question is tagged as asp.net-core
– Nkosi
Nov 12 at 10:01




1




1




I would suggest you recheck the startup flow
– Nkosi
Nov 12 at 10:05




I would suggest you recheck the startup flow
– Nkosi
Nov 12 at 10:05












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted











Do I need any startup configuration or I can use this standalone controller? What am I missing?




As the error describes, it happens because the DI container cannot resolve service for IDocumentWriter.



It seems that you're missing some service registration. Did you forget to add the IDocumentWrite service ?



public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IDependencyResolver>(s => new FuncDependencyResolver(s.GetRequiredService));

services.AddSingleton<IDocumentExecuter, DocumentExecuter>();
services.AddSingleton<IDocumentWriter, DocumentWriter>();

// ...
services.AddSingleton<ISchema, StarWarsSchema>();
// ...
}


For more details, see startup here




or I can use this standalone controller?




No. You're not supposed to do that. That's because your controller depends on the IDocumentExecuter, IDocumentWriter, and ISchema service. They're all resolved by Dependency Injection. You have to register all the services before resolve the controller.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248792%2funable-to-resolve-service-for-type-graphql-http-idocumentwriter%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted











    Do I need any startup configuration or I can use this standalone controller? What am I missing?




    As the error describes, it happens because the DI container cannot resolve service for IDocumentWriter.



    It seems that you're missing some service registration. Did you forget to add the IDocumentWrite service ?



    public void ConfigureServices(IServiceCollection services)
    {
    services.AddSingleton<IDependencyResolver>(s => new FuncDependencyResolver(s.GetRequiredService));

    services.AddSingleton<IDocumentExecuter, DocumentExecuter>();
    services.AddSingleton<IDocumentWriter, DocumentWriter>();

    // ...
    services.AddSingleton<ISchema, StarWarsSchema>();
    // ...
    }


    For more details, see startup here




    or I can use this standalone controller?




    No. You're not supposed to do that. That's because your controller depends on the IDocumentExecuter, IDocumentWriter, and ISchema service. They're all resolved by Dependency Injection. You have to register all the services before resolve the controller.






    share|improve this answer

























      up vote
      1
      down vote



      accepted











      Do I need any startup configuration or I can use this standalone controller? What am I missing?




      As the error describes, it happens because the DI container cannot resolve service for IDocumentWriter.



      It seems that you're missing some service registration. Did you forget to add the IDocumentWrite service ?



      public void ConfigureServices(IServiceCollection services)
      {
      services.AddSingleton<IDependencyResolver>(s => new FuncDependencyResolver(s.GetRequiredService));

      services.AddSingleton<IDocumentExecuter, DocumentExecuter>();
      services.AddSingleton<IDocumentWriter, DocumentWriter>();

      // ...
      services.AddSingleton<ISchema, StarWarsSchema>();
      // ...
      }


      For more details, see startup here




      or I can use this standalone controller?




      No. You're not supposed to do that. That's because your controller depends on the IDocumentExecuter, IDocumentWriter, and ISchema service. They're all resolved by Dependency Injection. You have to register all the services before resolve the controller.






      share|improve this answer























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted







        Do I need any startup configuration or I can use this standalone controller? What am I missing?




        As the error describes, it happens because the DI container cannot resolve service for IDocumentWriter.



        It seems that you're missing some service registration. Did you forget to add the IDocumentWrite service ?



        public void ConfigureServices(IServiceCollection services)
        {
        services.AddSingleton<IDependencyResolver>(s => new FuncDependencyResolver(s.GetRequiredService));

        services.AddSingleton<IDocumentExecuter, DocumentExecuter>();
        services.AddSingleton<IDocumentWriter, DocumentWriter>();

        // ...
        services.AddSingleton<ISchema, StarWarsSchema>();
        // ...
        }


        For more details, see startup here




        or I can use this standalone controller?




        No. You're not supposed to do that. That's because your controller depends on the IDocumentExecuter, IDocumentWriter, and ISchema service. They're all resolved by Dependency Injection. You have to register all the services before resolve the controller.






        share|improve this answer













        Do I need any startup configuration or I can use this standalone controller? What am I missing?




        As the error describes, it happens because the DI container cannot resolve service for IDocumentWriter.



        It seems that you're missing some service registration. Did you forget to add the IDocumentWrite service ?



        public void ConfigureServices(IServiceCollection services)
        {
        services.AddSingleton<IDependencyResolver>(s => new FuncDependencyResolver(s.GetRequiredService));

        services.AddSingleton<IDocumentExecuter, DocumentExecuter>();
        services.AddSingleton<IDocumentWriter, DocumentWriter>();

        // ...
        services.AddSingleton<ISchema, StarWarsSchema>();
        // ...
        }


        For more details, see startup here




        or I can use this standalone controller?




        No. You're not supposed to do that. That's because your controller depends on the IDocumentExecuter, IDocumentWriter, and ISchema service. They're all resolved by Dependency Injection. You have to register all the services before resolve the controller.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 at 9:26









        itminus

        2,6511320




        2,6511320






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248792%2funable-to-resolve-service-for-type-graphql-http-idocumentwriter%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Error while running script in elastic search , gateway timeout

            Adding quotations to stringified JSON object values