azure-service-fabric - V2Listener 未找到错误

标签 azure-service-fabric service-fabric-stateless service-fabric-on-premises

我正在将 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接口(interface)的接口(interface),通过CompanyManagament实现,该阶段仅返回静态对象。

API 名为 MyProject.Portal, Controller 代码为:

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' not found in the address '{"Endpoints":{"ServiceEndpoint":"localhost:59286+12a705ed-11a5-4bf5-bafd-84179c966257-131719261525940414-9e876439-9294-4ec9-8b33-05f17515aaf4"}}' for partition '12a705ed-11a5-4bf5-bafd-84179c966257'

最后:我使用.netcore v2,service Fabric v6.2.274。

最佳答案

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

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

在您的服务 ( CompanyManagement ) list ( ServiceManifest.xml 文件) 中,确保端点设置为版本 2:

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

将 CreateServiceInstanceListeners 方法更改为:

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

最后在您的 Web api Controller 中注册您的服务代理,如下所示:

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

如果您按照以下步骤操作,它将起作用。

关于azure-service-fabric - V2Listener 未找到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50556355/

相关文章:

azure - 错误 System.BadImageFormatException 服务结构

list - 可靠集合上的用户-帖子-评论关系实现

c# - IReliableDictionary 上的 Linq 查询

azure-service-fabric - 跨分区共享可靠集合

azure - 订阅 Service Fabric 群集级别事件

c# - Azure Service Fabric 完成后如何重新运行 RunAsync 方法?

Azure Service Fabric 与 Docker 数据中心

azure-service-fabric - 动态 Servicefabric 设置和覆盖

azure - 如何设计 Azure Service Fabric 中的层