locked
Unable to deploy dacpacs with C# object model RRS feed

  • Question

  • I am using the DacPackage and DacServices classes to daploy dacpacs from my solution and am having no success.  The DacServices is firing dozens of error messages during  deployment in the form of:

    The reference to external elements from the source named 'DBProject1_SSDT.dacpac' could not be resolved, because no such source is loaded.
    <repeat 100+ times>
    The reference to external elements from the source named 'master.dacpac' could not be resolved, because no such source is loaded.
    <repeat 3 times>
    The reference to external elements from the source named 'msdb.dacpac' could not be resolved, because no such source is loaded.
    <repeat 10+ times>
    The reference to external elements from the source named 'DBProject2_SSDT.dacpac' could not be resolved, because no such source is loaded.

    ... and the errors continue in similar fashion.  It's as if the variables defined in the projects aren't being evaluated.

    What am I doing wrong here?

    I have included my DAC helper class that deploys a dacpac

        /// <summary>
        /// Wraps Dac Package deployment functionality
        /// </summary>
        sealed class DAC
        {
            #region Public properties
            /// <summary>
            /// Gets or Sets the target database to deploy to
            /// </summary>
            public string TargetDatabase { get; set; }
            /// <summary>
            /// Gets or Sets the target connection for deployment
            /// </summary>
            public string TargetConnection { get; set; }
            /// <summary>
            /// Gets or sets the dacpac file for deployment
            /// </summary>
            public string DacPac
            {
                get
                {
                    return dac;
                }
                set
                {
                    dac = value;
                }
            }
    
            /// <summary>
            /// Gets a list of errors that occured during deployment
            /// </summary>
            public List<string> Errors
            {
                get
                {
                    return errors;
                }
            }
    
            /// <summary>
            /// Gets a list of warnings that occured during deployment
            /// </summary>
            public List<string> Warnings
            {
                get
                {
                    return warnings;
                }
            }
    
            /// <summary>
            /// Gets a list of messages that occured during deployment
            /// </summary>
            public List<string> Messages
            {
                get
                {
                    return messages;
                }
            }
            #endregion
    
            #region Private fields/properties
            private string dac = string.Empty;
            private List<string> errors = new List<string>();
            private List<string> warnings = new List<string>();
            private List<string> messages = new List<string>();
            #endregion
    
            #region Events
            public event EventHandler Complete;
            public event EventHandler DidNotComplete;
            #endregion
    
            /// <summary>
            /// Gets the deployment options used with any dacpac
            /// </summary>
            DacDeployOptions DeploymentOptions
            {
                get
                {
                    return new DacDeployOptions
                            {
                                BlockOnPossibleDataLoss = false,
                                DropConstraintsNotInSource = true,
                                DropDmlTriggersNotInSource = true,
                                DropIndexesNotInSource = true,
                                GenerateSmartDefaults = true,
                                IgnoreFilegroupPlacement = true,
                                IgnoreFillFactor = true,
                                IgnoreIndexPadding = true,
                                IgnoreKeywordCasing = true,
                                IgnoreLoginSids = true,
                                IgnoreObjectPlacementOnPartitionScheme = true,
                                IgnoreRouteLifetime = true,
                                IgnoreSemicolonBetweenStatements = true,
                                ScriptDatabaseCollation = true,
                                UnmodifiableObjectWarnings = true,
                                VerifyDeployment = true
                            };
                }
            }
    
            /// <summary>
            /// Executes the deployment of a DAC
            /// </summary>
            internal void Execute()
            {
                Errors.Clear();
                Warnings.Clear();
                Messages.Clear();
    
                var package = DacPackage.Load(DacPac, DacSchemaModelStorageType.File);            
                var service = new DacServices(TargetConnection);
                service.ProgressChanged += new EventHandler<DacProgressEventArgs>(service_ProgressChanged);
                service.Message += new EventHandler<DacMessageEventArgs>(service_Message);
                Task.Factory.StartNew(() =>
                    {
                        try
                        {
                            service.Deploy(package, TargetDatabase, true, DeploymentOptions);
                        }
                        catch (DacServicesException ex)
                        {
                            Errors.AddRange(ex.Messages.Select(m => m.Message));
                            OnDidNotComplete();
                        }
                    });
            }
    
            void service_Message(object sender, DacMessageEventArgs e)
            {
                System.Diagnostics.Debug.WriteLine(e.Message.Message);
                switch (e.Message.MessageType)
                {
                    case DacMessageType.Message:
                        Messages.Add(e.Message.Message);
                        break;
                    case DacMessageType.Warning:
                        Warnings.Add(e.Message.Message);
                        break;
                    case DacMessageType.Error:
                        Errors.Add(e.Message.Message);
                        break;
                }
            }
    
            void service_ProgressChanged(object sender, DacProgressEventArgs e)
            {
                switch (e.Status)
                {
                    case DacOperationStatus.Running:
                    case DacOperationStatus.Pending:
                    case DacOperationStatus.Completed:
                        {
                            Messages.Add(e.Message);
                            if (e.Status == DacOperationStatus.Completed)
                            {
                                OnComplete();
                            }
                        }
                        break;
                    case DacOperationStatus.Faulted:
                    case DacOperationStatus.Cancelled:
                        {
                            Errors.Add(e.Message);
                            OnDidNotComplete();
                        }
                        break;
                }
            }
    
            private void OnComplete()
            {
                var ev = this.Complete;
                if (ev != null)
                {
                    ev(this, EventArgs.Empty);
                }
            }
    
            private void OnDidNotComplete()
            {
                var ev = this.DidNotComplete;
                if (ev != null)
                {
                    ev(this, EventArgs.Empty);
                }
            }        
        }

    Monday, September 17, 2012 11:08 PM

