UPnP (SSDP/UDA) library for .NET 10: device discovery, event subscriptions, SOAP control points, and a complete device server — for UPnP 1.0, 1.1, and 2.0 (including the 2020 revision).
MIT licensed.
- Discovery —
UpnpSearchService: multicast SSDP listening and search requests (ssdp:all, device/service targets), boot-id tracking, description caching. RaisesDeviceFound,DeviceUpdated,DeviceExpired,DeviceByeBye, andServiceFoundevents; discovered devices are kept inListedDevices. - Eventing —
HttpUpnpNotificationListener: TCP +System.IO.PipelinesNOTIFY receiver. Subscribe / renew / unsubscribe viaIServiceControlPoint. - Control —
ServiceControlPoint.GetControlPointAsync(...): SOAP action invocations and state variable queries, with per-spec-version behavior (1.0 / 1.1 / 2.0).
UpnpDeviceServer— full device role: serves the description document over HTTP, dispatches control (SOAP) and eventing (SUBSCRIBE/UNSUBSCRIBE) requests to their services, and advertises the device over SSDP.SsdpServer— periodicNOTIFY aliveadvertisements (IPv4 + IPv6 multicast), answersM-SEARCHwith200 OK, sendsNOTIFY byebyeon shutdown.UpnpEventServer— subscription management withSID/TIMEOUThandling,NOTIFYdelivery, and pruning of subscribers that fail repeatedly (threshold injectable).- Icons — icon bytes are registered with
SetIcon(icon, data)and served from internal storage; the description document carries only icon metadata, not the data. - Spec-compliant subscription validation (UPnP 2.0, Tables 4-4/4-5 and 2020 revision §4.1.1):
412whenCALLBACKis missing/invalid orNTis notupnp:event400whenSIDappears together withNT/CALLBACK(incompatible headers)412when a renewalSIDis unknown, expired, or belongs to a different service412when the delivery URL is not on the same network segment as the subscription URL (private ranges 10/8, 172.16/12, 192.168/16, plus loopback, AutoIP, link-local and ULA for IPv6)
await using var service = new UpnpSearchService("MyApp", "1.0");
service.DeviceFound += (s, d) => Console.WriteLine(d.FriendlyName);
await service.StartDiscoveryAsync();
IReadOnlyCollection<UpnpDiscoveryResponse> responses =
await service.SendDiscoveryRequestAsync("ssdp:all");
UpnpDevice device = service.ListedDevices[udn];
UpnpService svc = device.FindService(new Target("urn:upnp-org:service:Power:1"))!;
IServiceControlPoint cp = await ServiceControlPoint.GetControlPointAsync(svc, "MyApp", "1.0");
ActionResponse resp = await cp.SendActionRequestAsync(new QueryAction("urn:upnp-org:service:Power:1", "PowerState"));Subscribing to events:
using var listener = new HttpUpnpNotificationListener(networkInterface);
listener.NotificationReceived += (s, e) => Console.WriteLine(e.PropertySet);
UpnpEventSuscriptionResponse sub =
await cp.SendSubscriptionRequestAsync(listener, TimeSpan.FromHours(1));
// ... later
await cp.SendUnsubscriptionRequestAsync(listener);Uri baseUri = new("http://192.168.1.50:18080/");
UpnpIcon icon = new(new Uri(baseUri, "icon.jpg"), "image/jpeg", 48, 48, 24);
UpnpService power = new("urn:upnp-org:service:Power:1", "urn:upnp-org:serviceId:power",
new Uri(baseUri, "scpd/PowerSCPD.xml"),
new Uri(baseUri, "control/power"),
new Uri(baseUri, "eventing/power"));
UpnpDevice device = new(
"urn:upnp-org:device:root:1", "My Device", "MyCompany", null, null, "M1", null, null, null,
"uuid:12345678-1234-1234-1234-123456789abc", null,
[icon], [power], null, null)
{ IsRootDevice = true };
UpnpDescription description = new(SpecVersion.Upnp20, baseUri, device);
var server = new UpnpDeviceServer(18080, "192.168.1.50", description, HandleAction, scpDefinitions);
server.SetIcon(icon, File.ReadAllBytes("icon.jpg"));
await server.StartAsync();
// Push an event to all subscribers:
await server.Events.SendEventAsync(power.ServiceType, new Dictionary<string, string> { ["PowerState"] = "1" });
// ... later
await server.DisposeAsync();where HandleAction is an UpnpDeviceServer.ActionHandler:
async Task<ActionResponseContent> HandleAction(UpnpType service, UdaAction action)
{
// ... do the work ...
return new ActionResponseContent(); // success
// return new ActionResponseContent(new UpnpError(402, "Forbidden")); // failure
}UpnpDeviceServer options: scpDefinitions (SCP XML per service, for argument/state validation), serverHeader, a shared HttpClient, and maxConsecutiveFailures for dead-subscriber pruning.
- Pass your own
HttpClienttoGetControlPointAsync/UpnpDeviceServerwhen possible; a self-created client is disposed with the object that owns it. - Discovered
LOCATIONURLs are validated (http/https, no loopback/link-local) before fetching. - The
specs/folder with the UPnP specification PDFs is for local reference only and is git-ignored — the documents are copyrighted by the UPnP Forum / Open Connectivity Foundation and may not be redistributed.
dotnet build
dotnet testTests cover parsing (description/SCP/SSDP/SOAP/event fixtures, including real-world device descriptors), control points, discovery, and end-to-end server behavior (description serving, SOAP dispatch, icon serving, subscription validation, event delivery and pruning).