Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Serializer runs in the new thread?

$
0
0

I have a Get (int Id) method in my controller that returns class instance.

Instance is created correctly(i can see that in the debugger) but on attempt to return

return classInstance;

my code crashes since it runs in the new thread and therefore setting culture to "en-US" .

Previously I have set default culture to "en-CA" which works beautifully on JsonConvert.SerializeObject(user,Formatting.Indented,settings).

i did not open any threads explicitely. I let the server (framework) handle multi-threading.

I use web.api core 2.0. I have a controller method that gets the instance of class. instance is a full graph and i can confirm that is correctly created.

[HttpGet("{id}")]

public UserModel Get(int id)

{

System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-CA");

UserModel user = UserService.GetUserById(id);

return user;

}

code crashes when I try to return instance.

I think at that point while framework tries to serialize object it open the new thread with default culture.


One of underlying classes has localized attributes. Culture is pulled from CurrentThread which in this case is set to be a defalut one "en-US" instead as set in the code several lines above to "en-CA".

since thread is set to "en-US" lazy loaded attribute crashes since it can not get correct item from collection.

public static int GetThreadCultureId()

{

CultureModel cultureModel = null;

string code = System.Threading.Thread.CurrentThread.CurrentCulture.Name;

try {

List<CultureModel> cultures = GetCulturesList();

cultureModel = cultures.Where(culture => culture.Code == code).First();

}

catch (Exception e)

{

throw new CoreExceptions.ObjectNotFoundInDatabaseException(string.Format("The requested culture (Code = \"{0}\") was not found.", code));

}

return cultureModel.Id;

}

i have already tried to post on web.api forum but they asked me to move thread in asp.net core

https://forums.asp.net/t/2138211.aspx


Viewing all articles
Browse latest Browse all 9386

Trending Articles