使用WCF HTTPS端点从控制台应用程序调用Service Fabric服务

时间:2017-02-13 11:24:31

标签: wcf azure azure-service-fabric

我在Azure(而非本地)的Service Fabric群集中托管了一项服务,并且我尝试使用本地计算机上的控制台应用程序调用其中的方法。使用WCF进行通信,我在特定端口上的应用程序中设置了HTTPS端点,并为Azure门户中的端口配置了负载平衡规则。群集有6个节点,应用程序是群集中唯一部署的节点。

已遵循GitHub上的ServiceFabric.WcfCalc(link),该服务器使用HTTP端点在本地群集上运行,但在部署后无法使用HTTPS端点调用服务上的方法。我需要做些什么才能让它发挥作用?尝试过遵循示例here,但不知道如何为多个节点上的服务配置HTTPS以供控制台应用程序访问。

提前致谢。

编辑这是我用来调用服务方法的客户端代码。我将fabric:/ URI传递给构造函数。

public class Client : ServicePartitionClient<WcfCommunicationClient<IServiceInterface>>, IServiceInterface
{
    private static ICommunicationClientFactory<WcfCommunicationClient<IServiceInterface>> communicationClientFactory;

    static Client()
    {
        communicationClientFactory = new WcfCommunicationClientFactory<IServiceInterface>(
            clientBinding: new BasicHttpBinding(BasicHttpSecurityMode.Transport));
    }

    public Client(Uri serviceUri)
        : this(serviceUri, ServicePartitionKey.Singleton)
    { }

    public Client(
        Uri serviceUri,
        ServicePartitionKey partitionKey)
        : base(
            communicationClientFactory,
            serviceUri,
            partitionKey)
    { }


    public Task<bool> ServiceMethod(DataClass data)
    {
        try
        {
             //It hangs here
            return this.InvokeWithRetry((c) => c.Channel.ServiceMethod(data));
        }
        catch (Exception)
        {
            throw;
        }
    }
}

在本地计算机上调试我的控制台应用程序时,应用程序挂起在InvokeWithRetry调用上,该调用调用Service Fabric中我的服务中的方法。应用程序不会抛出任何异常,也不会返回到Visual Studio中的调试器。

2 个答案:

答案 0 :(得分:0)

  • 确保使用唯一网址运行每个服务实例/副本。

  • 请务必使用WebHttpBinding致电WebHttpSecurityMode.Transport构造函数。

  • 确保使用与服务清单端点declaration中相同的端口编号(可能为443)注册网址。

  • 确保将端点配置为HTTPS

答案 1 :(得分:0)

您在Service Fabric中看到的警告告诉您已经注册了另一个服务,以便在节点上的port 443上进行侦听。这意味着Service Fabric无法启动您的服务(因为它在尝试使用http.sys注册URL时会在内部抛出异常)。您可以将服务端口更改为与现有服务不冲突的其他端口,例如:

<Resources>
  <Endpoint Name="CalculatorEndpoint" Protocol="https" Type="Input" Port="44330" />
</Endpoints>

如果您在https://{cluster_name}.{region}.cloudapp.azure.com:19080上登录Service Fabric Explorer,您应该能够看到其中正在运行的其他应用程序和服务。如果将服务一直扩展到节点,您应该能够看到现有服务的已注册端点,包括端口。

<强>加成 您可以使用FabricClient为所有已注册的端点查询集群

var fabricClient = new FabricClient();
var applicationList = fabricClient.QueryManager.GetApplicationListAsync().GetAwaiter().GetResult();
foreach (var application in applicationList)
{
    var serviceList = fabricClient.QueryManager.GetServiceListAsync(application.ApplicationName).GetAwaiter().GetResult();
    foreach (var service in serviceList)
    {
        var partitionListAsync = fabricClient.QueryManager.GetPartitionListAsync(service.ServiceName).GetAwaiter().GetResult();
        foreach (var partition in partitionListAsync)
        {
            var replicas = fabricClient.QueryManager.GetReplicaListAsync(partition.PartitionInformation.Id).GetAwaiter().GetResult();
            foreach (var replica in replicas)
            {
                if (!string.IsNullOrWhiteSpace(replica.ReplicaAddress))
                {
                    var replicaAddress = JObject.Parse(replica.ReplicaAddress);
                    foreach (var endpoint in replicaAddress["Endpoints"])
                    {
                        var endpointAddress = endpoint.First().Value<string>();
                        Console.WriteLine($"{service.ServiceName} {endpointAddress} {endpointAddress}");
                    }
}}}}}

只需使用正确的FabricClient凭据(如果它是安全的群集)运行它,您应该看到它列出了所有服务的所有端点。这应该可以帮助您找到具有端点的那个:443