Azure Service Fabric 中 WCF 服务的 HTTP 终结点

Posted

技术标签:

【中文标题】Azure Service Fabric 中 WCF 服务的 HTTP 终结点【英文标题】:HTTP Endpoint for WCF Service in Azure Service Fabric 【发布时间】:2017-05-14 23:23:39 【问题描述】:

如何获取一个 HTTP 端点,而不是一个 TCP 端点,该端点为 Service Fabric 中托管的 WCF 服务公开?我已经成功获得了 Microsoft 在WCF-based communication stack for Reliable Services 中提供的示例,但它使用了 TCP 端点。我已修改CreateServiceReplicaListeners() 代码以尝试使用BasicHttpBinding,但出现以下错误:

Unhealthy event: SourceId='System.RA', Property='ReplicaOpenStatus', HealthState='Warning', ConsiderWarningAsError=false.
Replica had multiple failures in API call: IStatelessServiceInstance.Open(); Error = System.InvalidOperationException (-2146233079)
The ChannelDispatcher at 'http://localhost:0/43a1f131-a650-46f7-8871-02cc9821e0d1/6428f811-6944-4097-bf4a-0538355a1cb5-131275909542555280/81291d44-3c65-4d9b-8e0c-17a731103b5f' with contract(s) '"ICalculator"' is unable to open its IChannelListener.
   at System.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult result)
   at Microsoft.ServiceFabric.Services.Communication.Wcf.Runtime.WcfCommunicationListener`1.b__0(IAsyncResult ar)
   at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.ServiceFabric.Services.Runtime.StatelessServiceInstanceAdapter.d__10.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.ServiceFabric.Services.Runtime.StatelessServiceInstanceAdapter.d__0.MoveNext()

有问题的代码:

/// <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()

    var binding = new BasicHttpBinding(BasicHttpSecurityMode.None)
    
        SendTimeout = TimeSpan.MaxValue,
        ReceiveTimeout = TimeSpan.MaxValue,
        OpenTimeout = TimeSpan.FromSeconds(5),
        CloseTimeout = TimeSpan.FromSeconds(5),
        MaxReceivedMessageSize = 1024 * 1024,
    ;
    binding.MaxBufferSize = (int)binding.MaxReceivedMessageSize;
    binding.MaxBufferPoolSize = Environment.ProcessorCount * binding.MaxReceivedMessageSize;
    return new[]
    
        new ServiceInstanceListener((context) =>
            new WcfCommunicationListener<ICalculator>(context, this, binding, "WcfServiceEndpoint"))
    ;

【问题讨论】:

【参考方案1】:

提供绑定地址:

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);
var listener = new WcfCommunicationListener<ICalculatorService>(
    serviceContext: context,
    wcfServiceObject: this,
    listenerBinding: new BasicHttpBinding(BasicHttpSecurityMode.None),
    address: new EndpointAddress(uri)
);

确保在服务清单中定义端点:

<Resources>
    <Endpoints>    
      <Endpoint Name="CalculatorEndpoint" Protocol="http" Type="Input" Port="80" />
    </Endpoints>
</Resources>

注意端点的名称与代码中使用的名称匹配。

确保为端口(在我的示例中为 80)创建负载平衡规则

一个工作示例是here。

【讨论】:

不得不使用您提供的示例中的更多内容,但该示例完美运行 - 谢谢! 是否可以在配置文件中配置端点(如普通 WCF),而不是在代码中?我在您共享的示例中看到了CalculatorService.EndpointTypeFromConfig 方法,但找不到它的使用方式。

以上是关于Azure Service Fabric 中 WCF 服务的 HTTP 终结点的主要内容,如果未能解决你的问题,请参考以下文章

Azure Service Fabric:无法运行本地 Service Fabric 群集

为 Service Fabric 配置 Azure 部署

在 Azure 中收集 Service Fabric 群集日志

Azure Service Fabric 群集端点不可访问

Azure App Service 与 Azure Service Fabric [关闭]

Azure Service Fabric 多租户