Author Topic: Get TaskName (Server Task name) inside Server Task when executing  (Read 129 times)

Offline Misiu

  • Sr. Member
  • ****
  • Posts: 81
  • Karma: +0/-1
  • Gender: Male
    • View Profile
When creating Server Task in Toolbox we must give is a name, then we can use that name to call that Server Task from Client code:
Code: [Select]
SystemClient.ExecuteServerTask(taskName, methodName, types, parameters)
I'm wondering if there is an option to get than Server Task name inside Server Task method (when it is executed).
For example:
1. I've created new Server Task in Toolkit. It is names "Sample" and has full class name: "AA.Server.Prototype.VersionOne".
2. I'm able to call method "Test" from my new Server taks using this code:
Code: [Select]
SystemClient.ExecuteServerTask("Sample", "Test", types, parameters)
Now I need to get string "Sample" (because that's the name of my Server Task) when executing method "Test"

I can get qualified class name and method name, but I need Server Task name.
I hope this is clear enough.

I need this to create notification system that will notify end user about server task progress, after finishing I can share some details about it.

Offline MartinR

  • Newbie
  • *
  • Posts: 4
  • Karma: +0/-0
    • View Profile
    • TA CRM Solutions Limited
Re: Get TaskName (Server Task name) inside Server Task when executing
« Reply #1 on: June 20, 2018, 06:33:36 AM »
Why not just pass it as one of the parameters?

Offline Misiu

  • Sr. Member
  • ****
  • Posts: 81
  • Karma: +0/-1
  • Gender: Male
    • View Profile
Re: Get TaskName (Server Task name) inside Server Task when executing
« Reply #2 on: June 20, 2018, 06:40:06 AM »
That's an option, but this way I must modify all Server Tasks (unfortunately Pivotal cant handle default parameter values  :-\)

For now I'm updating my two test Server Tasks with that additional parameters, hopefully it will work fine.
Ideally I'd like to get that name in Server Task code.

Offline Misiu

  • Sr. Member
  • ****
  • Posts: 81
  • Karma: +0/-1
  • Gender: Male
    • View Profile
Re: Get TaskName (Server Task name) inside Server Task when executing
« Reply #3 on: June 20, 2018, 08:04:37 AM »
@MartinR I've did like You suggested.

The downside is that I must define two methods in Server Task. For example:
Code: [Select]
[TaskExecute]
public ServerTaskResult GenerateDocuments(string package)
{
    return GenerateDocuments(wywiadownia, null);
}

[TaskExecute]
public ServerTaskResult GenerateDocuments(string package, string guid)
{
    //here real implementation
}

Inside Server Task I'm calling this method:
Code: [Select]
ClientNotifier.Publish(guid, $"Generowanie {current}/{total}...");

ClientNotifier is my custom class that push messages via sockets to all subscribed clients.

From client side I'm calling my Server Task using my extension method:

Code: [Select]
SystemClient.ExecuteServerTaskAsync("Generator", "GenerateDocuments", new object[] { "Package 1" }, "Trwa generowanie paczki...", () =>
{
    var searchView = ClientContext.ClientAPIService.GetActiveContentObject() as ISearchView;
    searchView.Refresh();
}, true);

This is how this looks on client (of course spinner is spinning :)):


This method allows me to show some kind of progress, because by default client "freezes" when You call Server Task and wait for result.