CrosRoad95
CrosRoad95
CC#
Created by CrosRoad95 on 4/27/2024 in #help
Blazor, auto unsubscribe pattern
I have created website using blazor, and if some component subscribe to something then it implement IDisposable interface to cleanup subscriptions. Because i have also a little experience with angular, rx.js and simillar, i wonder if it is good idea to introduce simillar pattern to backend:
@inherits ComponentWithSubscriptions
@inject MyService _myService

@code{
protected ovveride void OnInitializeAsync(){
Subscribe(_myService.IncomingMessage, (message) => { /* here i render message */ InvokeAsync(StateHasChanged) }
}
}
@inherits ComponentWithSubscriptions
@inject MyService _myService

@code{
protected ovveride void OnInitializeAsync(){
Subscribe(_myService.IncomingMessage, (message) => { /* here i render message */ InvokeAsync(StateHasChanged) }
}
}
and base class will automatically cleanup all subscrptions, what do you think? do you have any other examples like this? I ask because i'm warry that in more complex scenario where for example events are subscribed conditionally i may leave some not cleanuped event handlers
1 replies
CC#
Created by CrosRoad95 on 2/11/2024 in #help
Thread safe moving value between two objects
I have problem. Consider following class:
public class Foo {
public int Amount { get; set; }
public int MaxAmount { get; set; }
}

var foo1 = new Foo{ Amount = 75, MaxAmount = 100 };
var foo2 = new Foo{ Amount = 75, MaxAmount = 100 };
public class Foo {
public int Amount { get; set; }
public int MaxAmount { get; set; }
}

var foo1 = new Foo{ Amount = 75, MaxAmount = 100 };
var foo2 = new Foo{ Amount = 75, MaxAmount = 100 };
what i want to do is to thread safe move amount from one "Foo" object, to another that it won't overflow above "MaxAmount".
foo1.Transfer(foo2, 25); // Ok, foo2 now is full, foo1 have amount 50, foo2 amount 100
foo1.Transfer(foo2, 25); // Throw overflow exception, because 100 + 25 > 125
foo1.Transfer(foo2, 100); // Throw other exception, because now foo1 have Amount = 50
foo1.Transfer(foo2, 25); // Ok, foo2 now is full, foo1 have amount 50, foo2 amount 100
foo1.Transfer(foo2, 25); // Throw overflow exception, because 100 + 25 > 125
foo1.Transfer(foo2, 100); // Throw other exception, because now foo1 have Amount = 50
A bif more context: It is for game for items stacking purpose, it is used server side so two clients at once may attempt to move items. Please focus on thread safety aspect
23 replies
CC#
Created by CrosRoad95 on 2/3/2024 in #help
Blazor rerender
Can i somehow make blazor rerender when property change automatically whereever property change? I don't want to add manually "StateHasChanged"
public class Foo
{
private int _a;
public int A
{
get => _a; set
{
_a = value;
AChanged.InvokeAsync(value);
}
}
public EventCallback<int> AChanged { get; set; }
}
public class Foo
{
private int _a;
public int A
{
get => _a; set
{
_a = value;
AChanged.InvokeAsync(value);
}
}
public EventCallback<int> AChanged { get; set; }
}
then i want to render it:
Foo = @foo.A
Foo = @foo.A
and make it automatically rerender when property "A" changed
var _ = Task.Run(async () =>
{
while (true)
{
await Task.Delay(500);
foo.A++;
}
});
var _ = Task.Run(async () =>
{
while (true)
{
await Task.Delay(500);
foo.A++;
}
});
Or maybe i need to wrap it somehow into a component? but i'm not sure if it's good idea? Foo = <Property>@foo.A</Property>
2 replies
CC#
Created by CrosRoad95 on 1/27/2024 in #help
Blazor two way query parameter binding
[Parameter]
[SupplyParameterFromQuery]
public string Category { get; set; }
[Parameter]
[SupplyParameterFromQuery]
public string Category { get; set; }
How can i bind it two way? for example i open page with category=foo, then navigate on the site to category foo and it change query parameter value to bar?
4 replies
CC#
Created by CrosRoad95 on 1/9/2024 in #help
Blazor "auto-bindable" property source generator
anyone know lib that can auto generate required event and property? For example:
[BindableParameter]
public bool Visible { get; set; }
[BindableParameter]
public bool Visible { get; set; }
Auto generate into:
private bool _visible;

[Parameter]
public bool Visible
{
get => _visible;
set
{
if (_visible == value) return;

_visible = value;
VisibleChanged.InvokeAsync(value);
}
}
[Parameter]
public EventCallback<bool> VisibleChanged { get; set; }
private bool _visible;

[Parameter]
public bool Visible
{
get => _visible;
set
{
if (_visible == value) return;

_visible = value;
VisibleChanged.InvokeAsync(value);
}
}
[Parameter]
public EventCallback<bool> VisibleChanged { get; set; }
???
15 replies
CC#
Created by CrosRoad95 on 12/30/2023 in #help
MudBlazor on net8, MudSelect not working
first of all: i googled this issue 9999 times and couldn't find solution. https://github.com/MudBlazor/MudBlazor/issues/7510 - tried to set interactiveServer for one component, for entire application, nothing worked, when i click select button it doesn't show me options. When i set ref to select and attempt to use method "OpenMenu", it hang itd what i'm doing wrong. Using .net8 and newest mudblazor lib
7 replies
CC#
Created by CrosRoad95 on 12/18/2023 in #help
source generator razor components
No description
5 replies
CC#
Created by CrosRoad95 on 12/12/2023 in #help
how to use SignInManager.SignInAsync in blazor web app?
No description
4 replies
CC#
Created by CrosRoad95 on 11/27/2023 in #help
ActivatorUtilities.CreateInstance replacment for native AOT
as in title, how can i replace ActivatorUtilities.CreateInstance function? right now it complain that:
Error in startup A suitable constructor for type '<type>' could not be located. Ensure the type is concrete and all parameters of a public constructor are either registered as services or passed as arguments. Also ensure no extraneous arguments are provided.
Error in startup A suitable constructor for type '<type>' could not be located. Ensure the type is concrete and all parameters of a public constructor are either registered as services or passed as arguments. Also ensure no extraneous arguments are provided.
8 replies
CC#
Created by CrosRoad95 on 11/1/2023 in #help
❔ Question about getter property of list
so, i have some class containg property of type: List<string> , how can i thread-safe return this list as some kind of snapshot of current state of list? right now i'm doing it in followoing way but i'm not sure if it okey
private readonly object _lock = new();
private readonly List<string> _strings = [];
public IReadOnlyList<string> Strings {
get {
lock(_lock)
return [.._strings];
}
}
private readonly object _lock = new();
private readonly List<string> _strings = [];
public IReadOnlyList<string> Strings {
get {
lock(_lock)
return [.._strings];
}
}
9 replies
CC#
Created by CrosRoad95 on 10/29/2023 in #help
❔ Service provider, IDisposable
if i do:
Services.AddScoped/Transient<MyDisposableClass>();
class MyDisposableClass : IDisposable { ... }
Services.AddScoped/Transient<MyDisposableClass>();
class MyDisposableClass : IDisposable { ... }
will dispose method called when service goes out of scope?
49 replies
CC#
Created by CrosRoad95 on 10/28/2023 in #help
❔ What happening in background when i return Task.CompletedTask?
I just wonder, what c# doing if i have async method returning Task.completedTask? does it try to synchronously continue without actualy creating a task?
public Task Foo() => Task.CompletedTask;
public Task Foo() => Task.CompletedTask;
11 replies
CC#
Created by CrosRoad95 on 10/24/2023 in #help
❔ IServiceProvider, scoped services, scopes
is it correct usage?
projectA:
services.AddScoped<FooContext>();
var serviceProvider = services.BuildServiceProvider()

using(var scope = serviceProvider.CreateScope())
{
var fooContext = serviceProvider.GetRequiredService<FoContext>();
fooContext.SetFoo("foobar");
// let's say here i call mediator, other code that may use foocontext
}

projectB:
public class SomeClass // Part of same service provider, it maybe a mediator request handler
{
public Someclass(FooContext fooContext)
{
var foo = fooContext.GetFoo() // "foobar"
}
}
projectA:
services.AddScoped<FooContext>();
var serviceProvider = services.BuildServiceProvider()

using(var scope = serviceProvider.CreateScope())
{
var fooContext = serviceProvider.GetRequiredService<FoContext>();
fooContext.SetFoo("foobar");
// let's say here i call mediator, other code that may use foocontext
}

projectB:
public class SomeClass // Part of same service provider, it maybe a mediator request handler
{
public Someclass(FooContext fooContext)
{
var foo = fooContext.GetFoo() // "foobar"
}
}
8 replies
CC#
Created by CrosRoad95 on 10/22/2023 in #help
❔ Garbage collection question
I see in many libraries examples where in DI ".AddScoped<>", ".AddTransient<>" is used instead of ".AddSingleton<>", example in fluent assertions example says: services.AddScoped<IValidator<User>, UserValidator>(); and i wonder why? this class instance is sorto "read only" so it could be singleton? And here's my question regarding that, because singleton will create long lived instanced meanwhile the other two will live for let's say 100ms, they will be regularly cleared, less often reach further generation. I know that not all singletons are used at all time, they will live in memory for long time and cause extra pressure on garbage colletor. What do you think? is it better to use Transient/Scoped even when singleton make sense from memory point of view?
7 replies
CC#
Created by CrosRoad95 on 10/5/2023 in #help
❔ Polly - question
I have some method, but it can be quickly called multiple times and what i'm trying to do is that after certain amount of time it get called, for example Foo(); Foo(); Foo(); - called three times, but first two calls will be ignored because they happened too quickly and after let's say 200ms last foo get executed can i achieve something like this using polly? https://github.com/App-vNext/Polly it's probably called debouce, can polly do this?
5 replies
CC#
Created by CrosRoad95 on 10/1/2023 in #help
❔ docker image to store files
It's may be not really c# question, but do you know any docker image i could use to upload and store files, then download it? i want to add upload option to my website and then i want to list all uploaded files
6 replies
CC#
Created by CrosRoad95 on 9/28/2023 in #help
❔ Docker image to provide configuration
I'm hosting my c# application on vps, not in azure, aws or other. So i want to have some web application, container which i can use to configure my server in real time, any suggestions?
63 replies
CC#
Created by CrosRoad95 on 9/28/2023 in #help
❔ How to listen for changes made in visual studio?
I have .lua scripts in my project, but regular file watcher can't listen for changes in project directory and visual studio doesn't directly modify file. How can i listen for changes in non .html, .css or other already supported files?
4 replies
CC#
Created by CrosRoad95 on 9/24/2023 in #help
❔ where can i find documentation for Microsoft.VisualStudio.Azure.Containers.Tools.Targets?
as in title, trying to use it ( version 1.19.4 ) but i'm not sure where documentation is. Found https://learn.microsoft.com/en-us/dotnet/core/docker/introduction but i'm not sure if it is it because i followed it, changed tag in .csproj <ContainerImageTag>0.0.1;latest</ContainerImageTag> and still same tag ( only latest ) was used as in Publish profile was defined
3 replies
CC#
Created by CrosRoad95 on 9/18/2023 in #help
❔ Spread like operator in blazor
i have some .razor component accepting class, but i would like to pass individual parameters to it, let's say:
public class Foo{
public string Test1 {get;set;}
public string Test2 {get;set;}
}

// and my .razor component:
Foo fooClass = new();
<MyComponent ...fooClass />

MyComponent:
@code
{
[Parameter]
public string Test1 {get;set;}
[Parameter]
public string Test1 {get;set;}
}
public class Foo{
public string Test1 {get;set;}
public string Test2 {get;set;}
}

// and my .razor component:
Foo fooClass = new();
<MyComponent ...fooClass />

MyComponent:
@code
{
[Parameter]
public string Test1 {get;set;}
[Parameter]
public string Test1 {get;set;}
}
is it possible? to use ... spread operator?
7 replies