V2找不到侦听器错误



我正在将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);
}
}

代码是 侦听器代码 采用自此博客文章,甚至文档代码也不编译文档,CreateServiceRemotingListenervextension 方法不存在。

ICompanyManagement 是独立于 IService 接口的接口,实现是通过公司管理实现的,在此阶段只返回静态对象。

该 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;
}
}
}

当代码运行时,它会返回以下错误。

在地址中找不到命名终结点"V2侦听器 '{"Endpoints":{"ServiceEndpoint":"localhost:59286+12a705ed-11a5-4bf5-bafd-84179c966257-131719261525940414-9e876439-9294-4ec9-8b33-05f17515aaf4"}}' 对于分区"12A705ED-11A5-4BF5-BAFD-84179C966257">

最后:我正在使用.netcore v2,Service Fabric v6.2.274。

ICompanyManagement文件中使用后立即添加以下行:

[assembly: FabricTransportServiceRemotingProvider(RemotingListener = RemotingListener.V2Listener, RemotingClient = RemotingClient.V2Client)]

在服务 (CompanyManagement( 清单 (ServiceManifest.xml文件中( 确保终结点设置为版本 2:

<Resources>
<Endpoints>
<Endpoint Name="ServiceEndpointV2" />  
</Endpoints>
</Resources>

将 CreateServiceInstanceListeners 方法更改为:

protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
{
return this.CreateServiceRemotingInstanceListeners();
}

最后,在 Web API 控制器中注册服务代理,如下所示:

ICompanyManagement companyManagementClient = ServiceProxy.Create<ICompanyManagement>(new Uri($"fabric:/{applicationName}/{serviceName}"));

如果您按照这些步骤操作,它将起作用。

最新更新