Visual Studio Development Bookmark and Share   
 index > Visual Studio Guidance Automation Toolkit > Unable to add project reference to a web project
 

Unable to add project reference to a web project

I have a solution template with two projects. One is a business layer class library, and the other is a web project. I'm trying to add the reference from the business layer to the web project, but keep getting a null reference error when attempting to retrieve the project. The code for it looks like this:

Code Block

<Arguments>
<Argument Name="WebApplicationName" Required="true">
<Converter Type="Microsoft.Practices.RecipeFramework.Library.Converters.CodeIdentifierStringConverter, Microsoft.Practices.RecipeFramework.Library" />
</Argument>
<Argument Name="WebProjectName">
<ValueProvider Type="Evaluator" Expression="$(WebApplicationName).Web">
<MonitorArgument Name="WebApplicationName" />
</ValueProvider>
</Argument>
<Argument Name="PresenterProjectName">
<ValueProvider Type="Evaluator" Expression="$(WebApplicationName).Presenter">
<MonitorArgument Name="WebApplicationName" />
</ValueProvider>
</Argument>
</Arguments>



...

Code Block

<Actions>
<Action Name="GetWebProject" Type="Microsoft.Practices.RecipeFramework.Library.Actions.GetProjectAction, Microsoft.Practices.RecipeFramework.Library">
<Input Name="ProjectName" RecipeArgument="WebProjectName" />
<Output Name="Project" />
</Action>
<Action Name="GetPresenterProject" Type="Microsoft.Practices.RecipeFramework.Library.Actions.GetProjectAction, Microsoft.Practices.RecipeFramework.Library">
<Input Name="ProjectName" RecipeArgument="PresenterProjectName" />
<Output Name="Project" />
</Action>
<Action Name="AddProjectReference" Type="Microsoft.Practices.RecipeFramework.Library.Solution.Actions.AddProjectReferenceAction, Microsoft.Practices.RecipeFramework.Library">
<Input Name="ReferringProject" ActionOutput="GetWebProject.Project" />
<Input Name="ReferencedProject" ActionOutput="GetPresenterProject.Project" />
</Action>
</Actions>



Anyone have any clue as to why the project would come back as null when trying to add a reference? Thanks.

wgolz  Friday, November 30, 2007 7:17 PM
I managed to find a solution to this problem, and figured I would post the answer here for others with the same issue. I ended up creating a custom action to handle this task. The guidance xml code looks like this:

Code Block

<Actions>
<Action Name="GetWebProject" Type="Microsoft.Practices.RecipeFramework.Library.Actions.GetProjectAction, Microsoft.Practices.RecipeFramework.Library">
<Input Name="ProjectName" RecipeArgument="WebProjectName" />
<Output Name="Project" />
</Action>
<Action Name="GetPresenterProject" Type="Microsoft.Practices.RecipeFramework.Library.Actions.GetProjectAction, Microsoft.Practices.RecipeFramework.Library">
<Input Name="ProjectName" RecipeArgument="PresenterProjectName" />
<Output Name="Project" />
</Action>
<Action Name="AddPresenterReference" Type="GuidancePackage.Actions.AddReferenceToWebProjectActionAction, GuidancePackage">
<Input Name="ReferringProjectName" RecipeArgument="PresenterProjectName" />
<Input Name="WebProjectName" RecipeArgument="WebProjectName" />
</Action>
</Actions>


And the action code looks like this:

Code Block

class AddReferenceToWebProjectActionAction : Action
{
#region Input Properties
[Input]
public string ReferringProjectName
{
get { return _referringProjectName; }
set { _referringProjectName = value; }
} string _referringProjectName;
[Input]
public string WebProjectName
{
get { return _webProjectName; }
set { _webProjectName = value; }
} string _webProjectName;
#endregion

#region IAction Members
public override void Execute()
{
EnvDTE.DTE dte = (EnvDTE.DTE)GetService(typeof(EnvDTE.DTE));

VSWebSite project = null;
Project referenceProject = null;

MessageBox.Show(_webProjectName);

foreach (Project proj in dte.Solution.Projects)
{
if (proj != null)
{
if (proj.Name.Contains(WebProjectName))
project = (VsWebSite.VSWebSite)proj.Object;
else if (proj.Name.Equals(ReferringProjectName))
referenceProject = proj;
}
}

if (!AddProjectReference(project, referenceProject))
MessageBox.Show("Error");
}

private bool AddProjectReference(VSWebSite aProject, Project aReferenceProject)
{
if (aProject == null || aReferenceProject == null)
return false;

aProject.References.AddFromProject(aReferenceProject);

return true;
}

public override void Undo()
{
throw new Exception("The method or operation is not implemented.");
}

#endregion
}


