Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8514483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:52:56+00:00 2026-06-11T04:52:56+00:00

I have an AutoMapper profile which currently depends on UrlHelper for the following mapping:

  • 0

I have an AutoMapper profile which currently depends on UrlHelper for the following mapping:

CreateMap<Post, OpenGraphModel>().ForMember(
    m => m.Title,
    x => x.MapFrom(p => p.Link.Title)
).ForMember(
    m => m.Description,
    x => x.MapFrom(p => p.Link.Description)
).ForMember(
    m => m.Image,
    x => x.MapFrom(p => p.Link.Picture)
).ForMember(
    m => m.Url,
    x => x.MapFrom(p => urlHelper.RouteUrl("PostShortcut", new { id = p.Id }, "http"))
);

This was fine up until when I wanted to reuse my AutoMapper profile in a context that’s outside of a web request.

I can think of three solutions for this, none of which really convinces me (no particular order):

  1. Remove the UrlHelper dependency altogether from the mapping profile.
    This would mean I now have to manually map the Url property of OpenGraphModel, which kind of defeats the purpose of using AutoMapper, in my opinion. I like my mappers being able to set all the properties I need in the destination object.

  2. Remove this mapping profile from non-web contexts, since non-web request context shouldn’t be mapping to view models directly anyways.
    In order to accomplish this, I would have to make my IMapper instances either transient, per web request (or thread), instead of singleton, which introduces additional overhead I do not want to introduce.

  3. A third option is to not use the IMapper implementation on non-web request contexts (i.e. jobs), but this seems pretty much out of the question, since I’m reusing components which end up requiring me to use the mapper.

I guess having two singleton mappers would be the one that makes most sense (which choose profiles based on the context), but I don’t know how I should do that, here’s my AutoMapper installer in it’s current form:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using AutoMapper;
using AutoMapper.Mappers;
using Castle.MicroKernel;
using Castle.MicroKernel.Registration;
using Castle.MicroKernel.SubSystems.Configuration;
using Castle.Windsor;

namespace Common
{
    internal sealed class AutoMapperInstaller : IWindsorInstaller
    {
        private readonly Type[] profileTypes;

        public AutoMapperInstaller(params Type[] profileTypes)
        {
            if (profileTypes == null)
            {
                throw new ArgumentNullException("profileTypes");
            }
            this.profileTypes = profileTypes;
        }

        public void Install(IWindsorContainer container, IConfigurationStore store)
        {
            IEnumerable<Assembly> assemblies = profileTypes.Select(t => t.Assembly).ToList();

            foreach (Assembly assembly in assemblies)
            {
                container.Register(
                    AllTypes
                        .FromAssembly(assembly)
                        .BasedOn(typeof (ITypeConverter<,>))
                        .WithServiceSelf()
                    );
            }

            foreach (Assembly assembly in assemblies)
            {
                container.Register(
                    Classes
                        .FromAssembly(assembly)
                        .BasedOn<Profile>()
                        .LifestyleTransient()
                    );
            }

            container.Register(
                Component
                    .For<ITypeMapFactory>()
                    .ImplementedBy<TypeMapFactory>()
                    .LifestyleTransient()
                );

            container.Register(
                Component
                    .For<IConfiguration, IConfigurationProvider>()
                    .UsingFactoryMethod(InstanceConfigurationStore)
                    .LifestyleTransient()
                );

            container.Register(
                Component
                    .For<IMappingEngine>()
                    .ImplementedBy<MappingEngine>()
                    .LifestyleTransient()
                );

            container.Register(
                Component
                    .For<IMapper>()
                    .ImplementedBy<Mapper>()
                    .DynamicParameters(
                        (k, parameters) => parameters["profileTypes"] = profileTypes
                    )
                    .LifestyleSingleton()
                );
        }

        private ConfigurationStore InstanceConfigurationStore(IKernel kernel)
        {
            ITypeMapFactory typeMapFactory = kernel.Resolve<ITypeMapFactory>();
            IEnumerable<IObjectMapper> mappers = MapperRegistry.AllMappers();

            return new ConfigurationStore(typeMapFactory, mappers);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T04:52:58+00:00Added an answer on June 11, 2026 at 4:52 am

    Resolved by mocking the RequestContext passed to UrlHelper in non-web request contexts.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following Automapper defintion: Mapper.CreateMap<IB.BusinessComponents.Data.LocationMaster, IB.Entites.Master.Location>(); Mapper.CreateMap<IB.BusinessComponents.Data.LocationMaster, IB.Entites.Master.Location>() .ForMember(destination => destination.Id, source
I have the following Map in AutoMapper: AutoMapper.Mapper.CreateMap<MySourceObject, Int32>() .ForMember(d => d, o =>
I have read here http://lostechies.com/jimmybogard/2009/09/18/the-case-for-two-way-mapping-in-automapper/ about how you probably shouldn't be trying to un-flatten
I have a problem with Automapper when I try use custom resolver which uses
I have a Domain object called User and UserCreateViewModel.Automapper is used for mapping from
I'm using Automapper and I have the following scenario: Class OrderModel has a property
I am using AUtomapper which I am very impressed with however, I have a
I am currently testing with AutoMapper, but i currently have a case where the
I am using .NET mapping library AutoMapper in my application, and I have a
so I have found a few questions on SO (for example: Automapper mapping list

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.