locked
Trying to Get Current UserName Using Generic Handler Template Not Working RRS feed

  • Question

  • Hi there!

    I'm trying to reproduce the Michael Washington HTML Client example from his blog An End-To-End Visual Studio LightSwitch 2013 HTML5 Application

    I'm using Windows 8.1 with VS2013 v3.

    When I try to get the current UserName the application freeze.

    For these, I created a class using VB.Net Generic Handler template to get the current UserName. The code is showed bellow:

    Imports System.Web
    Imports System.Web.Services
    Imports Microsoft.LightSwitch.Server
    Imports Microsoft.LightSwitch.Security
    
    Namespace LightSwitchApplication.Web
    
        Public Class GetUserName
            Implements System.Web.IHttpHandler
    
            Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
    
                Using serverContext As ServerApplicationContext = ServerApplicationContext.CreateContext()
                    context.Response.ContentType = "text/plain"
                    context.Response.Write(serverContext.Application.User.Name)
                End Using
    
            End Sub
    
            ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
                Get
                    Return False
                End Get
            End Property
    
        End Class
    
    End Namespace

    To call this code from the HTML client, I'm using the following code:

    myapp.Order.created = function (entity) {
        // Set the default date for the Order
        entity.OrderDate = new Date();
    
        // Using a Promise object we can call the CallGetUserName function
        msls.promiseOperation(CallGetUserName).then(function PromiseSuccess(PromiseResult) {
            // Set the result of the CallGetUserName function to the 
            // UserName of the entity
            entity.UserName = PromiseResult;
        });
    };
    
    // This function will be wrapped in a Promise object
    function CallGetUserName(operation) {
        $.ajax({
            type: 'post',
            data: {},
            url: '../Web/GetUserName.ashx',
            success: operation.code(function AjaxSuccess(AjaxResult) {
                operation.complete(AjaxResult);
            })
        });
    }

    After executing these code, the application freeze.
    What is wrong with my code?

    Best regards,



    Ciro




    • Edited by Oric.CQ Wednesday, August 13, 2014 10:58 AM
    Wednesday, August 13, 2014 2:01 AM

Answers

  • Hi, I found a solution:

    Using VB.Net Generic Handler template to get the current UserName, I don't know why, but you need to remove the Namespace, as showed bellow:

    Imports System.Web
    Imports System.Web.Services
    
    'Namespace LightSwitchApplication.Web
    
    Public Class GetUserName
        Implements System.Web.IHttpHandler
    
        Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
    
            Using serverContext As ServerApplicationContext = ServerApplicationContext.CreateContext()
                context.Response.ContentType = "text/plain"
                context.Response.Write(serverContext.Application.User.Name)
            End Using
    
        End Sub
    
        ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
            Get
                Return False
            End Get
        End Property
    
    End Class
    
    'End Namespace
    Best regards,


    Ciro

    • Marked as answer by Oric.CQ Thursday, September 11, 2014 12:19 PM
    Thursday, September 11, 2014 12:19 PM

All replies

  • Hi there!

    I've made some additional tests. Now, creating the same exemple using C#.

    It worked nice!

    I think the VB.NET TEMPLATE: Generic Handler is not working, or this template needs some additional code to work.

    Is it possible to get Server information using the template Generic Handler in VB.Net?

    Best regards,


    Ciro

    Wednesday, August 13, 2014 11:29 AM
  • Hi, I found a solution:

    Using VB.Net Generic Handler template to get the current UserName, I don't know why, but you need to remove the Namespace, as showed bellow:

    Imports System.Web
    Imports System.Web.Services
    
    'Namespace LightSwitchApplication.Web
    
    Public Class GetUserName
        Implements System.Web.IHttpHandler
    
        Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
    
            Using serverContext As ServerApplicationContext = ServerApplicationContext.CreateContext()
                context.Response.ContentType = "text/plain"
                context.Response.Write(serverContext.Application.User.Name)
            End Using
    
        End Sub
    
        ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
            Get
                Return False
            End Get
        End Property
    
    End Class
    
    'End Namespace
    Best regards,


    Ciro

    • Marked as answer by Oric.CQ Thursday, September 11, 2014 12:19 PM
    Thursday, September 11, 2014 12:19 PM