PDF Viewer Controller not working - Web-service is not listening

Hi there, 

I followed the instructions to create the serviceUrl for my Angular app as per this link: https://ej2.syncfusion.com/documentation/pdfviewer/how-to/create-pdfviewer-service/. 

It keeps giving me the following error message:
Web-service is not listening. PDF Viewer depends on web-service for all it's features. Please start the web service to continue.

However, if I use your serviceUrl (https://ej2services.syncfusion.com/production/web-services/api/pdfviewer) the PDFViewer works perfectly in my Angular app. See code below where I switch to your service URL and it works fine.

My API is up and running. I added the Controller with the same name as per the link above. 

What am I missing? Why does it work with your URL but not my own even when my API is up and running as per Swagger tests? Please will you assist. Thanks





<div class="content-wrapper" [style.visibility]="visibility">
  <ejs-pdfviewer id="pdfViewer" [serviceUrl]='service' [documentPath]='documentPath' style="height:640px;display:block"></ejs-pdfviewer>
</div>

loadPdfViewer(pdfDocument: Blob) {
    this.visibility = 'visible';
    // this.service = 'https://ej2services.syncfusion.com/production/web-services/api/pdfviewer';
    this.service = 'http://localhost:2440/api/pdfviewer';
    const reader = new window.FileReader();
    reader.readAsDataURL(pdfDocument);

    reader.onloadend = () => {
      const base64data = reader.result;

      const pdfviewer = (document.getElementById('pdfViewer'as any).ej2_instances[0];

      pdfviewer.load(base64data, null);
    };
  }

25 Replies 1 reply marked as answer

DM Dhivyabharathi Mohan Syncfusion Team February 25, 2021 09:59 AM UTC

Hi Junaid, 
 Thank you for contacting Syncfusion support. 
We have reproduced the reported issue while creating the PDF Viewer using the documentation link in our end. However we have created and shared the PDF Viewer web service and angular sample for your reference.  
  
Web Service Link:  
  
  
And, we suggest to configure the CORS Policy in web config file and update the global configuration in Global.asax file. Please refer to the below code snippet  
  
Configure the CORS policy in the web config file:  
<system.webServer>   
          <httpProtocol>  
                <customHeaders>  
                        <add name="Access-Control-Allow-Headers" value="accept, maxdataserviceversion, origin, x-requested-with, dataserviceversion,content-type" />  
                        <add name="Access-Control-Allow-Origin" value="*" />  
                </customHeaders>  
       </httpProtocol>  
  </system.webServer>  
  
Set the Global configuration in the Global.asax file:  
GlobalConfiguration.Configuration.Routes.MapHttpRoute(  
            name: "PdfViewerApi",  
           routeTemplate: "api/{controller}/{action}/{id}",  
           defaults: new { id = RouteParameter.Optional });  
  
  
        protected void Application_BeginRequest()  
        {  
            if (Request.Headers.AllKeys.Contains("Origin") && Request.HttpMethod == "OPTIONS")  
            {  
                Response.End();  
                Response.Flush();  
            }  
        }  
   
  
Kindly try this and revert to us with more details if you have any concerns. We will refresh the UG documentation on March 3rd weekly NuGet release. 
  
  
Regards, 
Dhivya. 



JU Junaid replied to Dhivyabharathi Mohan February 25, 2021 04:39 PM UTC

Hi Junaid, 
 Thank you for contacting Syncfusion support. 
We have reproduced the reported issue while creating the PDF Viewer using the documentation link in our end. However we have created and shared the PDF Viewer web service and angular sample for your reference.  
  
Web Service Link:  
  
  
And, we suggest to configure the CORS Policy in web config file and update the global configuration in Global.asax file. Please refer to the below code snippet  
  
Configure the CORS policy in the web config file:  
   
            
                  
                          
                          
                  
         
    
  
Set the Global configuration in the Global.asax file:  
GlobalConfiguration.Configuration.Routes.MapHttpRoute(  
            name: "PdfViewerApi",  
           routeTemplate: "api/{controller}/{action}/{id}",  
           defaults: new { id = RouteParameter.Optional });  
  
  
        protected void Application_BeginRequest()  
        {  
            if (Request.Headers.AllKeys.Contains("Origin") && Request.HttpMethod == "OPTIONS")  
            {  
                Response.End();  
                Response.Flush();  
            }  
        }  
   
  
Kindly try this and revert to us with more details if you have any concerns. We will refresh the UG documentation on March 3rd weekly NuGet release. 
  
  
Regards, 
Dhivya. 


Hi there,

I still get the following error:

Access to XMLHttpRequest at 'http://localhost:2440/api/pdfviewer/Load' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.




DM Dhivyabharathi Mohan Syncfusion Team February 26, 2021 10:22 AM UTC

Hi Junaid, 
 
We have created a new incident under your Direct Trac account. We suggest you follow up with the incident for further updates. Please log in using the below link.  
  
 
 
Regards, 
Dhivya.


LM Luke Mitchell April 22, 2021 02:40 AM UTC

I am getting the CORS error as well, and CORS is configured properly on the service and controller methods. Did you ever resolve this issue?


MS Mohan Selvaraj Syncfusion Team April 23, 2021 10:33 AM UTC

Hi Luke , 

We have tried to replicate the issue with the web-service and with the Angular sample provided in our previous update but we can't able to reproduce the issue. We suspect that the CORS policy is not been configured in the web config file. Please refer to the below code snippet   
   
Configure the CORS policy in the web config file:   
<system.webServer>    
          <httpProtocol>   
                <customHeaders>   
                        <add name="Access-Control-Allow-Headers" value="accept, maxdataserviceversion, origin, x-requested-with, dataserviceversion,content-type" />   
                        <add name="Access-Control-Allow-Origin" value="*" />   
                </customHeaders>   
       </httpProtocol>   
  </system.webServer>   
   

If you still face the issue, kindly provide the sample in which you have faced the issue. So that would be helpful for us to analyze more and assist you better. 

Regards, 
Mohan S 


Marked as answer

LR Lionel Raminhos July 6, 2021 02:16 PM UTC

The issue is based on NewtonSoft.Json not being used as this is an optional extra for .net core.

Further to this the system requires PascalCase, whereas camelCase is the default resolver.


So, in order to have this work, the following is required within the Startup.ConfigureServices function :

services.AddControllers().AddNewtonsoftJson(options => {

    // Use the default property (Pascal) casing

    options.SerializerSettings.ContractResolver = new DefaultContractResolver();

});


Once that is done then 'Web Services' are seen to be listening.

Please note that this has NOTHING to do with CORS policy and everything to do with not allowing different JSON configurations, i.e. the default Json service being System.Text.Json and further to this, the outputted Json must be in PascalCase, so either you need to do this globally, within the AddNewtonsoftJson call, or change the output format before submitting the Json



LR Lionel Raminhos July 6, 2021 02:31 PM UTC

I do understand that previously Pascal Case was the default, however for a while camelCase has been the default, as has System.Text.Json instead of NewtonsoftJson.


Why wouldn't this have been changed in SyncFusion to allow simpler implementation of the PDF Viewer.

I have already verified that I am able to load up a document on the server side using camelCase, however in order to furnish the data to the control the output itself has to be PascalCase



LR Lionel Raminhos July 6, 2021 10:36 PM UTC

So decided that although I have no issue with using NewtonSoft I wasn't happy with setting the default json ContractResolver to use PascalCase instead of camelCase.


With that in mind, I enabled NewtonSoft within the the Startup.ConfigureServices routine by using the following:

services.AddControllers().AddNewtonsoftJson();


thereafter within the PdfViewerController I created a private routine to return the Json as required:

private IActionResult json (object response) {

    return Json(response, new Newtonsoft.Json.JsonSerializerSettings {
        ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver()
    });
}


And then in all the other routines, where SyncFusion was passing back Json, 

 i.e: return Content(JsonConvert.SerializeObject(jsonResult));


I changed it to use the new private json routine:

return json(new PdfRenderer(cache).GetPage(jsonObject));


With that in mind, please note that I am not passing back Content where the content has been serialised into Json, I am rather using the routines provided by .net core Controllers to have the system serialise as required, simply passing in the serialisation options.


All is working now, and I have the benefit of all other Json responses still being in camelCase as expected based on standardisation.


I am not clear, then I have added the cs file so that it may be looked at.


Attachment: PdfViewerController_f8ed4c8b.rar


DR Desika Rajendran Syncfusion Team July 7, 2021 01:18 PM UTC

Hi Lionel, 
 
We are happy that your issue has been resolved at your end. However we will check further and update the same in the documentation. 
 
Regards, 
Desika R 



LR Lionel Raminhos August 5, 2021 02:29 PM UTC

Good day, please note that my issue was not resolved in it's entirely. 

I would still get intermittent failures stating that the API was not listening, or some documents would not load correctly, specifically documents that have landscape pages with images in them.


After much analysis and testing, I found that the most stable way to use this control was to change the PdfViewerController load function to not use the logic that you provided, but rather load the document myself into a byte array and then load up the document via a stream from that byte array,


Then instead of allowing the control to specify when to load itself, I changed the app to call the load myself from JavaScript when required, and also set the serviceUrl before calling the load instead of directly on the control itself.

This was the only way to get the control to work in a stable manner.


PLEASE NOTE

Simply changing the Load endpoint within the PdfViewerController was not sufficient to consistently remove the "Web-service is not listening" message.

I literally had to manage when to load the PDF Document via JavaScript calling the load function.

Although I could not debug the load function, I found that when I allowed the control to load itself, the Load endpoint was called, and data was returned correctly, however the RenderPdfPages endpoint would then no be called.

Once changed to the JavaScript manual load, then I found that the Load endpoint would be called, and subsequently so would the RenderPdfPages endpoint.


Here is an example of the load function that I am calling. Note that within this function I am setting the serviceUrl programmatically from a dataset value and passing the Id of the Pdf Document to the load function based on how we store the PDF's on the server. 

load: function () {        
    var pdfElement       = document.getElementById("ej2-pdf-viewer");        
    var pdfViewer        = pdfElement.ej2_instances[0];        
    pdfViewer.serviceUrl = pdfElement.dataset.serviceuri;

    pdfViewer.load(pdfElement.dataset.itemid);
}


DM Dhivyabharathi Mohan Syncfusion Team August 6, 2021 07:39 AM UTC

Hi Lionel, 
 
Thank you for your suggestion. Kindly revert us, if you want further assistance. 
 
Regards, 
Dhivya. 



AT atul November 26, 2021 12:05 PM UTC

hi syncfusion team i have an issue when run pdfviewer in my local host its working pdf load successfully but i host my website in server then pdfviewer not working why .......   and i found a message webservice not listening please start web service , please support me what a issue 




DM Dhivyabharathi Mohan Syncfusion Team November 29, 2021 12:17 PM UTC

Hi Atul, 
 
 
We suspect that issue is due to the write access permission denied in the environment. We will create the pdfium.dll based on the operating system at the runtime. So, could you please try copying the below provided x64 and x86 folder and paste inside the folder into your project to resolve the issue or enable the write permission for that folder?
  
 
    
Note:    
· Kindly use both the client and server-side of the same version in your project    
· Ensure whether the Pdfium dll will be created in your project during runtime. Else we can also place the pdfium assemblies in any of the production environment locations and refer to the path by using the ReferencePath API.    
    
For example:     
   
If the Pdfium assembly is available in this path C:\Pdfium\x64 or D:\Pdfium\x86, the reference path should be PdfRenderer.ReferencePath = "C:/";     
    
The parent folder has to be provided as the path in the ReferencePath API.     
Note: Provide this path in the Load method of the PDFViewerController.cs     
PdfRenderer PdfRenderer = new PdfRenderer();     
PdfRenderer.ReferencePath = @"C:/";     
  
Steps to refer the Pdfium.dll:   
  
1.Extract the given file (Pdfium folder) and copy it in the sample’s root directory (parallel to Controllers folder)   
  
2. Right-click on the pdfium.dll (both the X64 andX86 folder) and then choose the “Copy to Output Directory” property from the property window and set its value to “copy always”. This setting will ship the pdfium.dll assembly to the published location.    
   
Screenshot:    
     
   
3. Provide this path in the Load method of the PDFViewerController.cs      
  
Note: You need to refer the ParentFolder up to x64/x86 folder.   
  
PdfRenderer.ReferencePath = _hostingEnvironment.ContentRootPath + \\Pdfium\\ 
   
4. Build and publish the application.   
  
   
Also please install only the package related to that OS, then build and run the project in that platform. For Windows, Linux and OSX operating systems, use the following corresponding libraries:   
  
  • Syncfusion.EJ2.PdfViewer.AspNet.Core.Linux
  • Syncfusion.EJ2.PdfViewer.AspNet.Core.Windows          
  • Syncfusion.EJ2.PdfViewer.AspNet.Core.OSX
  
Kindly try this and let us know if the issue resolves or not. If not, please share the more details about the issue and environment details. This will be helpful to investigate further and provide the solution at the earliest.   
 
 
Regards, 
Dhivya. 



AT atul replied to Dhivyabharathi Mohan December 1, 2021 08:21 AM UTC

madam the solution you gave was successful but a new issue is coming webservice is not listening please start web service how can solve it..?

if I do the same thing on local server then pdf load successfully ,how can start webservice asp.net mvc


AT atul December 2, 2021 05:50 AM UTC

Document Reference pointer does not exist in the cache this erorr show console




DM Dhivyabharathi Mohan Syncfusion Team December 2, 2021 08:03 AM UTC

Hi Atul, 
  
 
 
Could you please confirm whether you are facing this issue only with the particular document? If yes, please share the PDF document in which you have reproduced the issue. And kindly share the response details in the network tab of the Load request and the hosting environment details. And we have shared the Asp.Net MVC web service for your reference. Kindly try the provided MVC web service and let us know whether you are facing the same issue with the provided web service. Kindly share the modified sample in which you are facing this issue. These details will be helpful for us to investigate further and assist you better. 
 
 
 
 
Regards, 
Dhivya. 



AT atul replied to Dhivyabharathi Mohan December 2, 2021 09:32 AM UTC

madam an error coming


Document Reference pointer does not exist in the cache this error show console



AT atul December 2, 2021 09:43 AM UTC

catche  error

     



LR Lionel Raminhos December 2, 2021 11:21 AM UTC

Good day Atul,

That error is quite common. If you are looking to simply display a PDF then I would suggest you use an iFrame or an Embed.


I have been trying for months to get the SyncFusion PDF Viewer working, and in Production it 8 out of 10 times provides me with the " Web-service is not listening" issue.


I truly would like to get this working consistently as I use other SyncFusion components and would like to keep my toolset consistent, but having no lock with this component.


I have tested with other PDF Management components for JavaScript and Blazor, and those work without issue, however the cost of the other services are exorbitant. 



DM Dhivyabharathi Mohan Syncfusion Team December 3, 2021 03:00 PM UTC

Hi Lionel, 
 
 
Sorry for the inconvenience. We suspect that the reported issue “Document Reference pointer does not exist in the cache” might occur due to the cache being cleared while rendering the PDF pages. In our PDF Viewer control, we store the document data in the cache for rendering the PDF pages. In a load balancing environment, the request will send to different server machines, in which the cache data will not be available. However, we have implemented the feature ‘Achieve load balancing environment in MVC framework’ and shared the PDF Viewer web-service sample. Kindly download it from the below link. Please find the steps to configure the cache in your application. 
 
    
 
      
Kindy refer the below documentation for more details about using the Redis cache in your end.   
 
    
 
 
Kindly try this and let us know if you have any concerns about this. 
 
 
Regards, 
Dhivya. 
 



AT atul replied to Dhivyabharathi Mohan December 8, 2021 10:38 AM UTC

madam how can set connection string in  IDatabase

i found a message this when set memory cache 


No connection is available to service this operation: GET PDF_INFOSYb6CdyKTdpLm0beysR9VA==; An existing connection was forcibly closed by the remote host; IOCP: (Busy=0,Free=1000,Min=4,Max=1000), WORKER: (Busy=1,Free=8190,Min=4,Max=8191), Local-CPU: n/a




DM Dhivyabharathi Mohan Syncfusion Team December 8, 2021 02:33 PM UTC

Hi Atul, 
 
We suspect that the Redis server is not started in your environment. So, the reported issue occurs. We suggest you follow the steps from the below link to start the Redis server and then configure the Redis in the MVC sample using the below link. 
 
 
 
Code snippet to set the connection string: 
 
public string redisconnectonstring = "xxxx ,syncTimeout=100000"; 
 
 
Commands in Redis terminal: 
 
keys *
flushall
 
 
 
 
Kindly try this and revert us, if you have any concerns. 
 
Regards, 
Dhivya. 



AT atul replied to Dhivyabharathi Mohan December 9, 2021 10:35 AM UTC

hi madam i follow all step connection problem resolve but even now  "Document Reference pointer does not exist in the cache" issue not resolve 



LR Lionel Raminhos replied to Dhivyabharathi Mohan December 9, 2021 11:14 AM UTC

Good day,


This would not be the case, The production environment that we loaded it on to was a simple physical server at a client site, there was no load balancing in place.


Upon testing I noticed that even on my dev machine, when the "debugger" was not attached the same issue would occur.


The point is moot though, I have had to remove the SyncFusion PDF Viewer from the solution that I was providing, and have gone another route.



DM Dhivyabharathi Mohan Syncfusion Team December 10, 2021 10:49 AM UTC

Hi Lionel, 
 
We have checked with the local MVC web service and we were unable to replicate the reported issue. We have shared the sample in which we tried to replicate this issue. 
 
 
Could you please try the provided web service and let us know whether you are facing the same issue with the provided sample. And this reported issue will occur only while using multiple servers. As we mentioned earlier, we need to configure the distributed Cache to get the issue resolved. If the issue is still not resolved, kindly share the modified sample in which you are facing this issue. So, that it will be helpful for us to investigate further and assist you better. 
 
 
Regards, 
Dhivya 


Loader.
Up arrow icon