One thing to note is that in order to cast your web project as type VSWebSite, you need to include VsWebSite.Interop class library in your project. Also, for some reason you can't cast the project itself. That's why I ended up using proj.Object

Hope this helps others.
wgolz  Friday, November 30, 2007 9:56 PM
I managed to find a solution to this problem, and figured I would post the answer here for others with the same issue. I ended up creating a custom action to handle this task. The guidance xml code looks like this:

Code Block

<Actions>
<Action Name="GetWebProject" Type="Microsoft.Practices.RecipeFramework.Library.Actions.GetProjectAction, Microsoft.Practices.RecipeFramework.Library">
<Input Name="ProjectName" RecipeArgument="WebProjectName" />
<Output Name="Project" />
</Action>
<Action Name="GetPresenterProject" Type="Microsoft.Practices.RecipeFramework.Library.Actions.GetProjectAction, Microsoft.Practices.RecipeFramework.Library">
<Input Name="ProjectName" RecipeArgument="PresenterProjectName" />
<Output Name="Project" />
</Action>
<Action Name="AddPresenterReference" Type="GuidancePackage.Actions.AddReferenceToWebProjectActionAction, GuidancePackage">
<Input Name="ReferringProjectName" RecipeArgument="PresenterProjectName" />
<Input Name="WebProjectName" RecipeArgument="WebProjectName" />
</Action>
</Actions>


And the action code looks like this:

Code Block

class AddReferenceToWebProjectActionAction : Action
{
#region Input Properties
[Input]
public string ReferringProjectName
{
get { return _referringProjectName; }
set { _referringProjectName = value; }
} string _referringProjectName;
[Input]
public string WebProjectName
{
get { return _webProjectName; }
set { _webProjectName = value; }
} string _webProjectName;
#endregion

#region IAction Members
public override void Execute()
{
EnvDTE.DTE dte = (EnvDTE.DTE)GetService(typeof(EnvDTE.DTE));

VSWebSite project = null;
Project referenceProject = null;

MessageBox.Show(_webProjectName);

foreach (Project proj in dte.Solution.Projects)
{
if (proj != null)
{
if (proj.Name.Contains(WebProjectName))
project = (VsWebSite.VSWebSite)proj.Object;
else if (proj.Name.Equals(ReferringProjectName))
referenceProject = proj;
}
}

if (!AddProjectReference(project, referenceProject))
MessageBox.Show("Error");
}

private bool AddProjectReference(VSWebSite aProject, Project aReferenceProject)
{
if (aProject == null || aReferenceProject == null)
return false;

aProject.References.AddFromProject(aReferenceProject);

return true;
}

public override void Undo()
{
throw new Exception("The method or operation is not implemented.");
}

#endregion
}


One thing to note is that in order to cast your web project as type VSWebSite, you need to include VsWebSite.Interop class library in your project. Also, for some reason you can't cast the project itself. That's why I ended up using proj.Object

Hope this helps others.
wgolz  Friday, November 30, 2007 9:56 PM
Thanks for your share!
Darrell Wang  Wednesday, July 09, 2008 1:03 PM

You can use google to search for other answers

Custom Search

More Threads

• Can't run the Guidance Package
• GAX/GAT for VS 2005-2008 default templates removed
• Bug in Microsoft.Practices.RecipeFramework.Library.DteHelper.IsWebProject
• Cannot install DataAccessGuidancePackageSetup.msi
• confused about data sources
• SMART CLIENT SOFTWARE FACTORY Is not completely installed....Help Please!
• <ProjectGuid>{$WebGuidProp$}</ProjectGuid> Ignored -- very stragne behaviour
• Unable to uninstall several guidance packages
• no menu entry in VSS 08 for guidance automation packages
• Custom Directory Structure through GAT