Answers

  • To quickly update this thread, working with Aaron offline, I was able to successfully deploy his packages using the DacServices API, as was he when using a trimmed down sample app. He is doing further investigations as to what might be causing the issues on his end or in his wrapper class.

    Thanks,

    Adam


    Adam Mahood - Program Manager - Data-Tier Application Framework (DACFX)/SQL Server Data Tools

    • Marked as answer by Janet Yeilding Tuesday, September 25, 2012 4:55 PM
    Wednesday, September 19, 2012 3:23 PM

All replies

  • Hey ajhuddymsdn,

    It appears that the dacpac which you are attempting to deploy has references to other user dacpacs (ones you have created) as well as the SSDT system dacpacs - is this true? If this is the case what I think is happening is that these other dacpacs which you are referencing are not in the same directory as the dacpac which you are trying to deploy using DacServices. When you deploy using SqlPackage.exe or DacServices, other dacpacs which you reference must be in the same directory as the package you are attempting to deploy.

    Let me know if this solves your issue,

    Adam


    Adam Mahood - Program Manager - Data-Tier Application Framework (DACFX)/SQL Server Data Tools

    Tuesday, September 18, 2012 2:46 AM
  • To add to what Adam said. If you look in the output directory of ProjectA, you will see it contains all DACPACs you depend on.

    -GertD @ www.sqlproj.com

    Tuesday, September 18, 2012 5:04 AM
    Answerer
  • Hi Adam,

    Thanks for the reply.

    I have found that deploying the dacpac does finally finish, but it takes at least a minute before the process begins.  Also, there are hundreds of errors in the following format before the deployment begins.

    The reference to external elements from the source named 'xyz.dacpac' could not be resolved, because no such source is loaded.

    I am deploying with the object model using my class I posted above.

    I can send you my dacpacs privately if that helps.

    ... and to answer your question, yes all referenced dacpac files are in the same directory.

    • Edited by ajhuddymsdn Tuesday, September 18, 2012 4:31 PM
    Tuesday, September 18, 2012 4:28 PM
  • Hey,

    Please send me your email address at admahood at microsoft dot com and I will set up a FTP workspace which you can use to send us the packages, as this would be the most efficient way of getting a repro!

    Thanks,

    Adam


    Adam Mahood - Program Manager - Data-Tier Application Framework (DACFX)/SQL Server Data Tools

    Tuesday, September 18, 2012 5:06 PM
  • To quickly update this thread, working with Aaron offline, I was able to successfully deploy his packages using the DacServices API, as was he when using a trimmed down sample app. He is doing further investigations as to what might be causing the issues on his end or in his wrapper class.

    Thanks,

    Adam


    Adam Mahood - Program Manager - Data-Tier Application Framework (DACFX)/SQL Server Data Tools

    • Marked as answer by Janet Yeilding Tuesday, September 25, 2012 4:55 PM
    Wednesday, September 19, 2012 3:23 PM