Question

When deploying a supplied package to a database:

DacServices service = new DacServices(connectionString);
DacPackage dacpac = DacPackage.Load(dacpacPath);
DacDeployOptions ddo = new DacDeployOptions();
ddo.BlockOnPossibleDataLoss = false;
service.Deploy(dacpac, dbName, true, ddo);

How could you cancel it through a cancellation token, and will this rollback the entire deployment operation?

So, I can create a cancellation token and pass it into the Deploy method, but then, how do I cancel it thereafter? But also, will it rollback the entire operation?

CancellationToken token = new CancellationToken();
service.Deploy(dacpac, dbName, true, ddo, token);
Was it helpful?

Solution

Setting the DacDeployOptions.IncludeTransactionalScripts flag to "true" will wrap the model deployment steps in transactions. If the deployment fails or is cancelled the engine will attempt to roll back these steps. Note that pre/post deployment script steps are not placed in transactions so if you use these you need to make them transactional yourself. This is because a) the engine doesn't parse them or try to understand their contents and b) they could contain code that's incompatible with transactions, so the engine doesn't try to enforce transactionality.

To actually call cancel you can use a CancellationTokenSource. I think the code would look something like:

DacServices service = new DacServices(connectionString);
DacPackage dacpac = DacPackage.Load(dacpacPath);
DacDeployOptions ddo = new DacDeployOptions();
ddo.BlockOnPossibleDataLoss = false;
ddo.IncludeTransactionalScripts = true;
CancellationTokenSource tokenSource = new CancellationTokenSource();
service.Deploy(dacpac, dbName, true, ddo, tokenSource.Token);
tokenSource.Cancel();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top