Duplicate Detection - Background Service (standalone)
=====================================================

This package contains the background service that performs the Data Quality App's long-running
jobs: bulk duplicate detection, bulk merge, phonetic field fill, and data quality score
calculation. It polls Dynamics 365 / Dataverse for scheduled background operations and runs them.

The same package can be run two ways: as an Azure App Service WebJob, or from a folder on a
Windows machine.


BEFORE YOU START
----------------

1. The Data Quality App solution must be installed in the target Dynamics 365 / Dataverse
   environment, and its version must match this package.

2. You need a connection string for the environment. An application user with a client secret is
   recommended:

       AuthType=ClientSecret;Url=https://myorg.crm4.dynamics.com/;ClientId={app-id};ClientSecret={secret};

   The account must have permission to read and write the Duplicate Detection tables and to merge
   the records the customer wants de-duplicated.


OPTION A - AZURE APP SERVICE WEBJOB (recommended)
-------------------------------------------------

1. Create a Web App. Runtime stack: ASP.NET V4.7 or later (.NET Framework, NOT .NET Core).
   Choose a plan sized for continuous operation - the detection engine uses several worker
   threads, so the smallest tiers will throttle it.

2. Configuration > General settings: switch "Always on" to On, then Save. Without this the site
   unloads and the job stops polling.

3. Configuration > Connection strings: add

       Name:  CRM
       Value: <your connection string>
       Type:  Custom

4. Configuration > Application settings (all optional - see "Settings" below):

       SleepTimeSeconds = 15

5. WebJobs > Add:

       File upload: this zip file
       Type:        Continuous
       Scale:       Single Instance      <-- important, do not scale out

6. Refresh until the job shows as Running, then open Logs. You should see the version, the CRM
   user it logged in as, and the effective settings.


OPTION B - A WINDOWS MACHINE
----------------------------

1. Extract this zip to a folder, for example C:\Program Files\Cowia\DuplicateDetection.

2. Open run.exe.config and replace the placeholder connection string with the real one.

3. Start run.exe. It writes to the console and to the logs sub-folder.

   To keep it running unattended, use the Windows service installer package instead, or configure
   a Scheduled Task that starts run.exe at boot and restarts it on failure.

Only ONE instance may run against an environment at a time. Two instances would compete for the
same pending background operations.


SETTINGS
--------

All settings are optional: each one already defaults to the fast, recommended value, so an
unconfigured deployment is correct. They exist for tuning. On Azure, set them under Configuration >
Application settings; on a Windows machine, in run.exe.config. Portal values win over the file.

    SleepTimeSeconds                    15     Seconds between polls when idle.
    BulkDetectionServerSide             true   Run detection queries inside CRM. Biggest speed factor.
    BulkDetectionBatchSize              200    Records per background step (~ parallelism x 25).
    BulkDetectionDegreeOfParallelism    8      Concurrent detection workers. 1 = sequential.
    BulkDetectionUseExecuteMultiple     true   Batch row writes (only used when ServerSide = false).
    BulkMergeServerSide                 true   Run merges inside CRM, next to the database.
    BulkMergeMergesPerStep              20     Merges per step before saving state.
    BulkMergeDegreeOfParallelism        1      Experimental. Leave at 1.

run.exe.config contains a description of each setting.


VERIFYING IT WORKS
------------------

On startup the log shows the version, the CRM user, and every effective setting. If a value you
configured is not listed there, the key name does not match the table above.

While a bulk detection runs, the job record in CRM shows progress like

    Processing records 201-400/4284

Throughput is ultimately limited by Dataverse service protection limits (roughly 6,000 requests per
5-minute window per user), not by the machine or the App Service plan. Raising the parallelism past
the point where Dataverse starts throttling does not help.


TROUBLESHOOTING
---------------

"No CRM connection string found"
    The connection string is missing. On Azure, check the name is exactly CRM under Connection
    strings; in a folder deployment, check run.exe.config.

"Could not connect to CRM with the configured connection string"
    The connection string was found but rejected - wrong URL, expired secret, disabled account, or
    the user lacks privileges.

Nothing happens
    Check that a background operation exists in CRM with status Scheduled, and that only one
    instance of the service is running.

Logs are in the logs sub-folder next to run.exe, and in the WebJob Logs view on Azure.
