Reputation: 325
I have a problem with AutoMapper when I wanna start up my API. I have the following types:
[<CLIMutable>]
type User = {
[<BsonElement("_id")>]
[<JsonProperty("id")>]
Id : BsonObjectId
[<BsonElement("email")>]
[<JsonProperty("email")>]
Email : string
[<BsonElement("name")>]
[<JsonProperty("name")>]
Name : string
[<BsonElement("password")>]
[<JsonProperty("password")>]
Password : string
[<BsonElement("createdAt")>]
[<JsonProperty("createdAt")>]
[<BsonRepresentation(BsonType.DateTime)>]
CreatedAt : DateTime
[<BsonElement("updatedAt")>]
[<JsonProperty("updatedAt")>]
[<BsonRepresentation(BsonType.DateTime)>]
UpdatedAt : DateTime
}
type UserDto = {
[<JsonProperty("id")>]
Id : BsonObjectId
[<JsonProperty("email")>]
Email : string
[<JsonProperty("name")>]
Name : string
[<JsonProperty("createdAt")>]
CreatedAt : DateTime
[<JsonProperty("updatedAt")>]
UpdatedAt : DateTime
}
Also I have the following to configure the AutoMapper
// AutoMapping.fs
type AutoMapping =
inherit Profile
new () as this = AutoMapping() then
this.CreateMap<User, UserDto>() |> ignore
// AutoMapperExtensions.fs
module AutoMapperExtensions =
type IServiceCollection with
member this.AddMapperConfiguration(configuration: IConfiguration) =
let mapperConfig = new MapperConfiguration(fun mc -> mc.AddProfile(new AutoMapping()))
let mapper = mapperConfig.CreateMapper()
this.AddSingleton(mapper) |> ignore
this
// Startup.fs
member this.ConfigureServices(services: IServiceCollection) =
services.AddLocalization(fun options -> options.ResourcesPath <- "Resources") |> ignore
services.AddMapperConfiguration(this.Configuration) |> ignore
// another instructions
But when I run the dotnet run command just it print the following error: Stack overflow The problem is in the AutoMapping type, but I don't know if this is correct configured, that is, in C# it is like this:
public class AutoMapping : Profile
{
public AutoMapping()
{
CreateMap<User, UserDto>();
}
}
Upvotes: 1
Views: 112
Reputation: 17153
I don't know anything about AutoMapper, but the equivalent of your C# constructor is this:
type AutoMapping() as this =
inherit Profile
do this.CreateMap<User, UserDto>() |> ignore
Your code contains a secondary constructor that calls itself (resulting in stack overflow), but you don't need a secondary constructor at all in this case. Just create a primary constructor (by adding parens after the name of the type) and then initialize the object in a do
statement.
Upvotes: 2