![]() |
VOOZH | about |
dotnet add package Azure.Communication.JobRouter --version 1.0.0
NuGet\Install-Package Azure.Communication.JobRouter -Version 1.0.0
<PackageReference Include="Azure.Communication.JobRouter" Version="1.0.0" />
<PackageVersion Include="Azure.Communication.JobRouter" Version="1.0.0" />Directory.Packages.props
<PackageReference Include="Azure.Communication.JobRouter" />Project file
paket add Azure.Communication.JobRouter --version 1.0.0
#r "nuget: Azure.Communication.JobRouter, 1.0.0"
#:package Azure.Communication.JobRouter@1.0.0
#addin nuget:?package=Azure.Communication.JobRouter&version=1.0.0Install as a Cake Addin
#tool nuget:?package=Azure.Communication.JobRouter&version=1.0.0Install as a Cake Tool
This package contains a C# SDK for Azure Communication Services for JobRouter.
Source code | Package (NuGet) | Product documentation
Install the Azure Communication JobRouter client library for .NET with NuGet:
dotnet add package Azure.Communication.JobRouter
You need an Azure subscription and a Communication Service Resource to use this package.
To create a new Communication Service, you can use the Azure Portal, the Azure PowerShell, or the .NET management client library.
using Azure.Communication.JobRouter;
This will allow you to interact with the JobRouter Service
JobRouterClient routerClient = new JobRouterClient("<< CONNECTION STRING >>");
JobRouterAdministrationClient routerAdministrationClient = new JobRouterAdministrationClient("<< CONNECTION STRING >>");
A Job represents the unit of work, which needs to be routed to an available Worker. A real-world example of this may be an incoming call or chat in the context of a call center.
A Worker represents the supply available to handle a Job. Each worker registers with with or more queues to receive jobs. A real-world example of this may be an agent working in a call center.
A Queue represents an ordered list of jobs waiting to be served by a worker. Workers will register with a queue to receive work from it. A real-world example of this may be a call queue in a call center.
A Channel represents a grouping of jobs by some type. When a worker registers to receive work, they must also specify for which channels they can handle work, and how much of each can they handle concurrently.
A real-world example of this may be voice calls or chats in a call center.
An Offer is extended by JobRouter to a worker to handle a particular job when it determines a match, this notification is normally delivered via EventGrid. The worker can either accept or decline the offer using th JobRouter API, or it will expire according to the time to live configured on the distribution policy. A real-world example of this may be the ringing of an agent in a call center.
A Distribution Policy represents a configuration set that governs how jobs in a queue are distributed to workers registered with that queue. This configuration includes how long an Offer is valid before it expires and the distribution mode, which define the order in which workers are picked when there are multiple available.
The 3 types of modes are
Id and the next worker after the previous one that got an offer is picked.You can attach labels to workers, jobs and queues. These are key value pairs that can be of string, number or boolean data types.
A real-world example of this may be the skill level of a particular worker or the team or geographic location.
Label selectors can be attached to a job in order to target a subset of workers serving the queue. A real-world example of this may be a condition on an incoming call that the agent must have a minimum level of knowledge of a particular product.
A classification policy can be used to dynamically select a queue, determine job priority and attach worker label selectors to a job by leveraging a rules engine.
An exception policy controls the behavior of a Job based on a trigger and executes a desired action. The exception policy is attached to a Queue so it can control the behavior of Jobs in the Queue.
Before we can create a Queue, we need a Distribution Policy.
Response<DistributionPolicy> distributionPolicy = await routerAdministrationClient.CreateDistributionPolicyAsync(
new CreateDistributionPolicyOptions(
distributionPolicyId: "distribution-policy-1",
offerExpiresAfter: TimeSpan.FromDays(1),
mode: new LongestIdleMode())
);
Next, we can create the queue.
Response<RouterQueue> queue = await routerAdministrationClient.CreateQueueAsync(
new CreateQueueOptions(
queueId: "queue-1",
distributionPolicyId: distributionPolicy.Value.Id)
);
Now, we can submit a job directly to that queue, with a worker selector the requires the worker to have the label Some-Skill greater than 10.
Response<RouterJob> job = await routerClient.CreateJobAsync(
new CreateJobOptions(
jobId: "jobId-1",
channelId: "my-channel",
queueId: queue.Value.Id)
{
ChannelReference = "12345",
Priority = 1,
RequestedWorkerSelectors =
{
new RouterWorkerSelector("Some-Skill", LabelOperator.GreaterThan, new RouterValue(10))
}
});
Now, we register a worker to receive work from that queue, with a label of Some-Skill equal to 11.
Response<RouterWorker> worker = await routerClient.CreateWorkerAsync(
new CreateWorkerOptions(workerId: "worker-1", capacity: 1)
{
Queues = { queue.Value.Id },
Labels = { ["Some-Skill"] = new RouterValue(11) },
Channels = { new RouterChannel("my-channel", 1) },
AvailableForOffers = true,
}
);
We should get a RouterWorkerOfferIssued from our EventGrid subscription.
There are several different Azure services that act as a event handler. For this scenario, we are going to assume Webhooks for event delivery. Learn more about Webhook event delivery
Once events are delivered to the event handler, we can deserialize the JSON payload into a list of events.
// Parse the JSON payload into a list of events
EventGridEvent[] egEvents = EventGridEvent.ParseMany(BinaryData.FromStream(httpContent));
string offerId;
foreach (EventGridEvent egEvent in egEvents)
{
// This is a temporary fix before Router events are on-boarded as system events
switch (egEvent.EventType)
{
case "Microsoft.Communication.WorkerOfferIssued":
AcsRouterWorkerOfferIssuedEventData deserializedEventData =
egEvent.Data.ToObjectFromJson<AcsRouterWorkerOfferIssuedEventData>();
Console.Write(deserializedEventData.OfferId); // Offer Id
offerId = deserializedEventData.OfferId;
break;
// Handle any other custom event type
default:
Console.Write(egEvent.EventType);
Console.WriteLine(egEvent.Data.ToString());
break;
}
}
However, we could also wait a few seconds and then query the worker directly against the JobRouter API to see if an offer was issued to it.
Response<RouterWorker> result = await routerClient.GetWorkerAsync(worker.Value.Id);
foreach (RouterJobOffer? offer in result.Value.Offers)
{
Console.WriteLine($"Worker {worker.Value.Id} has an active offer for job {offer.JobId}");
}
Once a worker receives an offer, it can take two possible actions: accept or decline. We are going to accept the offer.
// fetching the offer id
RouterJobOffer jobOffer = result.Value.Offers.First<RouterJobOffer>(x => x.JobId == job.Value.Id);
string offerId = jobOffer.OfferId; // `OfferId` can be retrieved directly from consuming event from Event grid
// accepting the offer sent to `worker-1`
Response<AcceptJobOfferResult> acceptJobOfferResult = await routerClient.AcceptJobOfferAsync(worker.Value.Id, offerId);
Console.WriteLine($"Offer: {jobOffer.OfferId} sent to worker: {worker.Value.Id} has been accepted");
Console.WriteLine($"Job has been assigned to worker: {worker.Value.Id} with assignment: {acceptJobOfferResult.Value.AssignmentId}");
// verify job assignment is populated when querying job
Response<RouterJob> updatedJob = await routerClient.GetJobAsync(job.Value.Id);
Console.WriteLine($"Job assignment has been successful: {updatedJob.Value.Status == RouterJobStatus.Assigned && updatedJob.Value.Assignments.ContainsKey(acceptJobOfferResult.Value.AssignmentId)}");
Once the worker is done with the job, the worker has to mark the job as completed.
Response completeJob = await routerClient.CompleteJobAsync(new CompleteJobOptions(job.Value.Id, acceptJobOfferResult.Value.AssignmentId)
{
Note = $"Job has been completed by {worker.Value.Id} at {DateTimeOffset.UtcNow}"
});
Console.WriteLine($"Job has been successfully completed: {completeJob.Status == 200}");
After a job has been completed, the worker can perform wrap up actions to the job before closing the job and finally releasing its capacity to accept more incoming jobs
Response closeJob = await routerClient.CloseJobAsync(new CloseJobOptions(job.Value.Id, acceptJobOfferResult.Value.AssignmentId)
{
Note = $"Job has been closed by {worker.Value.Id} at {DateTimeOffset.UtcNow}"
});
Console.WriteLine($"Job has been successfully closed: {closeJob.Status == 200}");
updatedJob = await routerClient.GetJobAsync(job.Value.Id);
Console.WriteLine($"Updated job status: {updatedJob.Value.Status == RouterJobStatus.Closed}");
// Optionally, a job can also be set up to be marked as closed in the future.
var closeJobInFuture = await routerClient.CloseJobAsync(new CloseJobOptions(job.Value.Id, acceptJobOfferResult.Value.AssignmentId)
{
CloseAt = DateTimeOffset.UtcNow.AddSeconds(2), // this will mark the job as closed after 2 seconds
Note = $"Job has been marked to close in the future by {worker.Value.Id} at {DateTimeOffset.UtcNow}"
});
Console.WriteLine($"Job has been marked to close: {closeJob.Status == 202}"); // You'll received a 202 in that case
await Task.Delay(TimeSpan.FromSeconds(2));
updatedJob = await routerClient.GetJobAsync(job.Value.Id);
Console.WriteLine($"Updated job status: {updatedJob.Value.Status == RouterJobStatus.Closed}");
Running into issues? This section should contain details as to what to do there.
Read more about JobRouter in Azure Communication Services
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit cla.microsoft.com.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact with any additional questions or comments.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
This package is not used by any NuGet packages.
Showing the top 2 popular GitHub repositories that depend on Azure.Communication.JobRouter:
| Repository | Stars |
|---|---|
|
Azure-Samples/communication-services-AI-customer-service-sample
A sample app for the customer support center running in Azure, using Azure Communication Services and Azure OpenAI for text and voice bots.
|
|
|
Azure-Samples/communication-services-dotnet-quickstarts
Sample code for Azure Communication Services .Net quickstarts
|
| Version | Downloads | Last Updated |
|---|---|---|
| 1.1.0-beta.1 | 2,928 | 4/12/2024 |
| 1.0.0 | 65,935 | 11/18/2023 |
| 1.0.0-beta.3 | 467 | 9/12/2023 |
| 1.0.0-beta.2 | 233 | 9/6/2023 |
| 1.0.0-beta.1 | 1,694 | 7/27/2023 |