Determining if a Service supports an Action

Coordinator
May 31, 2011 at 2:35 AM
Edited May 31, 2011 at 2: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));
                else
                    Console.WriteLine(String.Format("{0} does not support port mapping.", lsService.Name));
            }
Sep 20, 2012 at 12:43 AM
Edited Sep 20, 2012 at 12:47 AM

it works.