【发布时间】:2020-07-08 17:59:37
【问题描述】:
我有一个 .NET Core Reliable Service,它几乎是一个模板服务,从 Add > New Service Fabric Service > .NET Core Stateless Service 获得。这意味着它包含ServiceEventSource、一个继承自StatelessService 类的Stateless1 类和一个Program.cs。 Program.cs 的内容是默认的:
private static void Main()
{
try
{
ServiceRuntime.RegisterServiceAsync("Stateless1Type",
context => new Stateless1(context)).GetAwaiter().GetResult(); ServiceEventSource.Current.ServiceTypeRegistered(Process.GetCurrentProcess().Id, typeof(Stateless1).Name);
Thread.Sleep(Timeout.Infinite);
}
catch (Exception e)
{ ServiceEventSource.Current.ServiceHostInitializationFailed(e.ToString());
throw;
}
}
现在,一切正常,我可以正确启动 Service Fabric 应用程序并查看日志等。
我想要完成的是将此服务容器化,根据以下文章:https://docs.microsoft.com/en-us/azure/service-fabric/service-fabric-services-inside-containers。这意味着我添加了SFBinaryLoader.cs 并将以下文件添加到我的Program.cs:
static Program()
{
SFBinaryLoader.Initialize();
}
但是,通过尝试这样做会出现许多文档未解决的问题和问题。
我使用microsoft/service-fabric-reliableservices-windowsservercore:1803 作为基础镜像来构建我的容器。这是因为 Windows 服务器主机是 1803 版。运行 CreateDockerPackage.ps1 后,我注意到 Microsoft.ServiceFabric.Data.Interfaces.dll 和 System.Fabric.*.dll 正在被删除。
我认为这就是为什么我们必须在运行时再次提供。这是由SFBinaryLoader.cs 通过在AppDomain.CurrentDomain.AssemblyResolve 上添加一个事件侦听器来执行的。此类应查看FabricCodePath 环境变量(在运行时看起来绑定到C:\SFFabricBin\ 目录)并手动添加这些二进制文件。
我想说,为了让程序运行,我还必须从容器中删除 *.deps.json 文件,因为在运行 dotnet Stateless1.dll 之前我遇到了一个异常,指出在*.deps.json 文件未找到。
现在,看起来我的容器通过结构可以正常启动,并且程序集正在正确加载。但是,每当尝试通过以下行将服务注册到结构时:
ServiceRuntime.RegisterServiceAsync("Stateless1Type",
context => new Stateless1(context)).GetAwaiter().GetResult();
这会引发以下异常:
注册服务时发生异常 System.Fabric.FabricException:服务类型已注册。 ---> System.Runtime.InteropServices.COMException:来自 HRESULT 的异常:0x80071BD1 在 System.Fabric.Interop.NativeRuntime.IFabricRuntime.EndRegisterStatelessServiceFactory(IFabricAsyncOperationContext 上下文) 在 System.Fabric.Interop.Utility.c__DisplayClass22_0.b__0(IFabricAsyncOperationContext 上下文) 在 System.Fabric.Interop.AsyncCallOutAdapter2
1.Finish(IFabricAsyncOperationContext context, Boolean expectedCompletedSynchronously) --- End of inner exception stack trace --- at Microsoft.ServiceFabric.Services.Runtime.ServiceRuntime.RegisterServiceAsync(String serviceTypeName, Func2 serviceFactory,TimeSpan 超时,CancellationToken cancelToken) 在 C:\MyLocalPath\StatelessService\Program.cs:line 33 中的 Eventellect.Fabric.TestDocker.Program.Main() 处
你知道为什么会这样吗?我还觉得很奇怪,我的 已发布 服务结构对我的本地路径一无所知。
我已经在 try...catch 块内包围了 RegisterServiceAsync 调用,但是,即使线程成功进入睡眠并且我的 docker 容器没有停止,我的 Stateless1.cs 类中的操作也永远不会执行。这意味着甚至没有调用构造函数。
有什么我可能遗漏的吗?
【问题讨论】:
标签: c# docker .net-core azure-service-fabric