【发布时间】:2023-03-11 13:54:01
【问题描述】:
我正在将 Web api 服务连接到后端无状态服务。
Backservice 名为 MyProject.Management.Company,其代码为:
internal sealed class Company: StatelessService,ICompanyManagement
{
private readonly CompanyManagementImpl _impl;
public Tenents(StatelessServiceContext context, CompanyManagementImpl impl)
: base(context)
{
this._impl = impl;
}
/// <summary>
/// Optional override to create listeners (e.g., TCP, HTTP) for this service replica to handle client or user requests.
/// </summary>
/// <returns>A collection of listeners.</returns>
protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
{
return new[]
{
new ServiceInstanceListener(serviceContext => new FabricTransportServiceRemotingListener(serviceContext, this), "ServiceEndpoint")
};
}
/// <summary>
/// This is the main entry point for your service instance.
/// </summary>
/// <param name="cancellationToken">Canceled when Service Fabric needs to shut down this service instance.</param>
protected override async Task RunAsync(CancellationToken cancellationToken)
{
// TODO: Replace the following sample code with your own logic
// or remove this RunAsync override if it's not needed in your service.
long iterations = 0;
while (true)
{
cancellationToken.ThrowIfCancellationRequested();
ServiceEventSource.Current.ServiceMessage(this.Context, "Working-{0}", ++iterations);
await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);
}
}
public Task CreateCompany(Company company)
{
return _impl.CreateCompany(company);
}
public Task<List<Company>> GetAllCompanies()
{
return _impl.GetAllCompanies();
}
public Task<Company> GetCompanyById(string companyId)
{
return _impl.GetCompanyById(companyId);
}
}
代码是Listener 代码是从This Blog Post 采用的,甚至文档代码都没有编译documentation CreateServiceRemotingListenervextension 方法不存在。
ICompanyManagement 是从 IService 接口继承的接口,实现是通过 CompanyManagament 实现的,在这个阶段只是返回静态对象。
API 名为 MyProject.Portal,控制器代码为:
public class CompanyController : Controller
{
ICompanyManagement _proxy;
public CompanyController(StatelessServiceContext context)
{
string serviceUri = $"{context.CodePackageActivationContext.ApplicationName}" + "/MyProject.Management.Company";
_proxy = ServiceProxy.Create<ICompanyManagement>(new Uri(serviceUri));
}
// GET: api/Company
[HttpGet]
public async Task<JsonResult> Get()
{
try
{
var result = await _proxy.GetAllCompanies();
return this.Json(result);
}
catch (Exception ex)
{
throw ex;
}
}
}
代码运行时会返回以下错误。
在地址中找不到 NamedEndpoint 'V2Listener' '{"端点":{"ServiceEndpoint":"localhost:59286+12a705ed-11a5-4bf5-bafd-84179c966257-131719261525940414-9e876439-9294-4ec9-8b33-05f17515aaf4"}}' 对于分区 '12a705ed-11a5-4bf5-bafd-84179c966257'
最后:我正在使用 .netcore v2,服务结构 v6.2.274。
【问题讨论】:
-
你是如何在
CompanyController类中创建ServiceProxy的?此外,您不需要明确命名您的侦听器,因为只定义了一个侦听器。 -
您是否在 servicemanifest.xml 文件中添加了 V2 侦听器?
-
@PeterBons Create 函数是 ServiceProxy 是一个静态函数。
-
@LoekD 你能详细说明一下吗?我没有更改任何配置默认值。
-
是的,我明白了,但是您是如何创建
ServiceProxy实例的?你能发布那个代码吗?
标签: azure-service-fabric service-fabric-stateless service-fabric-on-premises