При запуску така помилка -
dbug: Microsoft.AspNetCore.Watch.BrowserRefresh.BlazorWasmHotReloadMiddleware[0]
Middleware loaded
dbug: Microsoft.AspNetCore.Watch.BrowserRefresh.BrowserScriptMiddleware[0]
Middleware loaded. Script /_framework/aspnetcore-browser-refresh.js (16507 B).
dbug: Microsoft.AspNetCore.Watch.BrowserRefresh.BrowserScriptMiddleware[0]
Middleware loaded. Script /_framework/blazor-hotreload.js (799 B).
dbug: Microsoft.AspNetCore.Watch.BrowserRefresh.BrowserRefreshMiddleware[0]
Middleware loaded: DOTNET_MODIFIABLE_ASSEMBLIES=debug, __ASPNETCORE_BROWSER_TOOLS=true
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://localhost:5200
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
Content root path: A:\Documents\CheckersApi
dbug: Microsoft.AspNetCore.Watch.BrowserRefresh.BrowserRefreshMiddleware[1]
Response markup is scheduled to include browser refresh script injection.
dbug: Microsoft.AspNetCore.Watch.BrowserRefresh.BrowserRefreshMiddleware[2]
Response markup was updated to include browser refresh script injection.
dbug: Microsoft.AspNetCore.Watch.BrowserRefresh.BrowserScriptMiddleware[0]
Script injected: /_framework/aspnetcore-browser-refresh.js
Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at CheckersApi.Engine.NativeKingsRow.CallGetMoveAscii(System.String, Int32)
at CheckersApi.Engine.NativeKingsRowAdapter.Suggest(CheckersApi.Contracts.SuggestRequest, System.Threading.CancellationToken)
at CheckersApi.Engine.CachedEngineAdapter+<>c__DisplayClass3_0.<Suggest>b__0(Microsoft.Extensions.Caching.Memory.ICacheEntry)
at Microsoft.Extensions.Caching.Memory.CacheExtensions.GetOrCreate[[System.__Canon, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](Microsoft.Extensions.Caching.Memory.IMemoryCache, System.Object, System.Func`2<Microsoft.Extensions.Caching.Memory.ICacheEntry,System.__Canon>)
at CheckersApi.Engine.CachedEngineAdapter.Suggest(CheckersApi.Contracts.SuggestRequest, System.Threading.CancellationToken)
at CheckersApi.Controllers.MoveController.Suggest(CheckersApi.Contracts.SuggestRequest)
at DynamicClass.lambda_method5(System.Runtime.CompilerServices.Closure, System.Object, System.Object[])
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor+SyncActionResultExecutor.Execute(Microsoft.AspNetCore.Mvc.ActionContext, Microsoft.AspNetCore.Mvc.Infrastructure.IActionResultTypeMapper, Microsoft.Extensions.Internal.ObjectMethodExecutor, System.Object, System.Object[])
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync()
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State ByRef, Scope ByRef, System.Object ByRef, Boolean ByRef)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State ByRef, Scope ByRef, System.Object ByRef, Boolean ByRef)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State ByRef, Scope ByRef, System.Object ByRef, Boolean ByRef)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State ByRef, Scope ByRef, System.Object ByRef, Boolean ByRef)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeAsync()
at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(Microsoft.AspNetCore.Http.HttpContext)
at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(Microsoft.AspNetCore.Http.HttpContext)
at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware+<Invoke>d__5.MoveNext()
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[[Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware+<Invoke>d__5, Swashbuckle.AspNetCore.SwaggerUI, Version=6.5.0.0, Culture=neutral, PublicKeyToken=4232c99127b3c254]](<Invoke>d__5 ByRef)
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[[Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware+<Invoke>d__5, Swashbuckle.AspNetCore.SwaggerUI, Version=6.5.0.0, Culture=neutral, PublicKeyToken=4232c99127b3c254]](<Invoke>d__5 ByRef)
at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(Microsoft.AspNetCore.Http.HttpContext)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware+<Invoke>d__4.MoveNext()
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[[Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware+<Invoke>d__4, Swashbuckle.AspNetCore.Swagger, Version=6.5.0.0, Culture=neutral, PublicKeyToken=62657d7474907593]](<Invoke>d__4 ByRef)
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[[Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware+<Invoke>d__4, Swashbuckle.AspNetCore.Swagger, Version=6.5.0.0, Culture=neutral, PublicKeyToken=62657d7474907593]](<Invoke>d__4 ByRef)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(Microsoft.AspNetCore.Http.HttpContext, Swashbuckle.AspNetCore.Swagger.ISwaggerProvider)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware+<Invoke>d__9.MoveNext()
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[[Microsoft.AspNetCore.Authorization.AuthorizationMiddleware+<Invoke>d__9, Microsoft.AspNetCore.Authorization.Policy, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]](<Invoke>d__9 ByRef)
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[[Microsoft.AspNetCore.Authorization.AuthorizationMiddleware+<Invoke>d__9, Microsoft.AspNetCore.Authorization.Policy, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]](<Invoke>d__9 ByRef)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(Microsoft.AspNetCore.Http.HttpContext)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware+<Invoke>d__6.MoveNext()
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[[Microsoft.AspNetCore.Authentication.AuthenticationMiddleware+<Invoke>d__6, Microsoft.AspNetCore.Authentication, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]](<Invoke>d__6 ByRef)
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[[Microsoft.AspNetCore.Authentication.AuthenticationMiddleware+<Invoke>d__6, Microsoft.AspNetCore.Authentication, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]](<Invoke>d__6 ByRef)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(Microsoft.AspNetCore.Http.HttpContext)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl+<Invoke>d__10.MoveNext()
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[[Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl+<Invoke>d__10, Microsoft.AspNetCore.Diagnostics, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]](<Invoke>d__10 ByRef)
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[[Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl+<Invoke>d__10, Microsoft.AspNetCore.Diagnostics, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]](<Invoke>d__10 ByRef)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(Microsoft.AspNetCore.Http.HttpContext)
at Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware.Invoke(Microsoft.AspNetCore.Http.HttpContext)
at Microsoft.WebTools.BrowserLink.Net.BrowserLinkMiddleware+<InvokeAsync>d__7.MoveNext()
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[[Microsoft.WebTools.BrowserLink.Net.BrowserLinkMiddleware+<InvokeAsync>d__7, Microsoft.WebTools.BrowserLink.Net, Version=18.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a]](<InvokeAsync>d__7 ByRef)
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[[Microsoft.WebTools.BrowserLink.Net.BrowserLinkMiddleware+<InvokeAsync>d__7, Microsoft.WebTools.BrowserLink.Net, Version=18.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a]](<InvokeAsync>d__7 ByRef)
at Microsoft.WebTools.BrowserLink.Net.BrowserLinkMiddleware.InvokeAsync(Microsoft.AspNetCore.Http.HttpContext)
at Microsoft.WebTools.BrowserLink.Net.VsContentMiddleware.InvokeAsync(Microsoft.AspNetCore.Http.HttpContext)
at Microsoft.AspNetCore.Watch.BrowserRefresh.BrowserRefreshMiddleware+<InvokeAsync>d__8.MoveNext()
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[[Microsoft.AspNetCore.Watch.BrowserRefresh.BrowserRefreshMiddleware+<InvokeAsync>d__8, Microsoft.AspNetCore.Watch.BrowserRefresh, Version=10.0.100.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]](<InvokeAsync>d__8 ByRef)
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[[Microsoft.AspNetCore.Watch.BrowserRefresh.BrowserRefreshMiddleware+<InvokeAsync>d__8, Microsoft.AspNetCore.Watch.BrowserRefresh, Version=10.0.100.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]](<InvokeAsync>d__8 ByRef)
at Microsoft.AspNetCore.Watch.BrowserRefresh.BrowserRefreshMiddleware.InvokeAsync(Microsoft.AspNetCore.Http.HttpContext)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol+<ProcessRequests>d__238`1[[System.__Canon, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[[System.Threading.Tasks.VoidTaskResult, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol+<ProcessRequests>d__238`1[[System.__Canon, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], Microsoft.AspNetCore.Server.Kestrel.Core, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].ExecutionContextCallback(System.Object)
at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(System.Threading.Thread, System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[[System.Threading.Tasks.VoidTaskResult, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol+<ProcessRequests>d__238`1[[System.__Canon, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], Microsoft.AspNetCore.Server.Kestrel.Core, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].MoveNext(System.Threading.Thread)
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[[System.Threading.Tasks.VoidTaskResult, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol+<ProcessRequests>d__238`1[[System.__Canon, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], Microsoft.AspNetCore.Server.Kestrel.Core, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].ExecuteFromThreadPool(System.Threading.Thread)
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading.PortableThreadPool+WorkerThread.WorkerThreadStart()
коли тестую ендпоінт POST /v1/move/suggest
Request
{
"gameId": "checkers-8x8",
"state": { "notation": "PDN", "position": "B:W18,19,22,25,27,28,30,32:B1,5,6,7,10,12,14,16" },
"level": "weak",
"limits": { "maxDepth": 12, "softTimeMs": 250, "hardTimeMs": 1200 }
}
ось в цьому рядку -> lock (s_lock) { rc = s_getmove!.Invoke(pdnPtr, buf, (UIntPtr)capacity); }
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace CheckersApi.Engine
{
internal static class NativeKingsRow
{
private static readonly object s_lock = new();
private static bool s_bound;
private static IntPtr s_module;
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate IntPtr enginename_delegate();
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate int protocolversion_delegate();
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate int staticevaluation_delegate(IntPtr pdn);
// Changed capacity parameter types from 'int' to 'UIntPtr' to match native size_t on x64
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate int getmove_delegate(IntPtr pdn, IntPtr buffer, UIntPtr capacity);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate int enginecommand_delegate(IntPtr cmd, IntPtr buffer, UIntPtr capacity);
private static enginename_delegate? s_enginename;
private static protocolversion_delegate? s_protocolversion;
private static staticevaluation_delegate? s_staticevaluation;
private static getmove_delegate? s_getmove;
private static enginecommand_delegate? s_enginecommand;
public static void BindOnce(IntPtr module)
{
if (s_bound) return;
lock (s_lock)
{
if (s_bound) return;
if (module == IntPtr.Zero)
throw new DllNotFoundException("Kingsrow64.dll module handle is null");
s_module = module;
s_enginename = GetExport<enginename_delegate>(s_module, "enginename");
s_protocolversion = GetExport<protocolversion_delegate>(s_module, "protocolversion");
s_staticevaluation = GetExport<staticevaluation_delegate>(s_module, "staticevaluation");
s_getmove = GetExport<getmove_delegate>(s_module, "getmove");
s_enginecommand = GetExport<enginecommand_delegate>(s_module, "enginecommand");
s_bound = true;
}
}
private static T GetExport<T>(IntPtr module, string name) where T : Delegate
{
var addr = NativeLibrary.GetExport(module, name);
return Marshal.GetDelegateForFunctionPointer<T>(addr);
}
private static void EnsureBound()
{
if (!s_bound)
throw new InvalidOperationException("NativeKingsRow not bound—call KingsRowBootstrap.Initialize first.");
}
public static string EngineName()
{
EnsureBound();
lock (s_lock)
{
var ptr = s_enginename!.Invoke();
return ptr == IntPtr.Zero ? "KingsRow" : (Marshal.PtrToStringAnsi(ptr) ?? "KingsRow");
}
}
public static int ProtocolVersion()
{
EnsureBound();
lock (s_lock) { return s_protocolversion!.Invoke(); }
}
public static int StaticEvaluation(string pdn)
{
EnsureBound();
IntPtr pdnPtr = Marshal.StringToHGlobalAnsi(pdn);
try { lock (s_lock) { return s_staticevaluation!.Invoke(pdnPtr); } }
finally { Marshal.FreeHGlobal(pdnPtr); }
}
public static (int rc, string reply) EngineCommand(string command, int capacity = 1024)
{
EnsureBound();
IntPtr cmdPtr = Marshal.StringToHGlobalAnsi(command);
IntPtr buf = AllocZero(capacity);
try
{
int rc;
// pass capacity as UIntPtr
lock (s_lock) { rc = s_enginecommand!.Invoke(cmdPtr, buf, (UIntPtr)capacity); }
var reply = rc == 0 ? ReadAnsi(buf, capacity) : string.Empty;
return (rc, reply);
}
finally
{
Marshal.FreeHGlobal(cmdPtr);
Marshal.FreeHGlobal(buf);
}
}
public static (int rc, string move) CallGetMoveAscii(string pdn, int capacity = 32768)
{
EnsureBound();
try
{
lock (s_lock) { _ = s_protocolversion!.Invoke(); }
_ = EngineCommand("about", 512);
}
catch { }
IntPtr pdnPtr = Marshal.StringToHGlobalAnsi(pdn);
IntPtr buf = AllocZero(capacity);
try
{
int rc;
// pass capacity as UIntPtr
lock (s_lock) { rc = s_getmove!.Invoke(pdnPtr, buf, (UIntPtr)capacity); } // тут помилка
if (rc != 0) return (rc, string.Empty);
return (rc, ReadAnsi(buf, capacity).Trim());
}
finally
{
Marshal.FreeHGlobal(pdnPtr);
Marshal.FreeHGlobal(buf);
}
}
private static IntPtr AllocZero(int size)
{
var p = Marshal.AllocHGlobal(size);
unsafe { new Span<byte>((void*)p, size).Clear(); }
return p;
}
private static string ReadAnsi(IntPtr buf, int capacity)
{
var bytes = new byte[capacity];
Marshal.Copy(buf, bytes, 0, capacity);
int len = Array.IndexOf<byte>(bytes, 0);
if (len < 0) len = capacity;
return Encoding.ASCII.GetString(bytes, 0, len);
}
}
}