2017-11-08 16 views
0

Telerik REST APIがあり、クライアント側でhtml5 report-viewerを使用しています。レポートはHTML形式のレポートで正常に生成されています。今、私は同じAPIからのレポートをC#コンソールアプリケーションでリクエストしたいと思っています。私は検索しましたが、解決策はありませんでした。 C#コンソールアプリケーションを使用してレポートをリクエストする方法を提案してください。Telerik REST APIを使用してC#コンソールアプリケーションからレポートを要求する方法

html5 report-viewer Library

注:私はtelerikレポーティングでは非常に初心者です。

アップデート1:

私はこのAPIドキュメントを使用してサーバーにリクエストを送信するために管理しています。 Telerik Document for Getting Report

私はCustomReportResolverを書いています。しかし、現在はInstanceIdをコンソールクライアントに送信しています。

CustomReportResolver

public class CustomReportResolver : IReportResolver 
    { 
     public ReportSource Resolve(string reportJsonString) 
     { 
      var reportDto = JsonConvert.DeserializeObject<ReportDTO>(reportJsonString); 

      var connectionStringHandler = new CustomConnectionStringManager(reportDto.CompanyId); 


      var reportsPath = HttpContext.Current.Server.MapPath($"~/Reports/{reportDto.ReportPath}"); 


      var sourceReportSource = new UriReportSource { Uri = reportsPath + reportDto.ReportName }; 

     // sourceReportSource.Parameters.Add(new Telerik.Reporting.Parameter("companyId", reportDto.CompanyId)); 
      var reportSource = connectionStringHandler.UpdateReportSource(sourceReportSource); 
      return reportSource; 
     } 
    } 

私が正常にコンソールにPDFレポートを送信するデフォルトReportResolver自己ホスト型telerikサービスを使用しますが、私はCustomReportResolverを使用している場合、それはinstanceIdを生成していない場合。

何が問題なのですか?

答えて

1

多くの時間を無駄にした後、Telerik Self hosted Web ServiceからPDF(またはその他のレポート形式)のドキュメントを入手する方法を見つけました。以下は一般的な手順です。

  1. クライアントIDを取得します
  2. インスタンスIDを取得します
  3. ドキュメントIDを取得します
  4. ダウンロードドキュメント以下

段階的なコードです:

static HttpClient client = new HttpClient(); 
     static string reportServerAddress = "http://localhost:60031/"; 
     static string serverREStAPI = reportServerAddress + "api/"; 

     static void Main(string[] args) 
     { 
      try 
      { 
       Console.WriteLine("Demo started"); 

       RunAsync().Wait(); 

       Console.WriteLine("Demo ended"); 
       Console.ReadLine(); 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine(ex); 
       Console.ReadKey(); 
      } 
     } 

     static async Task RunAsync() 
     { 
      // readFile(); 
      // return; 
      client.BaseAddress = new Uri(serverREStAPI); 
      client.DefaultRequestHeaders.Accept.Clear(); 
      client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/html")); 



      /* 
      * Steps To get PDF documents from Telerik Self hosted Web Service 
      * Step 1) Get Client Id, 
      * Step 2) Get Instance Id 
      * Step 3) Get Document Id 
      * Step 4) Download Document 
      * 
      * */ 

      var clientId = await GetClientIdAsync(serverREStAPI + "reports/clients", "clientId"); 

      var instanceId = 
       await GetInstanceAsync(serverREStAPI + $"reports/clients/{clientId}/instances", "instanceId"); 
      var documentId = 
       await GetDocumentAsync(serverREStAPI + $"reports/clients/{clientId}/instances/{instanceId}/documents", 
        "documentId"); 

       await DownloadPDF(serverREStAPI + $"reports/clients/{clientId}/instances/{instanceId}/documents/{documentId}", true); 


     } 





     static async Task<string> GetClientIdAsync(string path, string paramName) 
     { 

      HttpResponseMessage response = await client.PostAsJsonAsync(path, ""); 
      response.EnsureSuccessStatusCode(); 

      var serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); 
      dynamic result = serializer.DeserializeObject(response.Content.ReadAsStringAsync().Result); 
      return result[paramName]; 
     } 

     static async Task<string> GetInstanceAsync(string path, string paramName) 
     { 

      /* 
      * For Default resolver in Service 
      * */ 
      var paramterValues = new {CompanyId = 1}; 
      // var data = new { report = "{ \"ReportName\":\"test.trdx\",\"CompanyId\":\"1\"}", parameterValues = "{\"CompanyId\": \"1\"}" };  
      var data = new 
      { 
       report = "{\"ReportName\":\"test.trdx\",\"CompanyId\":\"1\"}", 
       parameterValues = paramterValues 
      }; 





      HttpResponseMessage response = await client.PostAsJsonAsync(path, data); 
      response.EnsureSuccessStatusCode(); 

      var serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); 
      dynamic result = serializer.DeserializeObject(response.Content.ReadAsStringAsync().Result); 
      return result[paramName]; 
     } 

     static async Task<string> GetDocumentAsync(string path, string paramName) 
     { 



      var data = new {format = "PDF"}; //PDF,XLS,MHTML 
      HttpResponseMessage response = await client.PostAsJsonAsync(path, data); 
      response.EnsureSuccessStatusCode(); 

      var serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); 
      dynamic result = serializer.DeserializeObject(response.Content.ReadAsStringAsync().Result); 
      return result[paramName]; 
     } 



     static async Task DownloadPDF(string path, bool asAttachment) 
     { 
      var queryString = ""; 

      // if (asAttachment) 
      // { 
      // queryString += "?content-disposition=attachment"; 
      // } 



      var filePathAndName = @"D:\testing\tet.html"; 
      // File.Create(filePathAndName); 

      // string filePath = System.IO.Path.Combine(folderName, fileName); 

      //System.IO.File.WriteAllText(filePathAndName, result); 


      using (System.Net.WebClient myWebClient = new System.Net.WebClient()) 
      { 
       await myWebClient.DownloadFileTaskAsync(new Uri(path + queryString), filePathAndName); 
      } 



      System.Diagnostics.Process.Start(filePathAndName); 
     } 
は、
関連する問題