@@ -31,41 +31,35 @@ public AspNetCoreServiceHost<TService> Create<TService>()
31
31
else
32
32
host = new AspNetCoreServiceHost < TService > ( ) ;
33
33
34
- AddGlobalBehaviors ( host ) ;
35
- AddInstanceBehaviors ( host ) ;
36
- //EnsureSingleInstanceContextMode(host);
34
+ host . Initializing += AddGlobalBehaviors ;
35
+ host . Initializing += ( sender , args ) => AddInstanceBehaviors < TService > ( sender , args ) ;
37
36
38
37
return host ;
39
38
}
40
39
41
- private void AddGlobalBehaviors < TService > ( AspNetCoreServiceHost < TService > host )
40
+ private void AddGlobalBehaviors ( object sender , EventArgs args )
42
41
{
43
- host . DescriptionInitialized += ( sender , args ) =>
44
- {
45
- var behaviors = _provider . GetServices < IServiceBehavior > ( ) ;
46
- foreach ( var behavior in behaviors )
47
- host . Description . Behaviors . Add ( behavior ) ;
48
- } ;
42
+ var host = sender as ServiceHost ;
43
+ var behaviors = _provider . GetServices < IServiceBehavior > ( ) . ToArray ( ) ;
44
+ AddBehaviorsToHost ( host , behaviors ) ;
49
45
}
50
46
51
- private void AddInstanceBehaviors < TService > ( AspNetCoreServiceHost < TService > host )
47
+ private void AddInstanceBehaviors < TService > ( object sender , EventArgs args )
52
48
{
53
- host . DescriptionInitialized += ( sender , args ) =>
54
- {
55
- var behaviorProviders = _provider . GetServices < ServiceBehaviorProvider < TService > > ( ) ;
56
- foreach ( var provider in behaviorProviders )
57
- host . Description . Behaviors . Add ( provider . Behavior ) ;
58
- } ;
49
+ var host = sender as ServiceHost ;
50
+ var behaviors = _provider . GetServices < ServiceBehaviorProvider < TService > > ( ) . Select ( p => p . Behavior ) . ToArray ( ) ;
51
+ AddBehaviorsToHost ( host , behaviors ) ;
59
52
}
60
53
61
- private void EnsureSingleInstanceContextMode < TService > ( AspNetCoreServiceHost < TService > host )
54
+ private void AddBehaviorsToHost ( ServiceHost host , IServiceBehavior [ ] behaviors )
62
55
{
63
- host . DescriptionInitialized += ( sender , args ) =>
56
+ foreach ( var behavior in behaviors )
64
57
{
65
- var behaviors = host . Description . Behaviors . OfType < ServiceBehaviorAttribute > ( ) . Where ( b => b . InstanceContextMode != InstanceContextMode . Single ) ;
66
- foreach ( var behavior in behaviors )
67
- behavior . InstanceContextMode = InstanceContextMode . Single ;
68
- } ;
58
+ if ( typeof ( ServiceCredentials ) . IsAssignableFrom ( behavior . GetType ( ) ) )
59
+ host . Description . Behaviors . Remove ( typeof ( ServiceCredentials ) ) ;
60
+
61
+ host . Description . Behaviors . Add ( behavior ) ;
62
+ }
69
63
}
70
64
}
71
65
}
0 commit comments