Search code examples
azureentity-frameworkazure-service-fabricstateful

Service Fabric Application with Entity Framework?


I started learning Service Fabric applications, and little confused about stateful Reliable Services.

In stateful Reliable Services state means the data to be stored in the tables in our normal database applications or something else?

Is it possible to use EF with stateful Reliable Services ?

How we can store/retrieve the data to/from database (like Products, Categories, Employess etc...) using EF in Reliable Services?

Any tutorial/help will be much appreciable.

Thanks in advance


Solution

  • There are 2 flavors of reliable services, stateless and stateful. The main difference being that stateful services give access to reliable collections to store your data.

    TL;DR

    If you are planning to use Entity Framework (EF) and you have no plan for storing data using reliable collections, stick to stateless services.

    Q1

    In stateful Reliable Services state means the data to be stored in the tables in our normal database applications or something else?

    It means you are planning to store the data in Reliable Collections.

    Q2

    Is it possible to use EF with stateful Reliable Services ?

    Yes, even when you use a stateful service you can write logic to store data in EF, and optionally store data in reliable collections (See the use case presented by Oleg in the comments for example) but if you only want to use EF then go for a stateless service. A stateful service only makes sense if you use reliable collections.

    Q3

    How we can store/retrieve the data to/from database (like Products, Categories, Employess etc...) using EF in Reliable Services?

    Create a stateless service, add the EF NuGet packages and write the code as you would normally do.

    Additional information

    From this quickstart

    A stateless service is a type of service that is currently the norm in cloud applications. It is considered stateless because the service itself does not contain data that needs to be stored reliably or made highly available. If an instance of a stateless service shuts down, all of its internal state is lost. In this type of service, state must be persisted to an external store, such as Azure Tables or a SQL database, for it to be made highly available and reliable.

    and

    Service Fabric introduces a new kind of service that is stateful. A stateful service can maintain state reliably within the service itself, co-located with the code that's using it. State is made highly available by Service Fabric without the need to persist state to an external store.

    Reliable Collection can be best described as a No-Sql data store. It is up to you if you want to use this, or have a mix between stateful and stateless services.

    For a more in-depth overview of Reliable Collections, read this doc