这些选项怎么样?
选项 1 - 在调用 gRPC 之前检查空凭据(这就是我所做的)
public interface IGrpcCredentials
{
string? GetIP();
int? GetPort();
}
用法
public void TryCallGrpcService(){
IGrpcCredentials credentials; //pretend this has been passed here from somewhere
var ip = credentials.GetIP();
if(ip is null){
return;
}
var port = credentials.GetPort();
if(port is null){
return;
}
CallGrpcService((string)ip, (int)port);
}
选项 2 - 使用占位符服务初始化 DI
您可以使用不提供凭据的占位符服务来初始化 DI。例如:
public interface IGrpcCredentials
{
bool AreCredentialsAvailable();
string GetIP();
int GetPort();
}
public class RealGrpcCredentials
{
private readonly string _ip;
private readonly int _port;
public RealGrpcCredentials(string ip, int port)
{
_ip = ip;
_port = port;
}
bool AreCredentialsAvailable() => true;
string GetIP() => _ip;
int GetPort() => _port;
}
public class PlaceholderGrpcCredentials
{
bool AreCredentialsAvailable() => false;
string GetIP() => return string.Empty;
int GetPort() => return -1;
}
用法
// register palceholder before knowing the credentials
container.Register<IGrpcCredentials, PlaceholderGrpcCredentials>();
//ask user for their crednetials
var credentials = ShowUserCredentialsDialog();
//register the actual credentials
container.Register<IGrpcCredentials, RealGrpcCredentials>(new RealGrpcCredentials(credentials.Ip, credentials.Port));
选项 3 - 在用户输入凭据后初始化 DI
在初始化 DI 容器之前等待用户填写其凭据。您必须创建一个不依赖于 DI 的启动窗口,并且可以在构建 DI 容器之前启动。