{
"error": {
"code": "",
"message": "The request is invalid. Details: The parameter 'value' in the request payload is not a valid parameter for the operation 'search'."
}
}
Then the problem is probably the “search” in the URL.
Try “index” instead:
1
POST {{url}}/indexes/user-index-2021-03-25/docs/index?api-version=2023-07-01-preview
If you prefer video instead of text James Montemagno has a similar Youtube video.
Plans
Investigate the code. In MainPage.xaml there is a tight coupling from the xaml file to the code behind through
1
Clicked="OnCounterClicked"
. That method has code like
1
CounterBtn.Text = $"Clicked {count} time";
which means it is tightly coupled with the GUI.
When running the code, the counter and button text, will be updated by the code above.
Let’s get rid of the tight coupling.
Note: I am not satisfied how the click command will be wired up in the final solution. Please comment and help me make it better.
View model
MVVM works by GUI logic writing to an object/data structure (the view model) and the GUI graphic reading from it. And vice verse. It can be compared to a data contract with talking over the wire
This means we can test the logic without having a GUI and the GUI won’t have to rely on complicated GUI logic.
Create a class “MainPageViewModel.cs”, in a new folder “ViewModel”. We want to replace the counter.
To make it easier to react on the changes of the values we will use CommunityToolkit. To make it work the MVVM class must be public and partial. The field must be a field (not a property, and it must be with a leading lower case. Add a counter and a buttonText. Add a method to update the counter and replace the button text.
The reason for the above is that CommunityToolkit creates a Property for every field with ObservableProperty. You can find the rendered code in MauiApp1/Dependencies/.net7.0-xxxx/Analyzers. I have some reason for the view model to be public but won’t delve into it here.
Pressing play should work now as we only have added a class. Not changed or connected anything.
Connect
As per the readme you received when installing CommunityToolkit we have to manipulate the startup. Make it use the CommunityToolkit. Open MauiProgram.cs and add
1
.UseMauiCommunityToolkit()
Then we need to wire up the dependency injection. Alas add
In MainPage.xaml.cs we get rid of the OnCounterClicked method and the counter. We also need to inject the view model to the main page MainPage.xaml.cs. All that is left is:
1 2 3 4 5 6 7 8 9 10
namespace MauiApp1;
public partial class MainPage : ContentPage
{
public MainPage(ViewModel.MainPageViewModel vm)
{
InitializeComponent();
BindingContext = vm;
}
}
Do the xaml dance
As we have removed the event handler we also need to remove it from the xaml. Find the button and get rid of “Clicked=”OnCounterClicked””. We also don’t need the name as the code does not reference the button any more. The result looks like
1 2 3 4 5
<Button
Text="Click me"
SemanticProperties.Hint="Counts the number of times you click"
HorizontalOptions="Center"
/>
If you press play now the program will start but nothing will happen if you press play.
Add dynamic button text and the the cryptic command binding to the button
1 2 3 4 5 6
<Button
Text="{Binding ButtonText}"
SemanticProperties.Hint="Counts the number of times you click"
Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodel:MainPageViewModel}}, Path=IncreaseCountCommand}"
HorizontalOptions="Center"
/>
The above will not work unless we tell the xaml where to find the command. So update the ContentPage element
PRAISE: Nice refactoring SUGGESTION: Isn’t it an Article?, not a Device. CHECK: Have you investigated all uses of the enum?
ola fjelddahl göteborgskontoret
Förvänta dig inte att din Pull request går igenom
PRAISE: Nice refactoring! SUGGESTION: Split line. CHECK: Do you use the enum the correct way? Are all cases considered? EXPLAIN: How do this work?
ola fjelddahl göteborgskontoret
Förvänta dig inte att din Pull request går igenom
PRAISE: Nice refactoring! SUGGESTION: Split line. CHECK: Do you use the enum the correct way? Are all cases considered? EXPLAIN: How do this work? FIX: Check for null.
ola fjelddahl göteborgskontoret
Förvänta dig inte att din Pull request går igenom
PRAISE: Nice refactoring! SUGGESTION: Split line. CHECK: Do you use the enum the correct way? Are all cases considered? EXPLAIN: How do this work? FIX: Check for null. QUESTION: Is this a HasA or a IsA
Föredraget förutsätter en vag förståelse av ORM. Fet orm (EF/Hibernate/cache). Micro-orm. (mappning av data och datatyper) Att det är en översättning från RDb:ns mängdbaserade struktur till det imperativa språkets list-baserade; och tvärtom.
3 skäl Typsäkerhet ändras inte så ofta det så när det väl är mappat Höger-vänster ändras ofta i början men tenderar att stabiliseras Refaktorering man kan bara automatrefaktorera namn och typer. strukturer är manuellt arbete
The idea is this: There are certain requirements on a data contract; like it being serializable and whatnot.
To verify this automagically the test program must be able to find them. It can be done through: 1) they are all in the same namespace. 2) their names are all suffixed with “DataContract” 3) they all implement “IDataContract”
Then through reflection/RTTI a test can get all data contracts and verify they are all ok.
Unfortunately Aspnet does not have a way to verify that an endpoint returns a data contract through unit testing; it has to be solved through integration testing or surveillance of production.
var serviceProvider = new ServiceCollection().AddHttpClient().BuildServiceProvider();
var httpClientFactory = serviceProvider.GetService<IHttpClientFactory>();
I added some health check to my aspnet site to trigger from Kubernetes startupProbe and sometimes got
{
"EventId": 1,
"LogLevel": "Error",
"Category": "Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware",
"Message": "An unhandled exception has occurred while executing the request.",
"Exception": "System.Threading.Tasks.TaskCanceledException: A task was canceled.
at Microsoft.Extensions.Diagnostics.HealthChecks.DefaultHealthCheckService.CheckHealthAsync(Func`2 predicate, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Diagnostics.HealthChecks.HealthCheckMiddleware.InvokeAsync(HttpContext httpContext)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)",
"State": {
"Message": "An unhandled exception has occurred while executing the request.",
"{OriginalFormat}": "An unhandled exception has occurred while executing the request."
}
}
There is nothing in my health checking code that can throw (famous last words…). In the stack above there is none of my code.
To make a long story short I moved the calls to UseHealthCheck from below UseEndpoints to above UseAuthentication as my endpoints are anonymous (they are very lightweight).