Arkobat
Arkobat
CC#
Created by Arkobat on 10/23/2023 in #help
❔ EF Core not getting all entities
No description
5 replies
CC#
Created by Arkobat on 7/16/2023 in #help
✅ Serializing a dictonary without path in root object
Hello, I'm trying to create a DTO for an API, where the request takes a dictornary of values, but the "keys" are part of the root object, and not under a path.
{
// Defined key value pairs
"name": "Foo",
"path": "Bar",

// A map of language code to content
"en": {},
"de": {},
"fr": {},
}
{
// Defined key value pairs
"name": "Foo",
"path": "Bar",

// A map of language code to content
"en": {},
"de": {},
"fr": {},
}
I'm therefor not sure how I can create a C# DTO, where the json output will like the example. If I serilize the below, the language codes will be languageMap.en etc, which does not match the request object
public class MyDto
{
[JsonPropertyName("name")] public string Name { get; set; } = null!;
[JsonPropertyName("path")] public string Path { get; set; } = null!;
public Dictionary<string, object> LanguageMap { get; set; } = null!;
}
public class MyDto
{
[JsonPropertyName("name")] public string Name { get; set; } = null!;
[JsonPropertyName("path")] public string Path { get; set; } = null!;
public Dictionary<string, object> LanguageMap { get; set; } = null!;
}
Is there a solution on how to serilize the dictionary into the root object, without ot being under the path?
2 replies
CC#
Created by Arkobat on 11/21/2022 in #help
❔ Callback consumer to async Task
Hello I'm trying to convert a callback based method to an async task. The basic idea is I have a message bus where I can send messages. Some of these messages require a response from the other end. I can easily do this with a callback based system, but I would like to convert it to a async/await. I have created this example (I know the code is invalid, it just serves as a sketch). Is there a standardised way to do this? I have not been able to find it on stackoverflow.
public class MessageService
{

private readonly IMessageProvider_messageProvider;
private readonly Dictionary<Guid, Action<TResponse>> _dictionary = new();

public TResponse Send<TMessage, TResponse>(TMessage payload, Action<TResponse> func) where TMessage : class
{
var message = new Message<TMessage>
{
ResponseChannel = "my-channel",
Payload = payload
};

_dictionary[message.MessageId] = func;
_messageProvider.Publish(message);
}

public void OnMessage<T>(Response<T> response) where T : class
{
if (!_dictionary.TryGetValue(response.MessageId, out var action)) return;

action!.Invoke(response.Payload);
}

public async Task<TResponse> SendAsync<TMessage, TResponse>(TMessage payload, CancellationToken cancellationToken)
{
// TODO
throw new NotImplementedException();
}

}
public class MessageService
{

private readonly IMessageProvider_messageProvider;
private readonly Dictionary<Guid, Action<TResponse>> _dictionary = new();

public TResponse Send<TMessage, TResponse>(TMessage payload, Action<TResponse> func) where TMessage : class
{
var message = new Message<TMessage>
{
ResponseChannel = "my-channel",
Payload = payload
};

_dictionary[message.MessageId] = func;
_messageProvider.Publish(message);
}

public void OnMessage<T>(Response<T> response) where T : class
{
if (!_dictionary.TryGetValue(response.MessageId, out var action)) return;

action!.Invoke(response.Payload);
}

public async Task<TResponse> SendAsync<TMessage, TResponse>(TMessage payload, CancellationToken cancellationToken)
{
// TODO
throw new NotImplementedException();
}

}
7 replies
CC#
Created by Arkobat on 9/13/2022 in #help
Replying with Rebus
I'm trying to develop an application, where services communicates via Rebus, with RabbitMQ is transporter. I do however have some messages, I would like to reply to, e.g. retrieving X DTO. However, I cannot find any documentation on how to reply in Rebus, so I'm wondering if this is even possible. It should be possible in RabbitMQ, but I would love if it was something I could do directly with Rebus Does anyone have any experience with this, that can help me the right way?
1 replies
CC#
Created by Arkobat on 8/15/2022 in #help
Custom JSON serializer not working
Hello I have a custom serializer i cannot get to work. When I use the model in a controller, it just gives me null. It is the following class I try to serialize
public class ExternalId
{
public string Id { get; set; }
}
public class ExternalId
{
public string Id { get; set; }
}
I want to be able to use the following input
{
"id1": "a",
"id2": "b",
"id3": "3"
}
// to e.g. this class
public class MyDTO {
public ExternalId Id1 {get; set;}
public ExternalId Id2 {get; set;}
public ExternalId Id3 {get; set;}
}
{
"id1": "a",
"id2": "b",
"id3": "3"
}
// to e.g. this class
public class MyDTO {
public ExternalId Id1 {get; set;}
public ExternalId Id2 {get; set;}
public ExternalId Id3 {get; set;}
}
This is my serializer. Is should just take the string, and wrap it in a ExternalId I have tried to put break points in here also, but they are never reached
public class ExternalIdSerializer : JsonConverter<ExternalId>
{
public override ExternalId? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return reader.GetString() is null
? null
: new ExternalId(reader.GetString()!);
}

public override void Write(Utf8JsonWriter writer, ExternalId externalId, JsonSerializerOptions options)
{
writer.WriteStringValue(externalId.Id);
}
}
public class ExternalIdSerializer : JsonConverter<ExternalId>
{
public override ExternalId? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return reader.GetString() is null
? null
: new ExternalId(reader.GetString()!);
}

public override void Write(Utf8JsonWriter writer, ExternalId externalId, JsonSerializerOptions options)
{
writer.WriteStringValue(externalId.Id);
}
}
And last, this is where I add my serializer
services
.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;

var converters = options.JsonSerializerOptions.Converters;
converters.Add(new JsonStringEnumConverter());
converters.Add(new ExternalIdSerializer());
});
services
.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;

var converters = options.JsonSerializerOptions.Converters;
converters.Add(new JsonStringEnumConverter());
converters.Add(new ExternalIdSerializer());
});
7 replies
CC#
Created by Arkobat on 8/14/2022 in #help
Deserialize wrapper object in route
Hello. Does anyone know if it is possible to map deserilize a custom wrapper object in a route? IDs in my system is strings, and we have therefore made an External class. The only point of this, is to improve code readability, to easy be able to view what is ids, and what is not. We have made a serilizer, that maps maps the class to a string in any JSON schemes. However, this serilizer does not seem to work, when we use the class in a route. Can we do it via a serilizer or some middleware, or do I have to take a string as input, and map it to ExternalId in my controller?
[HttpGet("{id}")]
public async Task<MyDTO> GetSomething([FromRoute] ExternalId id)
{
var result = await _service.GetSomething(id);
return result.ToDTO();
}
[HttpGet("{id}")]
public async Task<MyDTO> GetSomething([FromRoute] ExternalId id)
{
var result = await _service.GetSomething(id);
return result.ToDTO();
}
public class ExternalId
{
public string Id { get; set; }

public ExternalId() {}
public ExternalId(string id)
{
Id = id;
}
public class ExternalId
{
public string Id { get; set; }

public ExternalId() {}
public ExternalId(string id)
{
Id = id;
}
11 replies