Saw the code here. Can any one tell me what it means?
Action wrappedAction = () =>
{
threadToKill = Thread.CurrentThread;
action();
};
Can we write such code with .net v2.0...?
It means that wrapAction is a delegate, that takes in no parameter and that executes the following code block
threadToKill = Thread.CurrentThread;
action();
It's equivalent to
public delegate void wrapActionDel();
public void wrapAction()
{
threadToKill = Thread.CurrentThread;
action();
}
public void CallwrapAction()
{
wrapActionDel del = wrapAction;
del ();
}
You can see that this is verbose, but Action is not.
And, this is only available in .Net 3.5. Don't worry, your .net 2.0 code will work seamlessly with .net 3.5, so you can just upgrade.
That is a lambda expression, and is only available in C# 3.0+. The C# 2.0 version of that code looks like this:
Action wrappedAction = delegate()
{
threadToKill = Thread.CurrentThread;
action();
};
Assuming you have declared the Action delegate previously.
Related
I haven't found anything about HttpClient in .NET MAUI.
Does anyone know if the service:
builder.Services.AddHttpClient<IMyService, MyService>();
is possible in MAUI's startup MauiProgram.cs? And then inject HttpClient to where it's going to be used. I have tried everything and it does not seem to work. Only AddSingleton of HttpClient works for me, but it doesn't seem optimal.
PS.: I had to install nuget package Microsoft.Extensions.Http in order to use the AddHttpClient service.
UPDATES:
WORKING CODE:
MauiProgram.cs
builder.Services.AddTransient<Service<Display>, DisplayService>();
builder.Services.AddTransient<Service<Video>, VideoService>();
builder.Services.AddTransient<Service<Image>, ImageService>();
builder.Services.AddTransient<Service<Log>, LogService>();
builder.Services.AddSingleton(sp => new HttpClient() { BaseAddress = new Uri("https://api.myapi.com") });
Example of VideosViewModel.cs using a service
[INotifyPropertyChanged]
public partial class VideosViewModel
{
readonly Service<Video> videoService;
[ObservableProperty]
ObservableCollection<Video> videos;
[ObservableProperty]
bool isEmpty;
[ObservableProperty]
bool isRefreshing;
public VideosViewModel(Service<Video> videoService)
{
this.videoService = videoService;
}
[ICommand]
internal async Task LoadVideosAsync()
{
#if ANDROID || IOS || tvOS || Tizen
UserDialogs.Instance.ShowLoading("Henter videoer fra databasen...");
#endif
await Task.Delay(2000);
Videos = new();
try
{
await foreach (Video video in videoService.GetAllAsync().OrderBy(x => x.Id))
{
Videos.Add(video);
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
IsRefreshing = false;
#if ANDROID || IOS || tvOS
UserDialogs.Instance.HideLoading();
#endif
if (Videos.Count is 0)
{
IsEmpty = true;
}
else
{
IsEmpty = false;
}
}
}
[ICommand]
async Task UploadVideoAsync()
{
await Shell.Current.DisplayAlert("Upload en video", "Under opbygning - kommer senere!", "OK");
}
}
NOT WORKING CODE:
MauiProgram.cs
builder.Services.AddHttpClient<Service<Display>, DisplayService>(sp => sp.BaseAddress = new Uri("https://api.myapi.com"));
builder.Services.AddHttpClient<Service<Video>, VideoService>(sp => sp.BaseAddress = new Uri("https://api.myapi.com"));
builder.Services.AddHttpClient<Service<Image>, ImageService>(sp => sp.BaseAddress = new Uri("https://api.myapi.com"));
builder.Services.AddHttpClient<Service<Log>, LogService>(sp => sp.BaseAddress = new Uri("https://api.myapi.com"));
VideosViewModel.cs
Same as above working code.
What specifically doesn't work is that I get object reference exception on OrderBy(x => x.Id), specifically highlighted x.Id in ViewModel. Removing OrderBy method gives no longer exceptions, but the view shows no data except one random empty Frame.
Do not use builder.Services.AddHttpClient in MAUI.
Use one instance.
I am trying to call a java method from within my JSNI method. I am not getting any type of error but the Window.alert() never gets called.
package com.mywebsite.myapp.client;
public class MyApp implements EntryPoint(){
/// Other stuff....
public native void getToServer(String trainerName)/*-{
$wnd.$.get( "http://testdastuff.dev/trainerstats", { trainer: trainerName} )
.fail(function() {
$wnd.console.log("error");
})
.done(function( data ) {
if(data == "noData"){
alert("NO DATA");
this.#com.mywebsite.myapp.client.MyApp::testJSNI()();
}
});
}-*/;
public void testJSNI(){
Window.alert("Working");
}
}
It is alerting "NO DATA" so i know something is wrong with the way I am calling the method. It cant be a static method.
Inside of the done callback you will have lost this (it will point to something else now).
You need to preserve it via bind or a local variable (var self = this).
I understand that the MOQ framework wasn't really designed to help in this instance, but perhaps you might be able to help...
I have a method that uses a try/catch that calls a notification method whenever an exception is thrown. What I am trying to do is create an integration/unit test that checks to make sure SendNotification is called when any exception is thrown.
Method Under Test:
public virtual void MonitorIntradayBuilds(IIntradayBuilds intradayBuilds)
{
try
{
var intradayBuildFound = intradayBuilds.CheckForIntradayBuilds();
if (intradayBuildFound && !IntradayBuildsComplete && !DailyBuildsFound)
{
IntradayBuildsComplete = intradayBuilds.StartIntradayBuilds();
//should start daily builds?
}
}
catch (Exception ex)
{
SendNotification("MonitorIntradayBuilds threw an exception", ex);
}
}
Test Case:
[Test]
public void it_should_notify_developers_immediately_if_there_is_a_problem_when_checking_for_intraday_builds()
{
//Arrange
var mockDua = new Mock<DUA>();
var mockIB = new Mock<IIntradayBuilds>();
//Act
mockIB.Setup(x => x.CheckForIntradayBuilds()).Throws(new Exception());
mockDua.Object.MonitorIntradayBuilds(mockIB.Object);
//Assert
mockDua.Verify(x => x.SendNotification(It.IsAny<string>(), It.IsAny<Exception>()), Times.Once);
}
I keep hitting a Moq.MockException and then see that SendNotification "expected an invocation on the mock once, but was 0 times..."
I've tried using the [ExpectedException] attribute on the test case, but to no avail. It makes the test pass, but still doesn't call the SendNotification method.
Any ideas?
Solved it.
Turns out you need to set the CallBase property on the System Under Test that you are mocking up.
Test case is now:
[Test]
public void it_should_notify_developers_immediately_if_there_is_a_problem_when_checking_for_intraday_builds()
{
//Arrange
var mockDua = new Mock<DUA>();
var mockIB = new Mock<IIntradayBuilds>();
mockDua.CallBase = true; // <<<< Added this line!
//Act
mockIB.Setup(x => x.CheckForIntradayBuilds()).Throws(new Exception());
mockDua.Object.MonitorIntradayBuilds(mockIB.Object);
//Assert
mockDua.Verify(x => x.SendNotification(It.IsAny<string>(), It.IsAny<Exception>()), Times.Once);
}
Hopefully someone else finds it helpful :)
The 'standard' CSLA async server calls have typically been structured per the following:
Base class:
public static void GetMyObject(EventHandler<DataPortalResult<MyObject>> callback) {
var dp = new DataPortal<MyObject>();
dp.FetchCompleted += callback;
dp.BeginFetch();
}
ViewModel:
protected override void OnInitialize(object parameter) {
base.OnInitialize(parameter);
base.IsBusy = true;
MyObject.GetMyObject((o, e) => {
if (HasNoException(e)) {
Model = e.Object;
}
base.IsBusy = false;
});
}
With the new async/await features, the format would be something like this:
public async static Task<MyObject> GetMyObject() {
return await DataPortal.FetchAsync<MyObject>();
}
and
protected async override void OnInitialize(object parameter) {
base.OnInitialize(parameter);
base.IsBusy = true;
Model = await MyObject.GetMyObjectAsync();
base.IsBusy = false;
}
Should the callback pattern be considered deprecated at this point, or is it still useful for certain UI technologies? When doing a new project, I'd rather not have those methods in there if I can help it.
Personally, I prefer TAP methods over callbacks in all my code.
With Microsoft.Bcl.Async, most platforms support async. However, there are a few situations where neither TAP nor Task is available, e.g., Windows Phone 7.0, .NET CF, SL 3. I would only use callbacks if I had to support one of those platforms.
I'm considering re-rewriting some of my MVC controllers to be async controllers. I have working unit tests for these controllers, but I'm trying to understand how to maintain them in an async controller environment.
For example, currently I have an action like this:
public ContentResult Transaction()
{
do stuff...
return Content("result");
}
and my unit test basically looks like:
var result = controller.Transaction();
Assert.AreEqual("result", result.Content);
Ok, that's easy enough.
But when your controller changes to look like this:
public void TransactionAsync()
{
do stuff...
AsyncManager.Parameters["result"] = "result";
}
public ContentResult TransactionCompleted(string result)
{
return Content(result);
}
How do you suppose your unit tests should be built? You can of course invoke the async initiator method in your test method, but how do you get at the return value?
I haven't seen anything about this on Google...
Thanks for any ideas.
As with any async code, unit testing needs to be aware of thread signalling. .NET includes a type called AutoResetEvent which can block the test thread until an async operation has been completed:
public class MyAsyncController : Controller
{
public void TransactionAsync()
{
AsyncManager.Parameters["result"] = "result";
}
public ContentResult TransactionCompleted(string result)
{
return Content(result);
}
}
[TestFixture]
public class MyAsyncControllerTests
{
#region Fields
private AutoResetEvent trigger;
private MyAsyncController controller;
#endregion
#region Tests
[Test]
public void TestTransactionAsync()
{
controller = new MyAsyncController();
trigger = new AutoResetEvent(false);
// When the async manager has finished processing an async operation, trigger our AutoResetEvent to proceed.
controller.AsyncManager.Finished += (sender, ev) => trigger.Set();
controller.TransactionAsync();
trigger.WaitOne()
// Continue with asserts
}
#endregion
}
Hope that helps :)
I've written short AsyncController extension method that simplifies unit testing a bit.
static class AsyncControllerExtensions
{
public static void ExecuteAsync(this AsyncController asyncController, Action actionAsync, Action actionCompleted)
{
var trigger = new AutoResetEvent(false);
asyncController.AsyncManager.Finished += (sender, ev) =>
{
actionCompleted();
trigger.Set();
};
actionAsync();
trigger.WaitOne();
}
}
That way we can simply hide threading 'noise':
public class SampleAsyncController : AsyncController
{
public void SquareOfAsync(int number)
{
AsyncManager.OutstandingOperations.Increment();
// here goes asynchronous operation
new Thread(() =>
{
Thread.Sleep(100);
// do some async long operation like ...
// calculate square number
AsyncManager.Parameters["result"] = number * number;
// decrementing OutstandingOperations to value 0
// will execute Finished EventHandler on AsyncManager
AsyncManager.OutstandingOperations.Decrement();
}).Start();
}
public JsonResult SquareOfCompleted(int result)
{
return Json(result);
}
}
[TestFixture]
public class SampleAsyncControllerTests
{
[Test]
public void When_calling_square_of_it_should_return_square_number_of_input()
{
var controller = new SampleAsyncController();
var result = new JsonResult();
const int number = 5;
controller.ExecuteAsync(() => controller.SquareOfAsync(number),
() => result = controller.SquareOfCompleted((int)controller.AsyncManager.Parameters["result"]));
Assert.AreEqual((int)(result.Data), number * number);
}
}
If you want to know more I've written a blog post about how to Unit test ASP.NET MVC 3 asynchronous controllers using Machine.Specifications
Or if you want to check this code it's on a github