Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

How to configure AutoMapper for another application #4831


User avatar
0
gep13 created

Hello,

I am trying to utilise the services, repositories, entity framework connection, that are used in the ASP.NET Zero Application, in another application. This is a Windows Service, and we want to use the same code to access the database that is used within the MVC application.

I have referenced all the necessary projects in my Windows Service, and I have been able to instantiate the service, and it successfully makes a call to the GetAll method. However, when it then tries to map the entity to the Dto, I get an error message:

Abp.ObjectMapping.IObjectMapper should be implemented in order to map objects

How can I go about passing the required mappings into the service layer for it to be used.

Thanks in advance.

Gary


16 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    From the documentation on Object To Object Mapping:

    AutoMapper Integration

    The Abp.AutoMapper NuGet package module implements the IObjectMapper and provides additional features.

    Installation

    First, install the Abp.AutoMapper NuGet package to your project:

    Install-Package Abp.AutoMapper
    

    Then add a dependency for AbpAutoMapperModule to your module definition class:

    [DependsOn(typeof(AbpAutoMapperModule))]
    public class MyModule : AbpModule
    {
       ...
    }
    

    You can then safely inject and use IObjectMapper in your code. You can also use AutoMapper's own API when you need it.


    <cite>gep13: </cite> How can I go about passing the required mappings into the service layer for it to be used.

    Refer to the Custom Mapping section.

  • User Avatar
    0
    gep13 created

    Thank you for taking the time to respond.

    I am still not sure if I understand...

    I have went ahead and created a Module as you suggested, but the PreInitialize and Initialize override methods are never called. In my Windows Service, I don't have the Castle Windsor DI System. Where should I configure the Abp Modules, so that they are initialized within my application?

    Thanks

    Gary

  • User Avatar
    0
    alper created
    Support Team

    Windows service must run AbpBootstrapper.

    Check out how to create ABP compatible Windows Service. <a class="postlink" href="https://stackoverflow.com/a/46502777/1767482">https://stackoverflow.com/a/46502777/1767482</a>

    Related docs: <a class="postlink" href="https://aspnetboilerplate.com/Pages/Documents/Module-System">https://aspnetboilerplate.com/Pages/Doc ... ule-System</a>

  • User Avatar
    0
    gep13 created

    Thank you for taking the time to respond.

    I am going to give this a try just now. Once I have initialized the bootstrapper as you have suggested, how can I then resolve an instance of a particular service, so use within my Windows Service?

    Thanks

  • User Avatar
    0
    gep13 created

    Based on my reading, I "think" the correct way to do this is:

    var bootstrapper = new AbpBootstrapper.Create<MySampleProjectApplicationModule>();
    bootstrapper.Initialize();
    

    and from there, I could do:

    var myService = bootstrapper.IocManager.Resolve<IMyService>();
    

    Is this the correct approach? When I try the above, I get an error message at the Initialize step:

    Can't create component 'Abp.BackgroundJobs.BackgroundJobStore' as it has dependencies to be satisfied.

    'Abp.BackgroundJobs.BackgroundJobStore' is waiting for the following dependencies:

    • Service 'Abp.Domain.Repositories.IRepository`2[[Abp.BackgroundJobs.BackgroundJobInfo, Abp, Version=3.4.0.0, Culture=neutral, PublicKeyToken=null],[System.Int64, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' which was not registered.

    Any ideas on what I need to do to correct this problem?

    I have added a reference to the Abp NuGet Package, which I thought was the issue, but looks like it is something else.

    Thanks in advance!

  • User Avatar
    0
    ismcagdas created
    Support Team

    @gep13 can you share the code of your windows service project's module ?

  • User Avatar
    0
    gep13 created

    @ismcagdas I have kept that module very simple, as per the example that was given.

    It contains the following

    [DependsOn(typeof(MySampleApplicationApplicationModule))]
        public class MySampleApplicationServiceModule : AbpModule
        {
            public override void Initialize()
            {
                IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
            }
        }
    
  • User Avatar
    0
    ismcagdas created
    Support Team

    @gep13,

    Can you also add a dependency to your Entity Framework module ?

    Thanks.

  • User Avatar
    0
    gep13 created

    @ismcagdas that gets me one step further, I think...

    The call to bootstrapper.Initialize() seems to start, but then it gets to this Initialize line in this code:

    public override void PostInitialize()
            {
                IocManager.RegisterIfNot<IChatCommunicator, NullChatCommunicator>();
    
                IocManager.Resolve<ChatUserStateWatcher>().Initialize();
                IocManager.Resolve<AppTimes>().StartupTime = Clock.Now;
            }
    

    Which is in the MySampleProjectCoreModule, and it just bombs out.

    Any ideas?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Probably IocManager.Resolve<AppTimes>() is null. You also need to depend on AbpAspNetZeroCoreModule I guess. Then, you will need to set license code in your custom module. You can take a look at Migrator project to see how to do that.

  • User Avatar
    0
    gep13 created

    @ismcagdas thank you for pointing me that the Migrator project. I was able to follow it's module, including the setup of the ServiceCollectionRegistrar, and I was able to get instances of my service being returned from the container.

    Thank you very much!

    As a follow up question though...

    When I try to call the GetAll method on one of my services, I get an exception saying:

    Current user did not login to the application.

    I have been able to workaround this for now by setting:

    Configuration.Authorization.IsEnabled = true;
    

    In my module, but this is far from ideal. Can you point me at where I would Login with a set of credentials to allow all interactions to happen using that user?

  • User Avatar
    0
    ismcagdas created
    Support Team

    If you want to make operations on behalf of a user, you can use <a class="postlink" href="https://aspnetboilerplate.com/Pages/Documents/Abp-Session#user-identifier">https://aspnetboilerplate.com/Pages/Doc ... identifier</a>.

  • User Avatar
    0
    gep13 created

    <cite>ismcagdas: </cite> If you want to make operations on behalf of a user, you can use <a class="postlink" href="https://aspnetboilerplate.com/Pages/Documents/Abp-Session#user-identifier">https://aspnetboilerplate.com/Pages/Doc ... identifier</a>.

    Sorry, I am not sure if I follow. Do you have an example of where/when this can be used? Within the Windows Service, I want to be able to "log in" as a user of the system, so that any modifications to the data can be tracked back to where they came form. In this case, we will likely create an account in the system that the Windows Service will use to login to the application, prior to making any changes.

    Thanks again!

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    If you are using app services remotely, you can use token based auth. Go to <a class="postlink" href="https://aspnetzero.com/Documents/Development-Guide">https://aspnetzero.com/Documents/Development-Guide</a> and select your project type. Then, in the opening page go to "TOKEN BASED AUTHENTICATION" section. After getting a token, you need to include it into request headers for every request you make to remote server.

    If you are using app services locally, then wrap your codes like below;

    using (_session.Use(idOfTheUser, idOfTheUsersTenant))
    {
    	// Do the actions of windows service here...
    }
    

    You don't have to login if you use _session.Use.

  • User Avatar
    0
    gep13 created

    ismcagdas thank you again! I have just given the _session.Use a try, and it seems to work exactly as I was hoping.

    Thanks again!

  • User Avatar
    0
    ismcagdas created
    Support Team

    Great :)