Open external program via mvc5

0

I'm having a hard time making an operation. I have a console application that opens a PDF. This console takes the name Servico.exe, although it has the service name, it is an exe and not a service. This open the PDF is ok. I created an MVC5 project. I used ADO .Net Entity Framework which led colleague Morrison to assert that this was a bad approach, but as it is just a POC I went ahead and then I see another approach. My difficulty is to open the program that calls the report via the controller or view. Below is the controller that should do this. This controller was generated by the VS2013 wizard. I can not build an action or a method that does this.

public class AppealReportController : Controller
    {
        private ReportDBContext db = new ReportDBContext();

        // GET: AppealReport
        public async Task<ActionResult> Index()
        {
            var pOC_SOLIC_RELATORIO = db.POC_SOLIC_RELATORIO.Include(p => p.POC_RELATORIO);
            return View(await pOC_SOLIC_RELATORIO.ToListAsync());
        }

        // GET: AppealReport/Details/5
        public async Task<ActionResult> Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            POC_SOLIC_RELATORIO pOC_SOLIC_RELATORIO = await db.POC_SOLIC_RELATORIO.FindAsync(id);
            if (pOC_SOLIC_RELATORIO == null)
            {
                return HttpNotFound();
            }
            return View(pOC_SOLIC_RELATORIO);
        }

        // GET: AppealReport/Create
        public ActionResult Create()
        {
            ViewBag.ID_RELATORIO = new SelectList(db.POC_RELATORIO, "ID_RELATORIO", "NM_RELATORIO");
            return View();
        }

        // POST: AppealReport/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create([Bind(Include = "ID_SOLIC_RELATORIO,ID_RELATORIO,ID_USUARIO,DT_SOLICITACAO,DT_AGENDAMENTO,DT_GERACAO,BL_RELATORIO")] POC_SOLIC_RELATORIO pOC_SOLIC_RELATORIO)
        {
            if (ModelState.IsValid)
            {
                db.POC_SOLIC_RELATORIO.Add(pOC_SOLIC_RELATORIO);
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }

            ViewBag.ID_RELATORIO = new SelectList(db.POC_RELATORIO, "ID_RELATORIO", "NM_RELATORIO", pOC_SOLIC_RELATORIO.ID_RELATORIO);
            return View(pOC_SOLIC_RELATORIO);
        }

        // GET: AppealReport/Edit/5
        public async Task<ActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            POC_SOLIC_RELATORIO pOC_SOLIC_RELATORIO = await db.POC_SOLIC_RELATORIO.FindAsync(id);
            if (pOC_SOLIC_RELATORIO == null)
            {
                return HttpNotFound();
            }
            ViewBag.ID_RELATORIO = new SelectList(db.POC_RELATORIO, "ID_RELATORIO", "NM_RELATORIO", pOC_SOLIC_RELATORIO.ID_RELATORIO);
            return View(pOC_SOLIC_RELATORIO);
        }

        // POST: AppealReport/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Edit([Bind(Include = "ID_SOLIC_RELATORIO,ID_RELATORIO,ID_USUARIO,DT_SOLICITACAO,DT_AGENDAMENTO,DT_GERACAO,BL_RELATORIO")] POC_SOLIC_RELATORIO pOC_SOLIC_RELATORIO)
        {
            if (ModelState.IsValid)
            {
                db.Entry(pOC_SOLIC_RELATORIO).State = EntityState.Modified;
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            ViewBag.ID_RELATORIO = new SelectList(db.POC_RELATORIO, "ID_RELATORIO", "NM_RELATORIO", pOC_SOLIC_RELATORIO.ID_RELATORIO);
            return View(pOC_SOLIC_RELATORIO);
        }

        // GET: AppealReport/Delete/5
        public async Task<ActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            POC_SOLIC_RELATORIO pOC_SOLIC_RELATORIO = await db.POC_SOLIC_RELATORIO.FindAsync(id);
            if (pOC_SOLIC_RELATORIO == null)
            {
                return HttpNotFound();
            }
            return View(pOC_SOLIC_RELATORIO);
        }

        // POST: AppealReport/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> DeleteConfirmed(int id)
        {
            POC_SOLIC_RELATORIO pOC_SOLIC_RELATORIO = await db.POC_SOLIC_RELATORIO.FindAsync(id);
            db.POC_SOLIC_RELATORIO.Remove(pOC_SOLIC_RELATORIO);
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }

I tried this and it did not work:

public void openApplication()
        {
            System.Diagnostics.Process.Start("C:\Projetos\Servicos\bin\Servicos.exe");
        }

And I made this call:

[HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create([Bind(Include = "ID_SOLIC_RELATORIO,ID_RELATORIO,ID_USUARIO,DT_SOLICITACAO,DT_AGENDAMENTO,DT_GERACAO,BL_RELATORIO")] POC_SOLIC_RELATORIO pOC_SOLIC_RELATORIO)
        {
            if (ModelState.IsValid)
            {
                db.POC_SOLIC_RELATORIO.Add(pOC_SOLIC_RELATORIO);
                openApplication();//Aqui não funfa
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }

            ViewBag.ID_RELATORIO = new SelectList(db.POC_RELATORIO, "ID_RELATORIO", "NM_RELATORIO", pOC_SOLIC_RELATORIO.ID_RELATORIO);
            return View(pOC_SOLIC_RELATORIO);
        }
    
asked by anonymous 06.11.2015 / 13:43

1 answer

2

Basically, what you do is the same as Rotating does, so I'll reproduce the method that invokes the service with some changes for your case, reads the output of it and converts it into an array of bytes, which is the content of the PDF:

    protected static byte[] AbrirExecutavelExtrairPdf()
    {
        var proc = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = @"C:\Projetos\Servicos\bin\Servicos.exe",
                Arguments = "",
                UseShellExecute = false,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                RedirectStandardInput = true,
                WorkingDirectory = @"C:\Projetos\Servicos\bin",
                CreateNoWindow = true
            }
        };
        proc.Start();

        using (var ms = new MemoryStream())
        {
            using (var sOut = proc.StandardOutput.BaseStream)
            {
                byte[] buffer = new byte[4096];
                int read;

                while ((read = sOut.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }
            }

            string error = proc.StandardError.ReadToEnd();

            if (ms.Length == 0)
            {
                throw new Exception(error);
            }

            proc.WaitForExit();

            return ms.ToArray();
        }
    }
    
06.11.2015 / 14:31