Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. public class ReportService : Service
  2. {
  3. public IReportRepository Repository {get; set;}
  4.  
  5. public object Get(GetInvoiceReport request)
  6. {
  7. var invoices = Repository.GetInvoices();
  8.  
  9. ExcelReport report = new ExcelReport();
  10. byte[] bytes = report.Generate(invoices);
  11.  
  12. return new FileResult(bytes);
  13. }
  14. }
  15.  
  16. public class FileResult : IHasOptions, IStreamWriter, IDisposable
  17. {
  18. private readonly Stream _responseStream;
  19. public IDictionary<string, string> Options { get; private set; }
  20.  
  21. public BinaryFileResult(byte[] data)
  22. {
  23. _responseStream = new MemoryStream(data);
  24.  
  25. Options = new Dictionary<string, string>
  26. {
  27. {"Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
  28. {"Content-Disposition", "attachment; filename="InvoiceFile.xlsx";"}
  29. };
  30. }
  31.  
  32. public void WriteTo(Stream responseStream)
  33. {
  34. if (_responseStream == null)
  35. return;
  36.  
  37. using (_responseStream)
  38. {
  39. _responseStream.WriteTo(responseStream);
  40. responseStream.Flush();
  41. }
  42. }
  43.  
  44. public void Dispose()
  45. {
  46. _responseStream.Close();
  47. _responseStream.Dispose();
  48. }
  49. }
  50.  
  51. [Test]
  52. public void TestInvoiceReport()
  53. {
  54. var client = new JsonServiceClient("http://localhost/report/");
  55.  
  56. var authResponse = client.Send(new Authenticate
  57. {
  58. provider = CredentialsAuthProvider.Name,
  59. UserName = "[User Name]",
  60. Password = "[Password]"
  61. });
  62.  
  63. var requestDTO = new GetInvoiceReport();
  64.  
  65. var ret = client.Get<FileResult>(requestDTO);
  66.  
  67. Assert.IsTrue(ret != null);
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement