.NET Core 2.0是否支持WCF发现?IoT设备对接配置技术问询
First off, let's get straight to the point: .NET Core 2.0 does not natively support WCF Service Discovery. The early .NET Core WCF client libraries (like System.ServiceModel.Http or System.ServiceModel.Tcp) only covered basic client communication functionality—advanced features like discovery were left out of the 2.x release cycle.
But don't worry, there are practical workarounds depending on whether you can upgrade your runtime or need to stick with 2.0.
Workaround 1: Build Custom UDP Discovery (For .NET Core 2.0)
WCF's default discovery uses UDP multicast under the hood, so you can replicate this behavior manually without relying on official WCF APIs. Here's how:
On the WCF Service Side
When your service starts up, send a multicast UDP message to a standard port (WCF uses 3702 by default) that includes your service's endpoint address:
using System.Net; using System.Net.Sockets; using System.Text; // Run this when the WCF service initializes using var udpClient = new UdpClient(); var multicastIp = IPAddress.Parse("239.255.255.250"); var multicastEndpoint = new IPEndPoint(multicastIp, 3702); var serviceEndpoint = "net.tcp://your-service-ip:8000/YourWcfService"; var messageBytes = Encoding.UTF8.GetBytes(serviceEndpoint); udpClient.Send(messageBytes, messageBytes.Length, multicastEndpoint);
On Your .NET Core 2.0 IoT Client
Set up a UDP listener on the same multicast port to catch these messages and extract the service addresses:
using System.Net; using System.Net.Sockets; using System.Text; using var udpClient = new UdpClient(3702); udpClient.JoinMulticastGroup(IPAddress.Parse("239.255.255.250")); var remoteEndpoint = new IPEndPoint(IPAddress.Any, 0); var receivedBytes = udpClient.Receive(ref remoteEndpoint); var serviceAddress = Encoding.UTF8.GetString(receivedBytes); // Now use this address to instantiate your WCF client var wcfClient = new YourServiceClient(new NetTcpBinding(), new EndpointAddress(serviceAddress));
Workaround 2: Upgrade to .NET Core 3.0+ (Cleaner, Official Support)
If you can upgrade your IoT device's .NET Core runtime to 3.0 or later (3.1 is LTS, which is a safe bet), you get full official support for WCF Discovery. Here's how to implement it:
Install the Required NuGet Package
Use the NuGet CLI or Package Manager to add the discovery package:dotnet add package System.ServiceModel.DiscoveryUse
DiscoveryClientto Find Services
In your client code, you can now use the native WCF discovery APIs to locate services on the network:using System.ServiceModel; using System.ServiceModel.Discovery; // Create a discovery client using the default UDP endpoint using var discoveryClient = new DiscoveryClient(new UdpDiscoveryEndpoint()); // Define the criteria (match your service contract) var findCriteria = new FindCriteria(typeof(IYourServiceContract)); var findResponse = discoveryClient.Find(findCriteria); // Iterate over discovered endpoints and connect foreach (var endpoint in findResponse.Endpoints) { var wcfClient = new YourServiceClient(endpoint.Binding, endpoint.Address); // Perform your service operations here }
This approach is far more maintainable than rolling your own UDP solution, so it's worth considering if an upgrade is feasible for your IoT device.
内容的提问来源于stack exchange,提问作者Towen

