Service Fabric中的自定义侦听器+如何从客户端访问?

时间:2017-04-24 14:48:24

标签: azure-service-fabric

实际上我正在尝试学习Service Fabric及其自定义侦听器部分。为此,我创建了一个示例Service Fabric Stateful服务,除了在这里返回数字10之外什么都不做。

internal sealed class StatefulService1 : StatefulService, INumber
{

    protected override IEnumerable<ServiceReplicaListener> 
    CreateServiceReplicaListeners()
    {
        return new[] { new ServiceReplicaListener(context => new 
        HttpCommunicationListener(context), "HttpListener") };
    }

    public int GetNumber()
    {
        return 10;
    }
}

我为此创建了一个自定义侦听器,如下所示:

class HttpCommunicationListener : ICommunicationListener
{
    StatefulServiceContext serviceContext;

    string publishAddress;

    public HttpCommunicationListener(StatefulServiceContext 
 serviceContext)
    {
        this.serviceContext = serviceContext;
    }

    public void Abort()
    {
        throw new NotImplementedException();
    }

    public Task CloseAsync(CancellationToken cancellationToken)
    {
        throw new NotImplementedException();
    }

    public Task<string> OpenAsync(CancellationToken cancellationToken)
    {
        var codePackageContext = 
 this.serviceContext.CodePackageActivationContext;
 EndpointResourceDescription desc =  
 codePackageContext.GetEndpoint("ServiceEndpoint");

        var port = desc.Port;

        var address = string.Format("http://Address:{0}/", port);
        this.publishAddress = address.Replace("Address", 
    FabricRuntime.GetNodeContext().IPAddressOrFQDN);

        return Task.FromResult(publishAddress);
    }
}

在客户端,我正在努力使用我的侦听器访问GetNumber方法, 我的客户端实施:

class CustomCommunicationClient : ICommunicationClient
{
    public CustomCommunicationClient(ResolvedServicePartition partition)
    {
        this.ResolvedServicePartition = partition;
        // this.Endpoint = partition.GetEndpoint();
    }

    public ResolvedServicePartition ResolvedServicePartition { get; set; }

    public string ListenerName { get; set; }

    public ResolvedServiceEndpoint Endpoint { get; set; }
}

客户工厂impl:

class CustomCommunicationClientFactory : CommunicationClientFactoryBase<CustomCommunicationClient>
{
    ResolvedServicePartition partition;

    public CustomCommunicationClientFactory(ResolvedServicePartition partition)
    {
        this.partition = partition;
    }

    protected override void AbortClient(CustomCommunicationClient client)
    {
    }

    protected override Task<CustomCommunicationClient> CreateClientAsync(string endpoint, CancellationToken cancellationToken)
    {
        return Task.FromResult(new CustomCommunicationClient(this.partition));
    }

    protected override bool ValidateClient(CustomCommunicationClient client)
    {
        return true;
    }

    protected override bool ValidateClient(string endpoint, CustomCommunicationClient client)
    {
        return true;
    }
}

Main.cs

class Program
{
    private static string ServiceName = "fabric:/CustomCommunication.StatefulService/StatefulService1";

    static void Main(string[] args)
    {
        var servicePartition = GetServicePartitionAsync();
        var clientFactory = new CustomCommunicationClientFactory(servicePartition.Result);

        var partition = new ServicePartitionKey(1);
        var myServicePartitionClient = new ServicePartitionClient<CustomCommunicationClient>(clientFactory,
                                       new Uri(ServiceName), partition);
        Console.WriteLine("Running request client...");

        //var result =
        //    myServicePartitionClient.InvokeWithRetryAsync(client => client., CancellationToken.None).Result;

        Console.ReadKey();
    }

    private static Task<ResolvedServicePartition> GetServicePartitionAsync()
    {
        ServicePartitionResolver resolver = ServicePartitionResolver.GetDefault();

        Task<ResolvedServicePartition> partition = resolver.ResolveAsync(new Uri(ServiceName), 
                                             new ServicePartitionKey(1), CancellationToken.None);

        return partition;
    }
}

有人可以指导我怎么做吗?如果有人为我提供了代码,那就太好了。

0 个答案:

没有答案