.NET Portable Class Library and UDP support

China☆狼群 提交于 2019-12-04 22:46:17

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!