locked
HttpHandler in asp.net core - Response.BinaryWrite does not contain a definition RRS feed

  • Question

  • User-1159036174 posted

    Hi!

    I am traying change my old "RequestHandler.ashx" to asp.net core but now I am getting some problem with the migration, could any one help?

    Into the Statup.cs:

    app.MapWhen(context => context.Request.Path.ToString().EndsWith(".order"),
    appBuilder =>
    {
         appBuilder.UsePrintMiddleware(); 
    }); 

    On my Meddleware:

    public static class PrintMiddlewareHelper
    {
    public static IApplicationBuilder UsePrintMiddleware
    (this IApplicationBuilder builder)
    {
    return builder.UseMiddleware<PrintController>();
    }
    }

    On my controller:

    private RequestDelegate _next;

    public PrintController(RequestDelegate next)
    {
    _next = next;
    }

    public async Task Invoke(HttpContext context)
    {
    context.Response.ContentType = "application/octet-stream";

    // Pega número do pedido para criar o cupom
    var BytesValue = CriaResponse(context.Request.Path.Value.ToLower().Replace("/", "").Replace(".order", ""));
    context.Response.BinaryWrite(BytesValue);

    await _next.Invoke(context);
    }

    // Acessa PrinterUtility e gera dados do cupom para enviar à impressora térmica
    private byte[] CriaResponse(string idOrder)
    {
    PrinterUtility.EscPosEpsonCommands.EscPosEpson obj = new PrinterUtility.EscPosEpsonCommands.EscPosEpson();

    var BytesValue = Encoding.ASCII.GetBytes(string.Empty);
    //BytesValue = PrintExtensions.AddBytes(BytesValue, GetLogo(@"D://logo1.bmp"));
    BytesValue = PrintExtensions.AddBytes(BytesValue, obj.Separator());
    BytesValue = PrintExtensions.AddBytes(BytesValue, obj.CharSize.DoubleWidth6());
    BytesValue = PrintExtensions.AddBytes(BytesValue, obj.FontSelect.FontA());
    BytesValue = PrintExtensions.AddBytes(BytesValue, obj.Alignment.Center());
    BytesValue = PrintExtensions.AddBytes(BytesValue, Encoding.ASCII.GetBytes("Title\n"));
    BytesValue = PrintExtensions.AddBytes(BytesValue, obj.CharSize.DoubleWidth4());
    BytesValue = PrintExtensions.AddBytes(BytesValue, Encoding.ASCII.GetBytes("Sub Title\n"));
    BytesValue = PrintExtensions.AddBytes(BytesValue, obj.CharSize.Nomarl());
    BytesValue = PrintExtensions.AddBytes(BytesValue, obj.Separator());
    BytesValue = PrintExtensions.AddBytes(BytesValue, Encoding.ASCII.GetBytes("Invoice\n"));
    BytesValue = PrintExtensions.AddBytes(BytesValue, obj.Alignment.Left());
    BytesValue = PrintExtensions.AddBytes(BytesValue, Encoding.ASCII.GetBytes("Invoice No. : " + IdOrder+ "\n"));
    BytesValue = PrintExtensions.AddBytes(BytesValue, Encoding.ASCII.GetBytes("Date : 12/12/2015\n"));
    BytesValue = PrintExtensions.AddBytes(BytesValue, Encoding.ASCII.GetBytes("Itm Qty Net Total\n"));
    BytesValue = PrintExtensions.AddBytes(BytesValue, obj.Separator());
    BytesValue = PrintExtensions.AddBytes(BytesValue, string.Format("{0,-40}{1,6}{2,9}{3,9:N2}\n", "item 1", 12, 11, 144.00));
    BytesValue = PrintExtensions.AddBytes(BytesValue, string.Format("{0,-40}{1,6}{2,9}{3,9:N2}\n", "item 2", 12, 11, 144.00));
    BytesValue = PrintExtensions.AddBytes(BytesValue, obj.Alignment.Right());
    BytesValue = PrintExtensions.AddBytes(BytesValue, obj.Separator());
    BytesValue = PrintExtensions.AddBytes(BytesValue, Encoding.ASCII.GetBytes("Total\n"));
    BytesValue = PrintExtensions.AddBytes(BytesValue, Encoding.ASCII.GetBytes("288.00\n"));
    BytesValue = PrintExtensions.AddBytes(BytesValue, obj.Separator());
    BytesValue = PrintExtensions.AddBytes(BytesValue, obj.Lf());
    BytesValue = PrintExtensions.AddBytes(BytesValue, obj.Alignment.Center());
    BytesValue = PrintExtensions.AddBytes(BytesValue, obj.CharSize.DoubleHeight6());
    BytesValue = PrintExtensions.AddBytes(BytesValue, obj.BarCode.Code128("12345"));
    BytesValue = PrintExtensions.AddBytes(BytesValue, obj.QrCode.Print("12345", PrinterUtility.Enums.QrCodeSize.Grande));
    BytesValue = PrintExtensions.AddBytes(BytesValue, "-------------------Thank you for coming------------------------\n");
    BytesValue = PrintExtensions.AddBytes(BytesValue, obj.Alignment.Left());
    BytesValue = PrintExtensions.AddBytes(BytesValue, CutPage());

    return BytesValue;
    }

    On my another application:


    static void Main(string[] args)
    {
    string Parameter = args[0].ToLower().ToString().Replace(POSJobHandler.Properties.Settings.Default.ReplaceText.ToLower(),"");
    string Ext = POSJobHandler.Properties.Settings.Default.PostExtn;

    string url = POSJobHandler.Properties.Settings.Default.UrlPost + "/" + Parameter + "." + Ext;


    WebRequest request = WebRequest.Create(url);
    WebResponse response=request.GetResponse();


    string result;
    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
    {
    result = reader.ReadToEnd();
    }

    RawPrinterHelper.SendValueToPrinter(POSJobHandler.Properties.Settings.Default.PrinterName, Encoding.ASCII.GetBytes(result) );

    return;
    }

    Everything was working fine before moveing to asp.net core, but now, context.Response.BinaryWrite(BytesValue) isn't working anymore! (Response.BinaryWrite does not contain a definition)

    How can I do that now? Need I change to Json, for exemplo, and than convert to Binary as a result on my another application? 

    Monday, August 26, 2019 6:14 PM

Answers

  • User711641945 posted

    Hi Gutox,

    You could use the following method:

    context.Response.Body.Write(BytesValue);

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 27, 2019 3:21 AM

All replies