Giter Site home page Giter Site logo

starcounter-async's Introduction

Starcounter Async Extensions

Available on nuget: Install-Package Joozek78.Starcounter.Async -Version 1.0.0

To introduce asynchronicity in Starcounter TypedJSON view-model, you have to use callbacks and handle errors carefully:

public void Handle(Input.StartWorkTrigger input)
{
    this.IsBusy = true;
    Task.Run(LengthyJob)
        .ContinueWith(task => Session.ScheduleTask(Session.Current.SessionId,
        (Session session, string sessionId) => {
            // might happen if this code is executed after the session has been destroyed
            if (session == null)
            {
                return;
            }
            try
            {
                // if LengthyJob resulted in exception, it will be unwrapped here
                this.Result = task.Result;
            }
            catch (Exception e)
            {
                this.Result = "Error";
            }
            finally
            {
                this.IsBusy = false;
                // otherwise the changes won't be immediately visible to the client
                session.CalculatePatchAndPushOnWebSocket();
            }
        }));
}

This library allows you to simplify this code by using async-await

public void Handle(Input.StartWorkTrigger input)
{
    AsyncInputHandlers.Run(async () =>
    {
        this.IsBusy = true;
        try
        {
            this.Result = await LengthyJob();
        }
        catch(Exception e)
        {
            this.Result = "Error";
        }
        finally
        {
            this.IsBusy = false;
        }
    });
}

starcounter-async's People

Contributors

joozek78 avatar

Watchers

 avatar

Forkers

starcounter

starcounter-async's Issues

MissingMethodException: Method Starcounter.Session.get_ActiveWebSocket() not found

During work with Kitchensink app We noticed that pages that use AsyncInputHandlers methods stopped working.

Reproduction:

  1. Run Kitchensink app branch master-2.4
  2. Go to http://localhost:8080/KitchenSink/ProgressBar
  3. Click on the download (imaginary) file!.

Exception that occurs after that:

System.Exception: ScErrUnhandledException (SCERR1069): An unhandled exception was detected.
Version: 2.4.0.6025.
Help page: https://docs.starcounter.io/?q=SCERR1069.
   at StarcounterInternal.Hosting.Processor.<>c__DisplayClass3_0.<RunTaskNoScheduling>b__0() in C:\TeamCity\BuildAgent\work\sc-17818\Level1\src\Starcounter.Hosting\StarcounterInternal.Hosting\Processor.cs:line 139
   at Starcounter.Internal.StarcounterEnvironment.RunWithinApplicationInternal(String desiredAppName, Action action) in C:\TeamCity\BuildAgent\work\sc-17818\Level1\src\Starcounter.Internal\Mock\StarcounterEnvironment.cs:line 146
   at StarcounterInternal.Hosting.Processor.RunTaskNoScheduling(Action userAction, String appName) in C:\TeamCity\BuildAgent\work\sc-17818\Level1\src\Starcounter.Hosting\StarcounterInternal.Hosting\Processor.cs:line 122
   at Starcounter.DbSession.GetAndExecuteQueuedTasks(Action`2 taskExecutionMethod) in C:\TeamCity\BuildAgent\work\sc-17818\Level1\src\Starcounter\DbSession.cs:line 181
   at StarcounterInternal.Hosting.Processor.RunTask(IntPtr hTask) in C:\TeamCity\BuildAgent\work\sc-17818\Level1\src\Starcounter.Hosting\StarcounterInternal.Hosting\Processor.cs:line 169
   at StarcounterInternal.Hosting.Processor.RunMessageLoop(Void* hsched) in C:\TeamCity\BuildAgent\work\sc-17818\Level1\src\Starcounter.Hosting\StarcounterInternal.Hosting\Processor.cs:line 82
HResult=-2146233088
HelpLink=https://docs.starcounter.io/?q=SCERR1069
---> System.MissingMethodException: Method not found: 'Starcounter.WebSocket Starcounter.Session.get_ActiveWebSocket()'.
   at Joozek78.Star.Async.Internal.StarcounterSessionSynchronizationContext.<Post>b__5_0(Session session, String sessionId)
   at Starcounter.Session.Use(Action action)
   at Starcounter.DbSession.<>c__DisplayClass5_0.<RunAsync>b__0() in C:\TeamCity\BuildAgent\work\sc-17818\Level1\src\Starcounter\DbSession.cs:line 217
   at Starcounter.Task.Run() in C:\TeamCity\BuildAgent\work\sc-17818\Level1\src\Starcounter\DbSession.cs:line 101
HResult=-2146233069

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.