0

I have added the following custom task to my project file following the guide in this answer:

<UsingTask
    TaskName="SetEnvironmentVariable"
    TaskFactory="CodeTaskFactory"
  AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v$(MSBuildToolsVersion).dll">

    <ParameterGroup>
        <Name/>
        <Value/>
    </ParameterGroup>
    <Task>
      <Using Namespace="System" />
      <Code Type="Fragment" Language="cs">
        <![CDATA[
        Environment.SetEnvironmentVariable(Name, Value);
      ]]>
      </Code>
    </Task>
  </UsingTask>

  <Target Name="BeforeBuild">
    <PropertyGroup>
      <TargetServer Condition="'$(TargetServer)' == ''">localhost</TargetServer>
    </PropertyGroup>
    <SetEnvironmentVariable Name="TARGET_SERVER" Value="$(TargetServer)"/>
  </Target>

This fails when I try to build the project in Visual Studio 2013 but succeeds when I build the project from the command line using MSBuild (I am using Visual Studio 2013 update 2).

The error message from Visual Studio is:

Target "BeforeBuild: (TargetId:13)" in project "D:\Dev\Tests.csproj" (target "Build" depends on it):
Initializing task factory "CodeTaskFactory" from assembly "C:\Program Files (x86)\MSBuild\12.0\bin\Microsoft.Build.Tasks.v12.0.dll".
Using "SetEnvironmentVariable" task from the task factory "Code Task Factory".
Task "SetEnvironmentVariable" (TaskId:14)
D:\Dev\Tests.csproj(130,29): error MSB4064: The "Name" parameter is not supported by the "SetEnvironmentVariable" task. Verify the parameter exists on the task, and it is a settable public instance property.
D:\Dev\Tests.csproj(130,5): error MSB4063: The "SetEnvironmentVariable" task could not be initialized with its input parameters. 
Done executing task "SetEnvironmentVariable" -- FAILED. (TaskId:14)

Any ideas why this would be the case?

As you can see from the output I've already turned the output of MSBuild to the diagnostic level but that hasn't provided any further clues. I've also tried playing around with the MSBuildToolsVersion to ensure that it is referencing the correct version (v12.0), which it is.

As far as I understand it these sort of issues should be a thing of the past as MSBuild is now embedded in Visual Studio. I've tried hard all morning to find answers to this particular issue using Google but it seems that this isn't a very common error message and now I'm stumped.

Community
  • 1
  • 1
MikeD
  • 5,004
  • 1
  • 25
  • 39

2 Answers2

0

Take a look at the guide you are following, you'll notice your XML is missing certain attributes that were supplied in that guide:

Update this:

  <ParameterGroup>
    <Name />
    <Value />
  </ParameterGroup>

To read:

  <ParameterGroup>
    <Name ParameterType="System.String" Required="true" />
    <Value ParameterType="System.String" Required="true" />
  </ParameterGroup>
Nicodemeus
  • 4,005
  • 20
  • 23
  • Sorry. Even with those attributes it still fails. Can you reproduce this issue or does it work correctly for you? – MikeD Jul 04 '14 at 14:54
0

I had this same issue I was editing the target file which contained the task, so my situation may be different. But restarting VS solved it, I'm guessing that the target I was editing was being cached so didn't see my changes.

David Martin
  • 11,764
  • 1
  • 61
  • 74