.NET Portable Class Library and UDP support
Asked Answered
C

2

17

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.

Copy answered 11/1, 2013 at 17:43 Comment(5)
Yes, the Xbox 360 is unchecked. I can't figure out what exactly the NCL is supposed to be comprised of. Every MSDN example I see is referencing System.Net.Sockets yet I do not have access to that in the .NET PCL. I am starting to think that I will have to move the discovery portion to the actual program as I cannot get UDP in any form in a portable manner.Copy
To fit the .NETCore profile, a class must have some commonality between the platforms. There just isn't any, no socket support at all for XBox, WP8 and Store does it totally differently. You can't use PCL if UDP is important, you have to pick a platform.Dasya
Yes, I believe I would have to create interfaces for the core architecture and then implement each set on the specific platform for UDP networking. It would seem that System.Net.Sockets is for traditional .NET applications and Windows.Networking.Sockets is for Windows Store applications.Copy
@HansPassant .Net, SL4, WP7 all have the Socket class. Still nothing in PCL.Binnings
You'll need to talk to David Kean, Microsoft doesn't employ me.Dasya
M
5

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.

Margaux answered 14/1, 2013 at 14:40 Comment(2)
I ended up doing similar. It's a shame that you can't use HttpWebRequest in a UDP manner for SSDP.Copy
There are different platforms that support the Socket class: .Net, Silverlight, Windows Phone. Yet, no PCL profile supports the class.Binnings
B
0

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.

Becnel answered 13/1, 2013 at 2:41 Comment(1)
I am not targeting the Xbox 360. NCL seems to include System.Net but not System.Net.Sockets. I understand the reasons for the PCL, I just thought that Sockets were supported across each platform (aside the Xbox). It would seem that is not the case.Copy

© 2022 - 2024 — McMap. All rights reserved.