.NET Portable Class Library and UDP support

2019-04-19 00:09发布

问题:

I am writing a C# library for the Philips Hue Lights. I am trying to write the base API wrappers in the .NET portable set that way I can re-use this library for various platforms such as Windows 8/RT/WP. The API itself is all over HTTP using REST, so HttpWebRequest will serve most of my needs.

The network bridge that controls the lights themselves can be discovered using SSDP over UDP. However, I am unable to find a way to use UDP sockets in the portable class library (PCL).

There is no System.Net.Sockets available. There is nothing in the System.Net namespace that would allow it either. I have seen a DatagramSocket listed in Windows.Networking.Sockets but am unable to see that namespace in Intellisense.

Does anyone have any idea how I could get UDP functionality for SSDP under the .NET PCL?

I really do not want to have to separate the discovery functionality from the core library.

Right now I am targeting .NET 4.5 + SL 5 + WP 8 + .NET for Windows Store. I was under the impression that Sockets were available still.

回答1:

There isn't a common intersect for socket support between WinRT and WPF apps, and so it isn't available in PCL projects targeting them.

I have a PCL library targeting WPF and WinRT that interacts with a UDP discovery network, and the cleanest implementation I came up with involved creating an IUDPSocket interface in the PCL library that defines members for sending / receiving data and connecting to multicast groups. The WPF app implements my IUDPSocket using a System.Net.Sockets.Socket, and the RT app implements this using a Windows.Networking.Sockets.DatagramSocket.

The constructor of my discovery network client class (defined in the PCL project) takes a delegate which is used to create an instance of the IUDPSocket. I do this instead of passing in an initialized IUDPSocket instance so the calling code doesn't have to know about which port(s) or address(es) are involved.



回答2:

As described by this MSDN article, PCL are limited to to common assemblies for the target platforms:

In a Portable Class Library project, you specify the platforms you want to target, and only the supported assemblies for those platforms are referenced in your project. If you try to reference an assembly that is not supported for the platforms you have targeted, Visual Studio warns you of the incompatibility. The core assemblies (mscorlib.dll, System.dll, System.Core.dll, and System.Xml.dll) are supported on all platforms.

As stated in the summary table of the same article, Network Class Library (NCL) is supported for every platform but XBox 360. Further reading leads to the following information:

When you specify the platforms you want to target in a Portable Class Library project, the supported assemblies for those platforms are automatically referenced in your project. You do not have to add or remove assemblies. The referenced assemblies are automatically updated if you change the target platforms.

So, probably, you selected every platform during the project creation. Disabling XBox 360 should bring you back support for NCL and UdpClient. However, if you need support also for Xbox 360, you have two options: choosing a project type which is not PCL or manually implement UDP support.