1

I already use ServiceStack OrmLite for quering my database, but now I want to create some CRUD API's myself. I have a separate project where my POCO's are declared with the ServiceStack DataAnnotations.

An example class of my POCO project:

    public class DropPosition
    {        
        [PrimaryKey, AutoIncrement]
        public long Id { get; set; }
        public int PositionId { get; set; }
        public string PositionBarcode { get; set; }
        public string BoxBarcode { get; set; }
        public int? OrderReference { get; set; }
        public DateTime? StartTime { get; set; }
        public DateTime? EndTime { get; set; }
        public bool ActivateLight { get; set; }

        [Reference]
        public virtual List<WorkOrder> WorkOrders { get; set; } = new List<WorkOrder>();
    }

Now I've created a new web project so I can serve my API endpoints using AutoQuery, and extended the POCO class:

    public class DropPosition
    {        
        [PrimaryKey, AutoIncrement]
        public long Id { get; set; }
        public int PositionId { get; set; }
        public string PositionBarcode { get; set; }
        public string BoxBarcode { get; set; }
        public int? OrderReference { get; set; }
        public DateTime? StartTime { get; set; }
        public DateTime? EndTime { get; set; }
        public bool ActivateLight { get; set; }

        [Reference]
        public virtual List<WorkOrder> WorkOrders { get; set; } = new List<WorkOrder>();
    }

    [Route("/DropPositions", "GET")]
    [Route("/DropPositions/{Id}", "GET")]
    public class QueryDropPositions : QueryDb<DropPosition>
    {
        public long Id { get; set; }
        public int PositionId { get; set; }
        public string PositionBarcode { get; set; }
        public string BoxBarcode { get; set; }
        public int? OrderReference { get; set; }
        public bool ActivateLight { get; set; }
    }

    [Route("/DropPositions", "POST")]
    public class CreateDropPosition : ICreateDb<DropPosition>, IReturn<DropPosition>
    {
        public int PositionId { get; set; }
        public string PositionBarcode { get; set; }
        public string BoxBarcode { get; set; }
        public int? OrderReference { get; set; }
        public DateTime? StartTime { get; set; }
        public DateTime? EndTime { get; set; }
        public bool ActivateLight { get; set; }
    }

    [Route("/DropPositions/{Id}", "PUT")]
    public class UpdateDropPosition : IUpdateDb<DropPosition>, IReturn<DropPosition>
    {
        public long Id { get; set; }
        public int PositionId { get; set; }
        public string PositionBarcode { get; set; }
        public string BoxBarcode { get; set; }
        public int? OrderReference { get; set; }
        public DateTime? StartTime { get; set; }
        public DateTime? EndTime { get; set; }
        public bool ActivateLight { get; set; }
    }

    [Route("/DropPositions/{Id}", "DELETE")]
    public class DeleteDropPosition : IDeleteDb<DropPosition>, IReturnVoid
    {
        public long Id { get; set; }
    }

And then followed the best pratice examples like this and referenced my POCO project.

    public class AppHost: AppHostBase, IHostingStartup
    {
        public AppHost() : base("AutoQuery", typeof(ServiceStackService).Assembly)
        {
        }

        public override void Configure(Container container)
        {
            // configure ServiceStack only IOC, Config & Plugins
            SetConfig(new HostConfig
            {
                DebugMode = true
            });

            // auto query
            Plugins.Add(new AutoQueryFeature
            {
                MaxLimit = 1000,
                //GenerateCrudServices = new GenerateCrudServices
                //{
                //    AutoRegister = true
                //}
            });
         
        }

        public void Configure(IWebHostBuilder builder)
        {
            builder.Configure(app =>
            {
                if (!HasInit)
                {
                    app.UseServiceStack(new AppHost());
                }

            });

            builder.ConfigureServices((context, services) =>
            {
                // ormlite               
                var sorterConnectionFactory = new OrmLiteConnectionFactory(ConnectionStrings.SorterLocal, SqlServerDialect.Provider);

                // unicode              
                sorterConnectionFactory.DialectProvider.GetStringConverter().UseUnicode = true;
              
                // register
                services.AddSingleton<IDbConnectionFactory>(sorterConnectionFactory);

            });
        }
    }

But I can't see my new API endpoints using the api explorer... But when I activate the GenerateCrudServices options, I see the auto-generated API endpoints. Did I forgot something? Should I change my code for only using my own declared AutoQuery CRUD API's?

api explorer

I've tried to follow the example BookingsCrud

Nesse
  • 373
  • 4
  • 14
  • This looks right, are the DTOs in the ServiceModel project? Can you upload a repro to GitHub somewhere so I can take a look. You can swap out any DBs for Sqlite. – mythz Jul 12 '23 at 14:06
  • @mythz The DTOs are located in another project. Could That be the problem? We've started our solution way back using EF6 and along the way we've replaced this with ServiceStack. But the structure remained the same. – Nesse Jul 12 '23 at 14:38
  • Yeah ServiceStack needs to be able to find them, all DTOs should be in the ServiceModel project – mythz Jul 12 '23 at 18:38
  • 1
    @mythz I found the problem. My AppHost file was using the wrong assemblies. I did use ***ServiceStack.Service***, but that wasn't working. Also using the assemblies of an empty ServiceInterface does not work. So for now I'm letting the default MyServices and Any Method as it is. – Nesse Jul 13 '23 at 08:27

0 Answers0