امکان استفاده از کتابخانههای native در Blazor WASM 6x
نویسنده: وحید نصیری
تاریخ: ۱۴۰۰/۱۰/۱۳ ۱۵:۳۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
namespace BlazorWasmSQLite.Models;
public class Car
{
public int Id { get; set; }
public string Brand { get; set; }
public int Price { get; set; }
} using Microsoft.EntityFrameworkCore;
using BlazorWasmSQLite.Models;
namespace BlazorWasmSQLite.Data;
public class ClientSideDbContext : DbContext
{
public DbSet<Car> Cars { get; set; } = default!;
public ClientSideDbContext(DbContextOptions<ClientSideDbContext> options) :
base(options)
{
}
} <Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
<ItemGroup>
<!-- EF Core and Sqlite -->
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.1" />
</ItemGroup>
</Project> using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using BlazorWasmSQLite;
using Microsoft.EntityFrameworkCore;
using BlazorWasmSQLite.Data;
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
// Sets up EF Core with Sqlite
builder.Services.AddDbContextFactory<ClientSideDbContext>(options =>
options
.UseSqlite($"Filename=DemoData.db")
.EnableSensitiveDataLogging());
await builder.Build().RunAsync(); @page "/"
@using Microsoft.Data.Sqlite
@using Microsoft.EntityFrameworkCore
@using BlazorWasmSQLite.Data
@using BlazorWasmSQLite.Models
<PageTitle>Index</PageTitle>
<h1>Hello, world!</h1>
Welcome to your new app.
<SurveyPrompt Title="How is Blazor working for you?" />
@code {
[Inject]
private IDbContextFactory<ClientSideDbContext> _dbContextFactory { get; set; } = default!;
protected override async Task OnInitializedAsync()
{
await using var db = await _dbContextFactory.CreateDbContextAsync();
await db.Database.EnsureCreatedAsync();
// create seed data
if (!db.Cars.Any())
{
var cars = new[]
{
new Car { Brand = "Audi", Price = 21000 },
new Car { Brand = "Volvo", Price = 11000 },
new Car { Brand = "Range Rover", Price = 135000 },
new Car { Brand = "Ford", Price = 8995 }
};
await db.Cars.AddRangeAsync(cars);
await db.SaveChangesAsync();
}
await base.OnInitializedAsync();
}
} crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] Unhandled exception rendering component: The type initializer for 'Microsoft.Data.Sqlite.SqliteConnection' threw an exception. System.TypeInitializationException: The type initializer for 'Microsoft.Data.Sqlite.SqliteConnection' threw an exception. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.DllNotFoundException: e_sqlite3 at SQLitePCL.SQLite3Provider_e_sqlite3.SQLitePCL.ISQLite3Provider.sqlite3_libversion_number()
$ git clone https://github.com/cloudmeter/sqlite $ cd sqlite $ emcc sqlite3.c -shared -o e_sqlite3.o
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
<ItemGroup>
<!-- EF Core and Sqlite -->
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.1" />
<NativeFileReference Include="Data\e_sqlite3.o" />
</ItemGroup>
</Project>
# Get the emsdk repo git clone https://github.com/emscripten-core/emsdk.git # Enter that directory cd emsdk # Download and install the latest SDK tools. emsdk install latest # Make the "latest" SDK "active" for the current user. (writes .emscripten file) emsdk activate latest # Activate PATH and other environment variables in the current terminal emsdk_env.bat
C:\Users\{your pc name}\emsdk emcc C:\Users\{your pc name}\sqlite\sqlite3.c -shared -o D:\e_sqlite3.o warning : @(NativeFileReference) is not empty, but the native references won't be linked in, because neither $(WasmBuildNative), nor $(RunAOTCompilation) are 'true'. NativeFileReference=Data\e_sqlite3.o [D:\project\Yourproject\Yourproject.csproj] 1 Warning(s)
<RunAOTCompilation>true</RunAOTCompilation>
<PropertyGroup>
dotnet workload install wasm-tools