C
C#16mo ago
fooo1

❔ getting sensitive config from .csproj

I would like to get some env vars in csproj, however when defining them in an ItemGroup I get compilation errors - msbuild error: specify the name of the target. Presumably it's something obvious, but I can't find it.. :
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<!--commenting this ItemGroup fixes the issue-->
<ItemGroup>
<Email__Host>smtp.gmail.com</Email__Host>
<Email__UserName>$(EMAIL_USERNAME)</Email__UserName>
<Email__Password>$(EMAIL_PASSWORD)</Email__Password>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0-preview.2.23128.3" />
</ItemGroup>

</Project>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<!--commenting this ItemGroup fixes the issue-->
<ItemGroup>
<Email__Host>smtp.gmail.com</Email__Host>
<Email__UserName>$(EMAIL_USERNAME)</Email__UserName>
<Email__Password>$(EMAIL_PASSWORD)</Email__Password>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0-preview.2.23128.3" />
</ItemGroup>

</Project>
7 Replies
Pobiega
Pobiega16mo ago
I've never heard of setting env vars in the csproj before, and even if you could that would be a bad idea that means having secrets in your checked in source
fooo1
fooo116mo ago
that exactly prevents checking them in source as they're just references, i.e. $(EMAIL_USERNAME)
Pobiega
Pobiega16mo ago
Ah, I see, you want to read the value in the csproj.. but that is a bit weird too, what would <Email__Host> do? thats not a standard property of a project file if you want to get the value ofEMAIL_USERNAME inside your program, just use var emailUsername = System.Environment.GetEnvironmentVariable("EMAIL_USERNAME"); it returns string? there are also libraries to let you use .env files, if you want that, or to register your environment variables as configuration options in the standard .net config system
fooo1
fooo116mo ago
Now that you mention it - indeed why not just use System.Environment.GetEnvironmentVariable - no need to modify csproj. I think I got derailed as I originally wanted different envs for prod and dev, but again - just getting env vars directly could be enough
Pobiega
Pobiega16mo ago
side point: I dont recommend using EFCore 8 preview with .net 7 🙂 so downgrade to 7.0.4, or upgrade your .NET to the .8 preview
fooo1
fooo116mo ago
ah thanks - will do that as well
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.