public static ServerHostBuilder UseRpc(this ServerHostBuilder builder, RpcMode mode = RpcMode.Tcp, bool isLibuv = true, ServerConfig config = null, params Assembly[] assemblies) { var ms = new ServerRpcMetadata(assemblies); UseTarsCodec(builder); builder.ConfigureServices(i => { var services = i.AddSingleton <ServerHandler, ServerHandler>(); foreach (var item in ms.metadatas) { services.AddSingleton(item.Value.InterfaceType, item.Value.ServantType); } services.AddSingleton(j => { foreach (var item in ms.metadatas) { var configMetadata = config?.ServantAdapterConfMap.Values.FirstOrDefault(x => x.ClassType == item.Value.ServantType); if (configMetadata != null) { item.Value.Servant = configMetadata.Servant; } item.Value.ServantInstance = j.GetRequiredService(item.Value.InterfaceType); item.Value.Codec = j.GetRequiredService(item.Value.CodecType) as CodecAttribute; } return(ms.ChangeRpcMetadataKeyForConfig()); }); }); ConfigHost(builder, mode, isLibuv); return(builder); }
private static void UseTarsCodec(ServerHostBuilder builder) { builder.ConfigureServices(i => { i.AddSingleton <IDictionary <string, CodecAttribute> >(j => { var dict = new Dictionary <string, CodecAttribute>(StringComparer.OrdinalIgnoreCase); var tars = j.GetRequiredService <TarsCodecAttribute>(); dict.Add(tars.Key, tars); foreach (var item in j.GetServices <CodecAttribute>()) { dict.Add(item.Key, item); } return(dict); }); }); }