0

In my Web api project I am having a controller DocumentViewerV1Controller which has a code as:

      /// <summary>
    /// 
    /// </summary>
    /// <param name="documentViewerService"></param>
    public DocumentViewerV1Controller(IDocumentViewerService<HtmlViewInformation> documentViewerService)
    {
        _documentViewerHtmlService = documentViewerService;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="documentViewerService"></param>
    public DocumentViewerV1Controller(IDocumentViewerService<PageImage> documentViewerService)
    {
        _documentViewerImageService = documentViewerService;
    }/// <summary>
    /// Rendering Document as Html
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    [Route(WebApiConfig.RootApiUri + "/v1/viewashtml/{docid}")]
    public string ViewAsHtml(string docId)
    {
        var documentInfo = new DocumentInfo { DocumentName = HostingEnvironment.MapPath("~/Uploads/") + docId };
        var response = _documentViewerHtmlService.RenderDocument(documentInfo, DocumentRenderType.Html);
        return GenerateResponse(response);
    }

When I run my service, make a call and debug the constructor initialization, It doesn't goes through the initialization which makes _documentViewerHtmlService as null, eventually fails returning Null reference Exception.

Is it possible to have service Interface as IDocumentViewerService?

6
  • Which IoC container are you using Commented Apr 20, 2016 at 13:55
  • I am using Unity as IoC container Commented Apr 20, 2016 at 13:56
  • what's your constructor code Commented Apr 20, 2016 at 14:00
  • @Karthik M R This is -> public DocumentViewerV1Controller(IDocumentViewerService<HtmlViewInformation> documentViewerService) { _documentViewerHtmlService = documentViewerService; } Commented Apr 20, 2016 at 14:04
  • But your class name is DocumentViewerController right?How will the function DocumentViewerV1Controller gets called? Commented Apr 20, 2016 at 14:05

1 Answer 1

1

Yes, but you'll need to remove one of the constructors or tell it which one to use.

container.RegisterType<IViewerInformation, HtmlViewerInformation>();
container.RegisterType<IDocumentViewerService<IViewerInformation>, DocumentViewerServce<IViewerInformation>>();

You may / may not also need to initialize some of the constructors which you can do by

container.RegisterType<IDocumentViewerService<IViewerInformation>, DocumentViewerServce<IViewerInformation>>(
    new InjectionConstructor(...));
Sign up to request clarification or add additional context in comments.

4 Comments

I tried this way : container.RegisterType<IDocumentViewerService<HtmlViewInformation>, DocumentViewerService<HtmlViewInformation>>(new InjectionConstructor(typeof(IViewerHtmlRepository))); container.RegisterType<IDocumentViewerService<PageImage>, DocumentViewerService<PageImage>>(new InjectionConstructor(typeof(IViewerImageRepository))); Is there anything I am doing wrong?
You can only run the constructor once, which constructor do you want to use?
I want to use both the constructors depending on the calls which I will be making.
You can't do that, if you need both, supply them both in the same constructor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.