はじめに
遭遇した問題
Google Cloud Functions にコードを記述してデプロイしようとすると下記のエラーが発生する。
error CS0246: The type or namespace name '' could not be found (are you missing a using directive or an assembly reference?)
原因
エラー文の通り、Google Cloud Functions 側に無いライブラリを参照してしまっている。
対処法
手順1
Google Cloud Functions に記述したコードと同じコードを Visual Studio に再現してビルドが通る状態にし、Visual Studio のプロジェクトのルートディレクトリにある「.csproj」ファイルを開く。

手順2
Visual Studio 側の「.csproj」ファイルにはあって、Google Cloud Functions 側の「.csproj」ファイルには無い項目を Google Cloud Functions 側の「.csproj」ファイルに追加してデプロイする。
(下の例では<ImplicitUsings>enable</ImplicitUsings>
と<Nullable>enable</Nullable>
、<PackageReference Include="Google.Cloud.Functions.Framework" Version="1.0.0-beta02" />
、<PackageReference Include="JWT" Version="10.1.1" />
を Google Cloud Functions 側の「.csproj」ファイルに追加している)
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Google.Cloud.Functions.Framework" Version="1.0.0-beta02" />
<PackageReference Include="JWT" Version="10.1.1" />
</ItemGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Google.Cloud.Functions.Hosting" Version="2.0.0" />
</ItemGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<!-- 追加↓ -->
<ImplicitUsings>enable</ImplicitUsings>
<!-- 追加↓ -->
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Google.Cloud.Functions.Hosting" Version="2.0.0" />
<!-- 追加↓ -->
<PackageReference Include="Google.Cloud.Functions.Framework" Version="1.0.0-beta02" />
<!-- 追加↓ -->
<PackageReference Include="JWT" Version="10.1.1" />
</ItemGroup>
</Project>
備考
デプロイに失敗する際の対処法
エラー文
error NU1605: Warning As Error: Detected package downgrade:Google.Cloud.Functions.Framework from 2.0.0 to 1.0.0-beta02. Reference the package directly from the project to select a different version.
対処法
Google.Cloud.Functions.Hosting
のバージョンと Google.Cloud.Functions.Framework
のバージョンを同じにするように「.csproj」ファイルを修正する。
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Google.Cloud.Functions.Hosting" Version="2.0.0" />
<!-- 「1.0.0-beta02」から「2.0.0」に変更する↓ -->
<PackageReference Include="Google.Cloud.Functions.Framework" Version="2.0.0" />
<PackageReference Include="JWT" Version="10.1.1" />
</ItemGroup>
</Project>
最後に
株式会社ModelingXについて

富山県を拠点とするスタートアップ企業、ModelingXは、2022年4月に創業し、「テクノロジーで感動体験を提供する」というミッションを掲げています。当社が開発中の暮らし体験メタバース「MELIFE(ミライフ)」は、バーチャル住宅展示場や銀行相談、家具購入、婚活などができる「地域単位の新たな経済圏」を目指しています。
↓サービスサイトはこちらから↓
ModelingXはリモートワーク&フレックスタイム制を完備。
UnityやUEを共に探求する仲間を全国から募集しています。
↓企業詳細はこちら↓