ICommunicationListener
这是ZeroMQ 实现的粗略示例。此实现将充当 ZeroMQ ResponseSocket
,但可以轻松更改为RequestSocket
,SubscriberSocket
或您喜欢的任何类型的NetMQ.Sockets.*
套接字实现。当然,它需要在实现中提供更多细节,例如在检索消息时不抛出异常,但它应该清楚地了解它是如何完成的。它受到接口的现有 dotnetcore 实现的极大启发ICommunicationListener
。
public class ZeroMqResponseSocketCommunicationListener : ICommunicationListener, IDisposable
{
private readonly CancellationTokenSource _cancellationToken = new CancellationTokenSource();
private readonly ResponseSocket _responseSocket = new ResponseSocket();
private readonly ServiceContext _serviceContext;
private readonly string _endpointName;
public ZeroMqResponseSocketCommunicationListener(ServiceContext serviceContext, string endpointName)
{
if (string.IsNullOrEmpty(endpointName))
throw new ArgumentException("endpointName cannot be null or empty string.");
_serviceContext = serviceContext;
_endpointName = endpointName;
}
public Task<string> OpenAsync(CancellationToken cancellationToken)
{
var address = GetListenerUrl();
if (address == null)
throw new InvalidOperationException("No Url returned from ZeroMqResponseSocketCommunicationListener.GetListenerUrl");
_responseSocket.Bind(address);
ThreadPool.QueueUserWorkItem(state => MessageHandler(_cancellationToken.Token));
return Task.FromResult(address);
}
public Task CloseAsync(CancellationToken cancellationToken)
{
_responseSocket.Close();
return Task.FromResult(true);
}
public void Abort()
{
_responseSocket.Close();
}
private void MessageHandler(CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
var message = _responseSocket.ReceiveFrameBytes();
if (message != null)
throw new Exception($"Message {Encoding.UTF8.GetString(message)}");
}
}
private string GetListenerUrl()
{
var endpoints = _serviceContext.CodePackageActivationContext.GetEndpoints();
if (!endpoints.Contains(_endpointName))
throw new InvalidOperationException($"{_endpointName} not found in Service Manifest.");
var serviceEndpoint = _serviceContext.CodePackageActivationContext.GetEndpoint(_endpointName);
if (string.IsNullOrEmpty(serviceEndpoint.IpAddressOrFqdn))
throw new InvalidOperationException("IpAddressOrFqdn not set on endpoint");
if (serviceEndpoint.Port <= 0)
throw new InvalidOperationException("Port not set on endpoint");
var listenUrl = $"{serviceEndpoint.Protocol.ToString().ToLower()}://{serviceEndpoint.IpAddressOrFqdn}:{serviceEndpoint.Port}";
return listenUrl;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!disposing || _responseSocket == null) return;
try
{
_responseSocket.Close();
_responseSocket.Dispose();
}
catch (Exception ex)
{
ServiceEventSource.Current.Message(ex.Message);
}
}
}
并在您的应用结构服务中返回 ZeroMqResponseSocketCommunicationListener:
protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
{
yield return new ServiceInstanceListener(listener => new ZeroMqResponseSocketCommunicationListener(listener, "EndpointName"));
}
确保您在服务的 ServiceManifest.xml 中指定了一个端点:
<Resources>
<Endpoints>
<Endpoint Name="EndpointName" Port="80" Protocol="tcp" />
</Endpoints>
</Resources>