Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • jake williamson 207 posts 873 karma points
    May 18, 2022 @ 05:53
    jake williamson
    0

    Is it possible to use the IUmbracoMapper in a console application or azure function?!

    hey out there,

    TL;DR is this even possible, or am i better off using something different for my mapping that doesn't have a dependency on the umbraco core?

    i hadda feeling this would come back to bite me...

    we've a umbraco 9 site that has a service that maps pocos to models. to do this i've used the IUmbracoMapper interface.

    it works fine in the site, however i now need to use the same service in a console application and an azure function...

    ...and i can't figure out how to bind up the mappers i've written to the IUmbracoMapper interface!

    for example, this is in the site composer:

    builder.WithCollectionBuilder<Umbraco.Cms.Core.Mapping.MapDefinitionCollectionBuilder>().Add<MyMapDefinition>();
    

    but how do i get that into my function startup?! as a long shot, i tried this:

    [assembly: FunctionsStartup(typeof(MyProject.Functions.Startup))]
    namespace MyProject.Functions
    {
        public class Startup : FunctionsStartup
        {
            public override void Configure(IFunctionsHostBuilder builder)
            {
                builder.Services.AddSingleton<IMapDefinition, MyMapDefinition>();
            }
        }
    }
    

    the mapper is null when the service code is hit...

    any suggestions are, as ever, greatly received (if only to tell me 'jake, you're barking up the wrong tree, ditch the umbraco mapper and use something else).

    cheers, jake

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    May 18, 2022 @ 07:08
    Dave Woestenborghs
    0

    Hi Jake,

    I don't know if your azure function has Umbraco dependencies.

    This is what I use in my unit tests for mappings :

     var definitions = new List<IMapDefinition> { new TestMapping() };
    
     var mapper = new UmbracoMapper(new MapDefinitionCollection(() => definitions), Mock.Of<IScopeProvider>());
    

    So you will need a instance of IScopeProvider in your azure function.

    But if you have no dependencies on Umbraco maybe it's better to use a standalone mapping library.

    Dave

  • jake williamson 207 posts 873 karma points
    May 18, 2022 @ 09:05
    jake williamson
    0

    hi dave,

    thank you for the reply, much appreciated.

    i see what you mean and i had started having a look through https://github.com/umbraco/Umbraco-CMS/blob/v8/dev/src/Umbraco.Tests/Mapping/MappingTests.cs to see if there would be any pointers on how to set things up...

    i'd got as far as new UmbracoMapper() and was trying to figure out how to sort out the scope provider... the other problem being that most of my maps have services injected into them which also makes binding up new List<IMapDefinition> difficult as you have to manually inject things into the constructor (which you'd be doing with mocks in tests...)

    the other problem was i couldn't get my head around was then set up the IUmbracoMapper to accept the var mapper = new UmbracoMapper()...

    basically, i spent a good couple of hours going in circles and wrapping myself in knots!

    the trick at the moment is that the services and the azure function don't really need to talk to umbraco at all, i'd used the mapper out of convivence more than anything else...

    i really am beginning to think that i should write my own mapper?!

    unless... i can find someone... whos managed to get umbraco... working in a console app?! i swear that i read a blog post not so long ago where someone had managed to get it working with v8...

    cheers,

    jake

  • jake williamson 207 posts 873 karma points
    May 18, 2022 @ 09:15
    jake williamson
    0

    i thought i'd seen something about a console app and umbraco...

    callum whyte did a talk on something called 'sidecar':

    https://github.com/callumbwhyte/sidecar/tree/dev/src/Sidecar.ConsoleApp

    but i'm having a real hard time getting any details on it...

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    May 18, 2022 @ 09:13
    Dave Woestenborghs
    0

    Hi Jake,

    Maybe you can try out this : https://github.com/callumbwhyte/sidecar

    This meant to use Umbraco services in a console app or azure function.

    But if your function is not talking to Umbraco maybe it's better to write your own factory methods or mapping. Or use something like Automapper.

    Dave

  • jake williamson 207 posts 873 karma points
    May 18, 2022 @ 09:16
    jake williamson
    0

    ha! you got there before i'd refreshed the page ;)

    yup, sidecar just popped into my mind... i'll have a dig through...

  • Bogdan 2 posts 72 karma points
    Oct 09, 2023 @ 14:53
    Bogdan
    1

    Hello!

    I managed to make a console application work with Umbraco on .NET Core and I created a simple working example here:

    https://github.com/bcdragomir/Umbraco.Console

    It's not using Sidecar, just plain .NET stuff.

  • jake williamson 207 posts 873 karma points
    Oct 11, 2023 @ 00:59
    jake williamson
    0

    nice work!

Please Sign in or register to post replies

Write your reply to:

Draft