@@ -367,11 +367,41 @@ protected internal virtual void Replace(ManagementJobConfiguration config, Inven
367367 {
368368 _logger . LogInformation ( $ "Re-binding { virtualServiceBackups . Count } virtual service port bindings after certificate replacement.") ;
369369
370- foreach ( var backup in virtualServiceBackups )
371- {
372- _logger . LogTrace ( $ "Re-binding { backup . TemplateType } template '{ backup . TemplateName } ' to virtual service '{ backup . VirtualServerName } ' port { backup . Port } ") ;
373- apiClient . BindTemplateToVirtualService ( backup . VirtualServerName , backup . Port , backup . Protocol , backup . TemplateType , backup . TemplateName ) ;
374- }
370+ // 1. Group backups by virtual server + port + protocol
371+ var portBindings = virtualServiceBackups
372+ . GroupBy ( b => new { b . VirtualServerName , b . Port , b . Protocol } )
373+ . ToList ( ) ;
374+
375+ // 2. For each unique port, build a single update with both template bindings
376+ foreach ( var bindingGroup in portBindings )
377+ {
378+ var vsName = bindingGroup . Key . VirtualServerName ;
379+ var port = bindingGroup . Key . Port ;
380+ var protocol = bindingGroup . Key . Protocol ;
381+
382+ var update = new VirtualServerPortUpdate
383+ {
384+ PortNumber = port ,
385+ Protocol = protocol
386+ } ;
387+
388+ foreach ( var b in bindingGroup )
389+ {
390+ if ( b . TemplateType . Equals ( "server-ssl" , StringComparison . OrdinalIgnoreCase ) )
391+ update . TemplateServerSsl = b . TemplateName ;
392+ else if ( b . TemplateType . Equals ( "client-ssl" , StringComparison . OrdinalIgnoreCase ) )
393+ update . TemplateClientSsl = b . TemplateName ;
394+ else
395+ throw new ArgumentException ( $ "Unknown template type: { b . TemplateType } ") ;
396+ }
397+
398+ var bindRequest = new VirtualServerPortUpdateRequest { Port = update } ;
399+ var requestJson = JsonConvert . SerializeObject ( bindRequest ) ;
400+ _logger . LogTrace ( $ "Re-binding templates to VS={ vsName } Port={ port } Protocol={ protocol } => { requestJson } ") ;
401+
402+ apiClient . PutVirtualServerPort ( vsName , port , protocol , requestJson ) ;
403+ }
404+
375405 }
376406
377407 apiClient . WriteMemory ( ) ;
@@ -388,8 +418,41 @@ protected internal virtual void Replace(ManagementJobConfiguration config, Inven
388418 {
389419 try
390420 {
391- _logger . LogTrace ( $ "Rolling back binding for virtual service '{ backup . VirtualServerName } ' port { backup . Port } ") ;
392- apiClient . BindTemplateToVirtualService ( backup . VirtualServerName , backup . Port , backup . Protocol , backup . TemplateType , backup . OriginalTemplateName ) ;
421+ _logger . LogTrace ( $ "Rolling back binding for virtual service '{ backup . VirtualServerName } ' port { backup . Port } ") ;
422+ // 1. Group backups by virtual server + port + protocol
423+ var portBindings = virtualServiceBackups
424+ . GroupBy ( b => new { b . VirtualServerName , b . Port , b . Protocol } )
425+ . ToList ( ) ;
426+
427+ // 2. For each unique port, build a single update with both template bindings
428+ foreach ( var bindingGroup in portBindings )
429+ {
430+ var vsName = bindingGroup . Key . VirtualServerName ;
431+ var port = bindingGroup . Key . Port ;
432+ var protocol = bindingGroup . Key . Protocol ;
433+
434+ var update = new VirtualServerPortUpdate
435+ {
436+ PortNumber = port ,
437+ Protocol = protocol
438+ } ;
439+
440+ foreach ( var b in bindingGroup )
441+ {
442+ if ( b . TemplateType . Equals ( "server-ssl" , StringComparison . OrdinalIgnoreCase ) )
443+ update . TemplateServerSsl = b . TemplateName ;
444+ else if ( b . TemplateType . Equals ( "client-ssl" , StringComparison . OrdinalIgnoreCase ) )
445+ update . TemplateClientSsl = b . TemplateName ;
446+ else
447+ throw new ArgumentException ( $ "Unknown template type: { b . TemplateType } ") ;
448+ }
449+
450+ var bindRequest = new VirtualServerPortUpdateRequest { Port = update } ;
451+ var requestJson = JsonConvert . SerializeObject ( bindRequest ) ;
452+ _logger . LogTrace ( $ "Re-binding templates to VS={ vsName } Port={ port } Protocol={ protocol } => { requestJson } ") ;
453+
454+ apiClient . PutVirtualServerPort ( vsName , port , protocol , requestJson ) ;
455+ }
393456 }
394457 catch ( Exception rollbackEx )
395458 {
0 commit comments