locked
Read and Write Configuration properties in a DTE project using Data Tools RRS feed

  • Question


  • I am working in Visual Studio Add-in which reads and alter configuration properties in a Visual studio Database project. The implemented solution in VS2010 working fine. The VS2010 version is using the library Microsoft.VisualStudio.Data.Schema.Package.dll. I need to migrate the same app into the VS2012. But in VS2012 we are using Data tools to deal with the Database project and the library that I am using is Microsoft.VisualStudio.Data.Tools.Package.dll.

    The VS2010 version is using IDatabaseProjectNode and IDatabaseProjectConfiguration to read config files. But these interfaces are no longer available in the Microsoft.VisualStudio.Data.Tools.Package.dll. Following are set of codes that using in the VS2010 version.

    Dim oPprojectNode As IDatabaseProjectNode = CType(project.Object, IDatabaseProjectNode) ' project is a DTE object 

     Dim oCurrentConfig As IDatabaseProjectConfiguration = CType(oPprojectNode.ConfigurationProvider.GetProjectConfiguration(msCurrentConfig), IDatabaseProjectConfiguration)

    Dim msDeploymentConfigFile As string = oCurrentConfig.GetConfigurationProperty("DeploymentConfigFile", False, DatabaseProjectPropertyValueType.EvaluatedValue)

    All these lines are failing when working with Data tools and VS2012. What are the alternative interfaces we do have in Microsoft.VisualStudio.Data.Tools.Package.dll in order to get the same functionality as in IDatabaseProjectNode and IDatabaseProjectConfiguration?


    Thursday, December 29, 2016 12:27 AM

Answers

  • Excuse my ignorance about SQL Server Database Projects, but I have installed SSDT for VS 2010 and I can see that in the SQL Server 2008 Database Project properties there is a Deploy tab that has a field for "SQL commands variables file", but for SSDT 11.0 for VS 2012, I cannot see the Deploy tab, which could explain that the "SqlCommandVariablesFile" value is null.

    Notice that if you change the property "SQLCommandVariablesFile" by, for example, "ConfigurationName", it returns the non-value "Debug", therefore the code seems correct.


    My portal and blog about VSX: http://www.visualstudioextensibility.com
    Twitter: https://twitter.com/VSExtensibility
    MZ-Tools productivity extension for Visual Studio: https://www.mztools.com

    Thursday, January 5, 2017 4:23 PM

