Determining if a Service supports an Action

May 31, 2011 at 3:35 AM
Edited May 31, 2011 at 3:35 AM
Here is a simple example using synchronous discovery (for simplicity's sake) to use the descriptions to determine if a service supports port mapping:

Note that the Arguments is a dictionary which contains all information, argument names, possible values etc., but in this example we only
go as far as checking the number of arguments (the names of the arguments could be different between different devices if they don't strictly
adhere to the UPnP standards).


using ManagedUPnP;
using ManagedUPnP.Descriptions;


            Services lsServices = Discovery.FindServices("urn:schemas-upnp-org:service:WANPPPConnection:1");

            foreach (Service lsService in lsServices)
                ServiceDescription lsdDesc = lsService.Description();

                if (
                    lsdDesc.Actions.ContainsKey("AddPortMapping") &&
                    lsdDesc.Actions["AddPortMapping"].Arguments.Count == 8 &&
                    lsdDesc.Actions.ContainsKey("DeletePortMapping") &&
                    lsdDesc.Actions["DeletePortMapping"].Arguments.Count == 3)
                    Console.WriteLine(String.Format("{0} supports port mapping.", lsService.Name));
                    Console.WriteLine(String.Format("{0} does not support port mapping.", lsService.Name));
Sep 20, 2012 at 1:43 AM
Edited Sep 20, 2012 at 1:47 AM

it works.