Azure Service Fabric中WCF服务的HTTP端点 [英] HTTP Endpoint for WCF Service in Azure Service Fabric

查看:113
本文介绍了Azure Service Fabric中WCF服务的HTTP端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为服务矩阵中托管的WCF服务获取HTTP端点而不是TCP端点?我已经在用于可靠服务的基于WCF的通信堆栈,但它使用TCP端点.我已经修改了CreateServiceReplicaListeners()代码以尝试使用BasicHttpBinding,但是出现以下错误:

How do I get an HTTP endpoint, rather than a TCP one, exposed for a WCF Service hosted in Service Fabric? I've successfully got the sample provided by Microsoft in WCF-based communication stack for Reliable Services, but it uses a TCP endpoint. I've modified the CreateServiceReplicaListeners() code to try using a BasicHttpBinding, but get the following error:

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"))
    };
}

推荐答案

为绑定提供地址:

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)
);

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

Make sure to define the endpoint in the service manifest:

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

请注意,EndPoint的名称与代码中使用的名称匹配.

Notice the name of the EndPoint matches the name used in code.

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

Make sure you create a load balancing rule for the port (80 in my example)

一个有效的示例是此处.

这篇关于Azure Service Fabric中WCF服务的HTTP端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