SingletonSean / firebase-authentication-net-demo

A demo using client and server-side Firebase Authentication.
15 stars 6 forks source link

Issue creating EF Migrations with this package #1

Open infroz opened 1 year ago

infroz commented 1 year ago

This package seems to cause some issues when trying to add migrations with EF tools. Some quick googling has showed that it may only be an issue with initial migration. Leaving this issue open for a bit while I debug.

Error Code:

PM> Add-Migration InitialCreate
Build started...
Build succeeded.
An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: FirebaseAdminAuthentication.DependencyInjection.Services.FirebaseAuthenticationHandler Lifetime: Transient ImplementationType: FirebaseAdminAuthentication.DependencyInjection.Services.FirebaseAuthenticationHandler': Unable to resolve service for type 'FirebaseAdmin.FirebaseApp' while attempting to activate 'FirebaseAdminAuthentication.DependencyInjection.Services.FirebaseAuthenticationFunctionHandler'.) (Error while validating the service descriptor 'ServiceType: FirebaseAdminAuthentication.DependencyInjection.Services.FirebaseAuthenticationFunctionHandler Lifetime: Scoped ImplementationType: FirebaseAdminAuthentication.DependencyInjection.Services.FirebaseAuthenticationFunctionHandler': Unable to resolve service for type 'FirebaseAdmin.FirebaseApp' while attempting to activate 'FirebaseAdminAuthentication.DependencyInjection.Services.FirebaseAuthenticationFunctionHandler'.)
Unable to create an object of type 'DatabaseContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

Packages:

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

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

    <ItemGroup>
        <PackageReference Include="FirebaseAdmin" Version="2.4.0" />
        <PackageReference Include="FirebaseAdminAuthentication.DependencyInjection" Version="1.1.0" />
        <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.7" />
        <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.9" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.9" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.9" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.9">
          <PrivateAssets>all</PrivateAssets>
          <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
        <PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
    </ItemGroup>

    <ItemGroup>
      <Folder Include="Context\Migrations\" />
      <Folder Include="Controllers\" />
    </ItemGroup>

</Project>

Program.cs

using api.context;
using FirebaseAdmin;
using FirebaseAdmin.Auth;
using FirebaseAdminAuthentication.DependencyInjection.Extensions;
using Google.Apis.Auth.OAuth2;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

// Adding CORS policies
builder.Services.AddCors(options =>
{
    options.AddPolicy("DevelopmentPolicy",
        builder =>
        {
            builder.WithOrigins("http://localhost:3000") // Adjust this to your local development URL
                .AllowAnyHeader()
                .AllowAnyMethod()
                .AllowCredentials();
        });
});

FirebaseApp.Create(new AppOptions()
{
    Credential = GoogleCredential.FromJson(builder.Configuration.GetValue<string>("FIREBASE_CFG"))
});

builder.Services.AddDbContext<DatabaseContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("Server=(localdb)\\MSSQLLocalDB;Database=team.organizer.db")));

builder.Services.AddFirebaseAuthentication();
builder.Services.AddAuthorization();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

// Use the CORS policy
app.UseCors("DevelopmentPolicy");

app.UseAuthorization();
app.UseAuthentication();

app.MapControllers().RequireAuthorization();

app.Run();
GuilhermePSDG commented 1 year ago

Well, you probably already solved the mistake, but this is the solution.

_FirebaseApp.Create(new AppOptions()
{
    Credential = GoogleCredential.FromJson(builder.Configuration.GetValue<string>("FIREBASE_CFG"))
});_
var fireBaseApp = FirebaseApp.Create(new AppOptions()
{
    Credential = GoogleCredential.FromJson(builder.Configuration.GetValue<string>("FIREBASE_CFG"))
});

builder.Services.AddSingleton(fireBaseApp);