All replies

  • Hi PriyalW,

    Based on your description, your case more related to VSX, I will move your case to VSX forum for better support.

     Best regards,

    Kristin


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, December 29, 2016 7:12 AM
  • Hi PriyalW,

    The API only for visual studio 2010, if you use visual studio 2012, it seems that you could access related property via project.ConfigurationManager.ActiveConfiguration, For more information, please refer to:

    https://msdn.microsoft.com/en-us/library/ms228959(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-5

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, December 29, 2016 9:39 AM
  • Hi Cole,

    Thanks a lot for your feedback. I can read the general properties using the method you mentioned here. I need to read some specific properties which are specific to SQL Server project opened in Visual Studio 2012 using Data tools. 

    As an example, I need to read properties such as "UseSandboxSettings" and "DeploymentConfigFile". I can easily read them using the Data.Schema.Package but not using the Data.Tools.Package ( In VS2012 project  Data.Schema.Package is no longer used )



    Tuesday, January 3, 2017 2:17 AM
  • Hi,

    The Microsoft.VisualStudio.Data.Tools.Package dll contains the interfaces, but most of them are now internal, not public.

    One approach is to use Reflection to get the objects. For example, this code (for VS 2015, assembly c:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\Microsoft\SQLDB\Microsoft.VisualStudio.Data.Tools.Package.dll) gets the private ProjectConfig property get of the public ProjectConfigProperties instance. The type of that property is public, ProjectConfig, which can be cast to the type DatabaseProjectConfig, also public. The DatabaseProjectConfig type has the GetConfigurationProperty / SetConfigurationProperty methods, but they are not public, so you need to use Reflection again (left as an exercise...).

    EnvDTE.DTE dte;
    EnvDTE.Configuration configuration;
    Microsoft.VisualStudio.Data.Tools.Package.Project.Internal.ProjectConfigProperties projectConfigProperties;
    object projectConfigObject;
    Microsoft.VisualStudio.Data.Tools.Package.Project.DatabaseProjectConfig databaseProjectConfig;
    
    dte = this.ServiceProvider.GetService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
    configuration = dte.Solution.Projects.Item(1).ConfigurationManager.ActiveConfiguration;
    projectConfigProperties = (Microsoft.VisualStudio.Data.Tools.Package.Project.Internal.ProjectConfigProperties) configuration.Object;
    projectConfigObject = projectConfigProperties.GetType().GetProperty("ProjectConfig", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(projectConfigProperties);
    databaseProjectConfig = (Microsoft.VisualStudio.Data.Tools.Package.Project.DatabaseProjectConfig)projectConfigObject;


    My portal and blog about VSX: http://www.visualstudioextensibility.com
    Twitter: https://twitter.com/VSExtensibility
    MZ-Tools productivity extension for Visual Studio: https://www.mztools.com

    Tuesday, January 3, 2017 6:11 PM
  • Hi Carlos,

    I managed to execute the code as you mentioned and it worked properly. Thanks !

    But when I tried to retrieve a config value using GetConfigurationProperty  , it is giving me an exception (Ambiguous match found  )

    Following is the code I did to retrieve a config value. 

    Dim configVal As Object = databaseProjectConfig.GetType().GetMethod("GetConfigurationProperty", BindingFlags.NonPublic Or BindingFlags.Instance).Invoke(databaseProjectConfig, New Object() {"UseSandboxSettings", False, DatabaseProjectPropertyValueType.EvaluatedValue})

    The EvaluatedValue property came from the Data.Schema.Package. I am not sure whether that caused the problem or not.  Following is how I retrieve the config value in VS2010 using Data.Schema.Package.

    Dim bSandboxedDeployment As Boolean = CBool(oCurrentConfig.GetConfigurationProperty("UseSandboxSettings", False, DatabaseProjectPropertyValueType.EvaluatedValue ))

    Please let me know where I got my reflection wrong.


    Wednesday, January 4, 2017 4:49 AM
  • Hi,

    There are two overloaded GetConfigurationProperty methods, hence the ambiguous match exception:

    Private Function GetConfigurationProperty(ByVal propertyName As String, ByVal resetCache As Boolean, ByVal evaluation As DatabaseProjectPropertyValueType) As String

    Private Function GetConfigurationProperty(ByVal propertyName As String, ByVal propertyCondition As String, ByVal resetCache As Boolean, ByVal evaluation As DatabaseProjectPropertyValueType) As String 

    You need to use the overloaded variant of GetMethod that allows you to specify the types of the parameters of the method that you are looking for (the first one):

    Type.GetMethod Method (String, BindingFlags, Binder, Type(), ParameterModifier())


    My portal and blog about VSX: http://www.visualstudioextensibility.com
    Twitter: https://twitter.com/VSExtensibility
    MZ-Tools productivity extension for Visual Studio: https://www.mztools.com


    Wednesday, January 4, 2017 6:10 AM
  • Hi,

    Thanks again for your feedback. I did the following coding. Everything works fine but I am getting a null result. It would be great if you can let me know where I went wrong. 

    Dim dte As EnvDTE.DTE
    Dim configuration As EnvDTE.Configuration
    Dim projectConfigProperties As Microsoft.VisualStudio.Data.Tools.Package.Project.Internal.ProjectConfigProperties
    
    ' Get the Assembly info 
    Dim dbProjectType As Type = assembly.GetType("Microsoft.VisualStudio.Data.Tools.Package.Project.DatabaseProjectPropertyValueType", True, True)
    Dim fields As FieldInfo() = dbProjectType.GetFields((BindingFlags.Public Or BindingFlags.Static Or BindingFlags.NonPublic))
    
    Dim databaseProjectConfig As Microsoft.VisualStudio.Data.Tools.Package.Project.DatabaseProjectConfig
    configuration = project.ConfigurationManager.ActiveConfiguration
    projectConfigProperties = CType(configuration.Object, Microsoft.VisualStudio.Data.Tools.Package.Project.Internal.ProjectConfigProperties)
    projectConfigObject = projectConfigProperties.GetType().GetProperty("ProjectConfig", System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.Instance).GetValue(projectConfigProperties)
    databaseProjectConfig = CType(projectConfigObject, Microsoft.VisualStudio.Data.Tools.Package.Project.DatabaseProjectConfig)
    
    Dim types As Type() = New Type() {GetType(String), GetType(Boolean), dbProjectType}
    ' Get method info 
    Dim myMethod As MethodInfo = databaseProjectConfig.GetType().GetMethod("GetConfigurationProperty", BindingFlags.NonPublic Or BindingFlags.Instance, Nothing, types, Nothing)
    ' Invoke the method 
    Dim val = myMethod.Invoke(projectConfigObject, New Object() {"SqlCommandVariablesFile", False, fields(0).GetValue(Nothing)})

    The val always returns null. The GetMethod works fine and I can get correct values in myMethod.

    Thanks ! 





    Thursday, January 5, 2017 3:32 AM
  • Excuse my ignorance about SQL Server Database Projects, but I have installed SSDT for VS 2010 and I can see that in the SQL Server 2008 Database Project properties there is a Deploy tab that has a field for "SQL commands variables file", but for SSDT 11.0 for VS 2012, I cannot see the Deploy tab, which could explain that the "SqlCommandVariablesFile" value is null.

    Notice that if you change the property "SQLCommandVariablesFile" by, for example, "ConfigurationName", it returns the non-value "Debug", therefore the code seems correct.


    My portal and blog about VSX: http://www.visualstudioextensibility.com
    Twitter: https://twitter.com/VSExtensibility
    MZ-Tools productivity extension for Visual Studio: https://www.mztools.com

    Thursday, January 5, 2017 4:23 PM