-1

I'm writing a plugin for in-app purchases for my unity game which needs to use the .Net 3.5 framework.

So going from the docs ref: unity WP8 plugin-docs

It states: "...implement identical non-private methods/fields/properties as in the real DLL"

So I'm trying to apply that to the following method, which needs to use the Task class as it awaits a method.

So here's the method that actually does the work.

public async Task<bool> PurchaseUpgrade(string ID)
{
    var listings = await CurrentApp.LoadListingInformationAsync();

     //The rest of the method-body is irrelevant... 
       So this won't compile as-is.
}

So yeah I need to write a method in the dll the editor 'uses' just with a matching signature, alas I can't because of the Task class.

public async Task<bool> PurchaseUpgrade(string ID)
{
    //This could literally be empty if the method was void, but need it to return
      Task<bool>, and can't work out how

    return true;
}

Anyone able to give any insight into how I can accomplish this?

Thanks in advance

Lee Brindley
  • 6,242
  • 5
  • 41
  • 62

2 Answers2

0

You should be able to use the Task.FromResult<T>() method.

return Task.FromResult(true);

Or if you can't use .NET 4.5, try this:

return Task.Factory.Start(() => true);
Martin Costello
  • 9,672
  • 5
  • 60
  • 72
  • Ah sorry, forgot to add the most important info to the question. I need to target the .Net 3.5 framework, so no access to Task. Upvoted for your time :P And edited. – Lee Brindley Apr 09 '14 at 22:45
  • @Lee2808: Wait, so your question title is about .NET 4.0, within your question text you reference Windows Phone 8, and your comment here says you need to target .NET 3.5? Those are three different platforms; please edit your question for consistency. – Stephen Cleary Apr 09 '14 at 23:32
  • @StephenCleary Using Task class in .Net 4.0, .Net 4.0 being the 'location' of the Task class. The question was also edited before you commented, did you actually read the question? – Lee Brindley Apr 10 '14 at 02:19
  • Also .Net is a framework, Windows Phone 8 is a platform. Two totally different things. So to me your comment and down-vote was based upon some seriously flawed reasoning. – Lee Brindley Apr 10 '14 at 02:26
  • @Lee2808: As of now, your question is still asking how you can write a `Task`-returning method on .NET 4, and your comment still says you are targeting .NET 3.5. FYI, *neither* .NET 3.5 nor .NET 4 are supported on WP8; the platform WP8 has its own framework (generally also called WP8). [A .NET 4 dll cannot load on WP8](http://social.msdn.microsoft.com/Forums/en-US/5228cbc6-66e3-4b1f-9981-702d68eb7263/can-i-refer-regular-net-dll-to-window-8-metro-app-or-windows-phone-8?forum=winappswithcsharp). – Stephen Cleary Apr 10 '14 at 12:12
  • I'm writing a dll that uses .net 3.5 that needs to use the Task class in .Net 4.0 is what the question is intended to say. – Lee Brindley Apr 10 '14 at 13:07
-1

I found the answer to this is encapsulation. The async methods need to be private and have public methods call them.

In the dll that targets .Net 4.0 or greater you have your async method like so:

 private async void Foo()
 {
     await LongRunningMethod();
 }

 public void Bar()
 {
    //Call the async method
    Foo();
 }

Now to use the dll above in Unity, you have to create a dll that targets .Net 3.5 as stated above, and have it include any method, along with matching signature that you wish to use from within a unity script.

So in the .Net 3.5 dll you just need to:

public void Bar()
{
    //This can literally be empty if you want
}

And that's it...almost

The assembly names and default namespace properties of both dll's have to match.

The .Net 3.5 dll needs to be placed in the folder

 /Assets/Plugins

And the .Net 4.0 dll needs to be placedin the folder

 /Assets/Plugins/WP8

This way, you won't get any compilation errors when working in the unity editor, the method calls from the .Net 3.5 dll will be called. When you're running on a WP8 device however the .Net 4.0 with the real 'magic' inside will be called.

Hope that helps anyone out there.

Note: As stated in the comments below it's worth mentioning that you won't be able to catch an exception thrown by a Async void method.

You might think this catches exceptions thrown by the async void method:

 public void CallAsyncMethod()
 {

        try
        {
              Foo(); //The async void mentioned above
        }
        catch(Exception)
        {
              //But it doesn't
        }

 }

To be able to write 'safe' code using an async void method you need to add the exception handling inside the async void method, like so:

 private async void Foo()
 {

        try
        {
              await LongRunningMethod();
        }
        catch(Exception)
        {
              //This is now safe, the exception will be caught, although admittedly  
              // ugly code, better to have it in the called IMO.
        }
 }
Lee Brindley
  • 6,242
  • 5
  • 41
  • 62
  • 1
    The use of `async void` causes serious problems around error handling, which are not addressed in this answer. – Stephen Cleary Apr 10 '14 at 12:08
  • I found the async-void thing to be irrelevant to the answer. In my implementation. In my code I have error handling inside the Async void methods, which is perfectly acceptable. Although not the nicest solution, it gets around the 'serious problems' you're referring to. – Lee Brindley Apr 10 '14 at 13:06
  • 1
    Yes, that error handling would work. I suggest you incorporate that into your answer, so that others searching for a solution will know to do that, too. – Stephen Cleary Apr 10 '14 at 13:11