GithubHelp home page GithubHelp logo

Comments (4)

jviau avatar jviau commented on May 26, 2024 1

You can workaround this by using an EnsureInitializeAsync pattern. InitializeState will kick off the task to populate the state, and then each operation will await that task.

private readonly Task? _initialization;

public async Task MyOperation()
{
    await EnsureInitializedAsync();
}

protected T InitializeState(..)
{
   async InitializeAsync()
   {
        // async work
   }

    _initialization = InitializeAsync();
    return default(T); // InitializeAsync 
}

private Task InitializeAsync()
{
    // perform async work
    this.State = T; // set this.State
}

private ValueTask EnsureInitializedAsync()
{
    return _initialization is null ? default : new(_initialization);
}

from durabletask-dotnet.

jviau avatar jviau commented on May 26, 2024 1

@gorillapower

Yes, returning default(T) would be fine there. InitializeState is just a convenience method for populating state when there is none. I updated my sample to show that.

You could also set it in InitializeState and then populate its contents in InitializeAsync.

from durabletask-dotnet.

jhinm112 avatar jhinm112 commented on May 26, 2024

Thread is also blocked in my initializeState because async operations are not supported

from durabletask-dotnet.

gorillapower avatar gorillapower commented on May 26, 2024

Thanks @jviau. Would this pattern still work with this method signature?

protected override T InitializeState(TaskEntityOperation operation){}

as I need to return T not void (as the entity inherites from TaskEntity<T>) and it looks like the base class will store the returned value in the base.State property.

Full Code Example

public class T : TaskEntity<T>
{
    public void MyMethod() { }
    
    [Function(nameof(T))]
    public static Task RunEntityAsync([EntityTrigger] TaskEntityDispatcher dispatcher)
    {
        return dispatcher.DispatchAsync<T>();
    }
    
    protected override T InitializeState(TaskEntityOperation operation)
    {
        //inject EnsureInitializeAsync pattern here?
        T state = new();
        return state; 
    }
}

Also worth noting, an additional alternative would be to use the functions based syntax which would allow the populating of the state inside the DispatchAsync()

from durabletask-dotnet.

Related Issues (20)

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.