C
C#2mo ago
Qwerz

Blazor Forms and Different Sessions

Hi all, I'm working on a simple webpage (functioning as a homepage) with blazor (server). My current goal is to create a contact form at the bottom of the page, where customers can send a message to staff. I created an EditForm using a model and used MailKit in the code fragment to send the message submitted by the EditForm via our mail server. This worked as expected until... A co'worker told me this approach was not feasible for the following reason: While one client visiting the website and submitting the form works, it does not work as intended when there are multiple clients accessing the site and editing the form:
c#
<EditForm Model="@Email" OnSubmit="sendEmail">
<button type="submit">Send Email</button>
</EditForm>

@code {
private EmailModel Email { get; set; } = new EmailModel();
void sendEmail() {
//using MailKit here
}
}
c#
<EditForm Model="@Email" OnSubmit="sendEmail">
<button type="submit">Send Email</button>
</EditForm>

@code {
private EmailModel Email { get; set; } = new EmailModel();
void sendEmail() {
//using MailKit here
}
}
E.g. if two clients connect to the website and fill out the form and then submit it simultaneously, the data submited is messed up and mixed between the two accessing sessions. How can this be? I though Blazor would handle these requests asynchronously. I was told the issue lies in the Datacontext being the same for every session and thus all clients editing the same instance of a static object (the EditForm). What must I change to get the expected behaviour? Thanks in advance!
9 Replies
Salman
Salman2mo ago
I think Blazor should just work fine as that's the normal way to do it in Blazor . You can verify it by opening the site in multiple tabs/browsers and then filling the form
Qwerz
Qwerz2mo ago
Do I not need sessions for this? Isn't the model used in the form a static object and thus always the same for any client?
Salman
Salman2mo ago
when the user will visit this page, each user will have his own Model Test : Open the site in the normal browser and fill the form Open the site in incognito mode and fill the form Enter different data in both of them and you'll see the email sent will be distinct
Qwerz
Qwerz2mo ago
I will try again, thanks!
exixt
exixt2mo ago
your coworker is wrong what you posted works as intended
Jimmacle
Jimmacle2mo ago
you should ask your coworker to demonstrate that issue and watch them struggle the model used in the form is not static, you haven't declared it as static therefore each instance of the component gets its own instance of the model
Qwerz
Qwerz2mo ago
You all seem to be right. I do wonder however, why my coworker experienced the issues he described. Generally speaking, would you approve of my methodology for creating a public contact form on a homepage?
Jimmacle
Jimmacle2mo ago
yeah there's nothing wrong with the code you've shown, though you might want to consider separating the concern of how to actually send the email to an injected service
Qwerz
Qwerz2mo ago
This is my first Blazor project, so I'll have to look into services and concern separation as a whole, I assume they work similar to WPF. Right now, my main focus is to get things working in a stable manner. Thank you for your help so far!
Want results from more Discord servers?
Add your server