c# - 外部客户端无法访问 Azure Service Fabric 上的 WCF 通信监听器

标签 c# wcf azure azure-service-fabric

我正在尝试使用 WCF 通信监听器将运行 WCF 的 Azure Web 角色迁移到 Azure Service Fabric 中的无状态服务。一切都在我的本地服务集群中运行。发布到 Azure 后,群集内的其他服务能够访问无状态 WCF 服务,但外部(互联网)客户端(包括我的开发计算机)无法连接,并出现暂时性网络错误。

我验证了资源组中的负载均衡器具有端口 80 和 8080 的规则/探针,并且已使用 TCP 和 HTTP 进行了测试。我还尝试在 WCF 客户端上设置分区解析器以指向服务集群上的“客户端连接端点”(默认情况下,它在服务集群内工作)。

此时,我不确定是否存在配置问题,或者外部(互联网)客户端是否可以连接到运行 WCF 通信监听器的无状态服务。

这是我的配置:

WCF 通信监听器

    private Func<StatelessServiceContext, ICommunicationListener> CreateListener()
    {
        return delegate (StatelessServiceContext context)
        {

            var host = new WcfCommunicationListener<IHello>(
                wcfServiceObject: this,
                serviceContext: context,
                endpointResourceName: "ServiceEndpoint",
                listenerBinding: CreateDefaultHttpBinding()
            );
            return host;
        };
    }

WCF 绑定(bind)

    public static Binding CreateDefaultHttpBinding()
    {
        var binding = new WSHttpBinding(SecurityMode.None)
        {
            CloseTimeout = new TimeSpan(00, 05, 00),
            OpenTimeout = new TimeSpan(00, 05, 00),
            ReceiveTimeout = new TimeSpan(00, 05, 00),
            SendTimeout = new TimeSpan(00, 05, 00),
            MaxReceivedMessageSize = int.MaxValue,
        };
        var quota = new XmlDictionaryReaderQuotas
        {
            MaxArrayLength = int.MaxValue,
            MaxDepth = int.MaxValue
        };
        binding.ReaderQuotas = quota;
        return binding;
    }

ServiceManifest.xml(我还使用了与各种端口绑定(bind)的默认 TCP)

<Endpoints>
  <Endpoint Name="ServiceEndpoint" Protocol="http" Port="8080" />
</Endpoints>

WCF 控制台应用

var address = new Uri("fabric:/ServiceFabricWcf.Azure/ServiceFabricWcf");
var client = GetClient(address, CreateDefaultHttpBinding());

try
  {
     var results = client.InvokeWithRetry(x => x.Channel.Hello());
     System.WriteLine($"Results from WCF Service: '{results}'");
     Console.ReadKey();
  }
  catch (Exception e)
  {
     System.Console.WriteLine("Exception calling WCF Service: '{e}'");
  }

WCF 客户端

    public static WcfServiceFabricCommunicationClient<IHello> GetClient(Uri address, Binding binding)
    {
        //ServicePartitionResolver.GetDefault(); Works with other services in cluster
        var partitionResolver = new ServicePartitionResolver("<clientConnectionEndpointOfServiceCluster>:8080");
        var wcfClientFactory = new WcfCommunicationClientFactory<IHello>(binding, null, partitionResolver);
        var sfclient = new WcfServiceFabricCommunicationClient<IHello>(wcfClientFactory, address, ServicePartitionKey.Singleton);
        return sfclient;
    }

WCF 客户端工厂

    public class WcfServiceFabricCommunicationClient<T> : ServicePartitionClient<WcfCommunicationClient<T>> where T : class
{
    public WcfServiceFabricCommunicationClient(ICommunicationClientFactory<WcfCommunicationClient<T>> communicationClientFactory,
                                               Uri serviceUri,
                                               ServicePartitionKey partitionKey = null,
                                               TargetReplicaSelector targetReplicaSelector = TargetReplicaSelector.Default,
                                               string listenerName = null,
                                               OperationRetrySettings retrySettings = null
                                               )
        : base(communicationClientFactory, serviceUri, partitionKey, targetReplicaSelector, listenerName, retrySettings)
    {

    }
}

最佳答案

这是一种适用于带有 WebHttpBinding 的 WCF 服务的方法:https://github.com/loekd/ServiceFabric.WcfCalc

尝试更改您的代码,使其不使用 endpointResourceName,而是使用包含显式 URL 的 address。 URL 应是集群的公共(public)名称,例如 mycluster.region.cloudapp.azure.com

编辑:url应该使用节点名称,这样更容易。

string host = context.NodeContext.IPAddressOrFQDN;
  var endpointConfig = context.CodePackageActivationContext.GetEndpoint    
    ("CalculatorEndpoint");
  int port = endpointConfig.Port;
  string scheme = endpointConfig.Protocol.ToString();
  string uri = string.Format(CultureInfo.InvariantCulture, 
    "{0}://{1}:{2}/", scheme, host, port);

关于c# - 外部客户端无法访问 Azure Service Fabric 上的 WCF 通信监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38237062/

相关文章:

c# - 具有不同线程的 C# 中的计时器

c# - 解决方案中的多个项目 - Entity Framework 和 ASP.Net MVC 4

powershell - 如何在azure自动化中设置服务主体帐户

c# - IIS 中的 WCF,在工作组模式下使用 MSMQ

azure - 更改 AAD B2C 中的登录名

azure - 部署新的 "latest"标签后,Kubernetes何时获得最新的容器镜像?

c# - 如何调用也访问数据库的DTO类?

c# - c#生成word文档

c# - 实例化对象的不同方式

jquery - WCF - "Encountered unexpected character ' c'。”