UWP中实现.NET Remoting功能是否有替代方案?
Hey there! Great question—first off, you’re right to note that .NET Remoting is legacy tech, and it’s not supported in UWP at all. Let’s break down the solid, supported alternatives you can use instead:
1. Windows Communication Foundation (WCF)
Since the docs explicitly recommend WCF for distributed apps, this is the most direct replacement if you’re working with existing service infrastructure. UWP apps can act as WCF clients (they can’t host WCF services) and support several common bindings:
BasicHttpBindingfor standard HTTP-based servicesNetTcpBinding(with limitations, e.g., restricted to private networks)WSHttpBindingfor secure, interoperable services
Here’s a quick example of calling a WCF service from UWP:
// Assuming you've added a service reference named "MyWcfService" var client = new MyWcfService.MyServiceClient(); try { var result = await client.GetDataAsync(42); // Handle the returned result } finally { if (client.State != System.ServiceModel.CommunicationState.Closed) { await client.CloseAsync(); } }
2. gRPC
If you’re building a new distributed system, gRPC is the modern, high-performance alternative Microsoft now prioritizes. It’s lightweight, uses HTTP/2, and supports seamless cross-platform communication. To use it in UWP:
- Install the
Grpc.Net.ClientNuGet package in your project - Generate client code from your .proto service definition
Example snippet:
using Grpc.Net.Client; // Establish a channel to your gRPC server using var channel = GrpcChannel.ForAddress("https://your-grpc-server-endpoint"); var client = new MyGrpcService.MyService.MyServiceClient(channel); // Call a remote method var response = await client.GetDataAsync(new MyRequest { Id = 42 }); // Process the response data
3. HTTP REST APIs with HttpClient
For simpler scenarios, a REST API paired with UWP’s built-in HttpClient is a straightforward, flexible choice. It works with any backend that exposes REST endpoints, making it perfect for lightweight remote communication.
Example:
using System.Net.Http; var httpClient = new HttpClient(); var response = await httpClient.GetAsync("https://your-api.com/data/42"); if (response.IsSuccessStatusCode) { var rawData = await response.Content.ReadAsStringAsync(); // Parse and use the data (e.g., with JSON serialization) }
All these options cover the core use cases of .NET Remoting—remote method calls, inter-process communication—while being fully supported in UWP and aligned with modern development best practices.
内容的提问来源于stack exchange,提问作者Ganeshkumar

