Noriega
Noriega
CC#
Created by Noriega on 12/24/2023 in #help
Exception about disposing DbContext
This exception I've been getting is so confusing since every other source I tried looking up for solutions doesn't exactly apply to my code. So, the exception I'm getting is that my db context gets disposed before I can use another operation on the db. Everywhere Ive checked for help had people screwing up asynchronous operations, which isnt the case for me. Here is my file for reference
- System.ObjectDisposedException: Cannot access a disposed context instance. A common cause of this error is disposing a context instance that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling 'Dispose' on the context instance, or wrapping it in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances. Object name: 'ScathachDbContext'.
- System.ObjectDisposedException: Cannot access a disposed context instance. A common cause of this error is disposing a context instance that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling 'Dispose' on the context instance, or wrapping it in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances. Object name: 'ScathachDbContext'.
Am using pooled db context btw
10 replies
CC#
Created by Noriega on 12/18/2023 in #help
Exception when attempting to use keyed dependencies
I tried making use of the new keyed DI feature that comes with net 8, but when I try injecting 2 dependencies with IServiceCollection.AddKeyedSingleton<T1, T2>(""), Im receiving an exception that I havent found any fix for. this kind of setup for dependencies doesn't seem to require any special setup according to the example on the microsoft page, so im confused Code:
serviceCollection.AddKeyedSingleton<IScraper, BooruScraper>("booru");
serviceCollection.AddKeyedSingleton<IScraper, HIScraper>("hi");
serviceCollection.AddKeyedSingleton<IScraper, BooruScraper>("booru");
serviceCollection.AddKeyedSingleton<IScraper, HIScraper>("hi");
Exception:
- System.InvalidOperationException: This service descriptor is keyed. Your service provider may not support keyed services.
- System.InvalidOperationException: This service descriptor is keyed. Your service provider may not support keyed services.
15 replies
CC#
Created by Noriega on 10/24/2023 in #help
✅ Newtonsoft serializing returns nothing with custom converter
So I have a custom JsonConverter from newtonsoft that is supposed to replace object properties that are a specific type. For context, I'm looking for a type called IOptional that has properties HasValue and Value only if HasValue is true. If HasValue is true, the converter is supposed to replace the json property with Value. Otherwise, it replaces to null. I have this
file class EvalResultConverter : JsonConverter
{
public override bool CanRead => false;
public override bool CanWrite => true;

public override bool CanConvert(Type objectType) => true;

public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
=> throw new NotImplementedException();

public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
var token = JToken.FromObject(value!);

if (token.Type is JTokenType.Object)
{
var tokenObject = (JObject)token;
foreach (var property in tokenObject.Properties())
{
if (property.Value.Type is JTokenType.Object && typeof(IOptional).IsAssignableFrom(value!.GetType().GetProperty(property.Name)!.PropertyType))
{
var propertyObject = (JObject)property.Value;
tokenObject[property.Name] = propertyObject["HasValue"]!.Value<bool>() ? propertyObject["Value"] : null;
}
}

tokenObject.WriteTo(writer);
}
else token.WriteTo(writer);
}
}
file class EvalResultConverter : JsonConverter
{
public override bool CanRead => false;
public override bool CanWrite => true;

public override bool CanConvert(Type objectType) => true;

public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
=> throw new NotImplementedException();

public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
var token = JToken.FromObject(value!);

if (token.Type is JTokenType.Object)
{
var tokenObject = (JObject)token;
foreach (var property in tokenObject.Properties())
{
if (property.Value.Type is JTokenType.Object && typeof(IOptional).IsAssignableFrom(value!.GetType().GetProperty(property.Name)!.PropertyType))
{
var propertyObject = (JObject)property.Value;
tokenObject[property.Name] = propertyObject["HasValue"]!.Value<bool>() ? propertyObject["Value"] : null;
}
}

tokenObject.WriteTo(writer);
}
else token.WriteTo(writer);
}
}
So just to be clear, any other token types works fine along with any objects that don't have IOptional properties. But when I try this with types that do, serialization returns nothing. Not sure if im incorrectly writing to the jsonwriter, but its been annoying for a few days
8 replies