John Kattenhorn John Kattenhorn - 1 year ago 315
C# Question

Service Fabric Stateless Server Custom UDP Listener

We are trying to define a Service Fabric Stateless Service which listeners for UDP data.

We are working with Microsoft who have said that it IS supported and that I should set up for TCP; below is the snippet from the ServiceManifest.xml file :

<!-- This endpoint is used by the communication listener to obtain the port on which to
listen. Please note that if your service is partitioned, this port is shared with
replicas of different partitions that are placed in your code. -->
<Endpoint Name="ServiceEndpoint" Protocol="tcp" Port="12345" Type="Input" />

The service starts fine but I cannot make the service receive any UDP data and if I do a
netstat -a
I cannot see anything listening on the port in either TCP or UDP.

I've done plenty of research on online and I've not found much on creating custom ICommunicationListener but I'm hoping that someone else might be able to verify if this should be possible with SF.

Here is the ICommunicationListener implementation:

public UdpCommunicationListener(string serviceEndPoint,
ServiceInitializationParameters serviceInitializationParameters, Action<UdpReceiveResult> connector)
if (serviceInitializationParameters == null)
throw new ArgumentNullException(nameof(serviceInitializationParameters));

var endPoint = serviceInitializationParameters
.GetEndpoint(serviceEndPoint ?? "ServiceEndPoint");

_connector = connector;

_ipAddress = FabricRuntime.GetNodeContext().IPAddressOrFQDN;
_port = endPoint.Port;

_server = new UdpServer(_ipAddress, _port);


public Task<string> OpenAsync(CancellationToken cancellationToken)
_listener = _server.Listen(_connector);

return Task.FromResult($"udp::{_ipAddress}:{_port}");

public Task CloseAsync(CancellationToken cancellationToken)

return Task.FromResult(true);

public void Abort()

public class UdpServer
private readonly UdpClient _udpClient;
private IObservable<UdpReceiveResult> _receiveStream;

public UdpServer(string ipAddress, int port)
Id = Guid.NewGuid();

_udpClient = new UdpClient(ipAddress, port);

public Guid Id { get; }

public void Open()
_receiveStream = _udpClient.ReceiveStream().Publish().RefCount();

public void Close()
//TODO: Not sure how to stop the process

public IDisposable Listen(Action<UdpReceiveResult> process)
return _receiveStream.Subscribe(async r =>

Answer Source

There was an defect with the UdpServer component which I resolved and this now works hosted in a Service Fabric Service.

The issue with the code wa with this line :

_udpClient = new UdpClient(ipAddress, port);

This is the wrong overload for listening for traffic, it needed to be:

_udpClient = new UdpClient(port);

I did try :

_udpClient = new UdpClient(new IPAddress(IPAddress.Parse(_ipAddress)),port)

But this doesn't work; as the line from the Communication listen (as it describes itself) which retrieves the host returns the hostname and not the IPAddress, I think you could alter this behaviour by makes some changes to the manifest but just the port was sufficient for now.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download