@@ -37,92 +37,97 @@ private RequestQueue(string? requestQueueName, RequestQueueMode mode, GenericSec
3737 SECURITY_ATTRIBUTES ? securityAttributes = null ;
3838 nint ? pSecurityDescriptor = null ;
3939
40- if ( _mode == RequestQueueMode . Attach )
40+ try
4141 {
42- flags = PInvoke . HTTP_CREATE_REQUEST_QUEUE_FLAG_OPEN_EXISTING ;
43- Created = false ;
44- if ( receiver )
42+ if ( _mode == RequestQueueMode . Attach )
4543 {
46- flags |= PInvoke . HTTP_CREATE_REQUEST_QUEUE_FLAG_DELEGATION ;
44+ flags = PInvoke . HTTP_CREATE_REQUEST_QUEUE_FLAG_OPEN_EXISTING ;
45+ Created = false ;
46+ if ( receiver )
47+ {
48+ flags |= PInvoke . HTTP_CREATE_REQUEST_QUEUE_FLAG_DELEGATION ;
49+ }
4750 }
48- }
49- else if ( securityDescriptor is not null ) // Create or CreateOrAttach
50- {
51- // Convert the security descriptor to a byte array
52- byte [ ] securityDescriptorBytes = new byte [ securityDescriptor . BinaryLength ] ;
53- securityDescriptor . GetBinaryForm ( securityDescriptorBytes , 0 ) ;
51+ else if ( securityDescriptor is not null ) // Create or CreateOrAttach
52+ {
53+ // Convert the security descriptor to a byte array
54+ byte [ ] securityDescriptorBytes = new byte [ securityDescriptor . BinaryLength ] ;
55+ securityDescriptor . GetBinaryForm ( securityDescriptorBytes , 0 ) ;
5456
55- // Allocate native memory for the security descriptor
56- pSecurityDescriptor = Marshal . AllocHGlobal ( securityDescriptorBytes . Length ) ;
57- Marshal . Copy ( securityDescriptorBytes , 0 , pSecurityDescriptor . Value , securityDescriptorBytes . Length ) ;
57+ // Allocate native memory for the security descriptor
58+ pSecurityDescriptor = Marshal . AllocHGlobal ( securityDescriptorBytes . Length ) ;
59+ Marshal . Copy ( securityDescriptorBytes , 0 , pSecurityDescriptor . Value , securityDescriptorBytes . Length ) ;
5860
59- unsafe
60- {
61- securityAttributes = new SECURITY_ATTRIBUTES
61+ unsafe
6262 {
63- nLength = ( uint ) Marshal . SizeOf < SECURITY_ATTRIBUTES > ( ) ,
64- lpSecurityDescriptor = pSecurityDescriptor . Value . ToPointer ( ) ,
65- bInheritHandle = false
66- } ;
63+ securityAttributes = new SECURITY_ATTRIBUTES
64+ {
65+ nLength = ( uint ) Marshal . SizeOf < SECURITY_ATTRIBUTES > ( ) ,
66+ lpSecurityDescriptor = pSecurityDescriptor . Value . ToPointer ( ) ,
67+ bInheritHandle = false
68+ } ;
69+ }
6770 }
68- }
69-
70- var statusCode = PInvoke . HttpCreateRequestQueue (
71- HttpApi . Version ,
72- requestQueueName ,
73- securityAttributes ,
74- flags ,
75- out var requestQueueHandle ) ;
7671
77- if ( _mode == RequestQueueMode . CreateOrAttach && statusCode == ErrorCodes . ERROR_ALREADY_EXISTS )
78- {
79- // Tried to create, but it already exists so attach to it instead.
80- Created = false ;
81- flags = PInvoke . HTTP_CREATE_REQUEST_QUEUE_FLAG_OPEN_EXISTING ;
82- statusCode = PInvoke . HttpCreateRequestQueue (
72+ var statusCode = PInvoke . HttpCreateRequestQueue (
8373 HttpApi . Version ,
8474 requestQueueName ,
85- SecurityAttributes : default , // Attaching should not pass any security attributes
75+ securityAttributes ,
8676 flags ,
87- out requestQueueHandle ) ;
88- }
77+ out var requestQueueHandle ) ;
8978
90- if ( pSecurityDescriptor is not null )
91- {
92- // Free the allocated memory for the security descriptor
93- Marshal . FreeHGlobal ( pSecurityDescriptor . Value ) ;
94- }
79+ if ( _mode == RequestQueueMode . CreateOrAttach && statusCode == ErrorCodes . ERROR_ALREADY_EXISTS )
80+ {
81+ // Tried to create, but it already exists so attach to it instead.
82+ Created = false ;
83+ flags = PInvoke . HTTP_CREATE_REQUEST_QUEUE_FLAG_OPEN_EXISTING ;
84+ statusCode = PInvoke . HttpCreateRequestQueue (
85+ HttpApi . Version ,
86+ requestQueueName ,
87+ SecurityAttributes : default , // Attaching should not pass any security attributes
88+ flags ,
89+ out requestQueueHandle ) ;
90+ }
9591
96- if ( ( flags & PInvoke . HTTP_CREATE_REQUEST_QUEUE_FLAG_OPEN_EXISTING ) != 0 && statusCode == ErrorCodes . ERROR_FILE_NOT_FOUND )
97- {
98- throw new HttpSysException ( ( int ) statusCode , $ "Failed to attach to the given request queue '{ requestQueueName } ', the queue could not be found.") ;
99- }
100- else if ( statusCode == ErrorCodes . ERROR_INVALID_NAME )
101- {
102- throw new HttpSysException ( ( int ) statusCode , $ "The given request queue name '{ requestQueueName } ' is invalid.") ;
103- }
104- else if ( statusCode != ErrorCodes . ERROR_SUCCESS )
105- {
106- throw new HttpSysException ( ( int ) statusCode ) ;
107- }
92+ if ( ( flags & PInvoke . HTTP_CREATE_REQUEST_QUEUE_FLAG_OPEN_EXISTING ) != 0 && statusCode == ErrorCodes . ERROR_FILE_NOT_FOUND )
93+ {
94+ throw new HttpSysException ( ( int ) statusCode , $ "Failed to attach to the given request queue '{ requestQueueName } ', the queue could not be found.") ;
95+ }
96+ else if ( statusCode == ErrorCodes . ERROR_INVALID_NAME )
97+ {
98+ throw new HttpSysException ( ( int ) statusCode , $ "The given request queue name '{ requestQueueName } ' is invalid.") ;
99+ }
100+ else if ( statusCode != ErrorCodes . ERROR_SUCCESS )
101+ {
102+ throw new HttpSysException ( ( int ) statusCode ) ;
103+ }
108104
109- // Disabling callbacks when IO operation completes synchronously (returns ErrorCodes.ERROR_SUCCESS)
110- if ( HttpSysListener . SkipIOCPCallbackOnSuccess &&
111- ! PInvoke . SetFileCompletionNotificationModes (
112- requestQueueHandle ,
113- ( byte ) ( PInvoke . FILE_SKIP_COMPLETION_PORT_ON_SUCCESS |
114- PInvoke . FILE_SKIP_SET_EVENT_ON_HANDLE ) ) )
115- {
116- requestQueueHandle . Dispose ( ) ;
117- throw new HttpSysException ( Marshal . GetLastWin32Error ( ) ) ;
118- }
105+ // Disabling callbacks when IO operation completes synchronously (returns ErrorCodes.ERROR_SUCCESS)
106+ if ( HttpSysListener . SkipIOCPCallbackOnSuccess &&
107+ ! PInvoke . SetFileCompletionNotificationModes (
108+ requestQueueHandle ,
109+ ( byte ) ( PInvoke . FILE_SKIP_COMPLETION_PORT_ON_SUCCESS |
110+ PInvoke . FILE_SKIP_SET_EVENT_ON_HANDLE ) ) )
111+ {
112+ requestQueueHandle . Dispose ( ) ;
113+ throw new HttpSysException ( Marshal . GetLastWin32Error ( ) ) ;
114+ }
119115
120- Handle = requestQueueHandle ;
121- BoundHandle = ThreadPoolBoundHandle . BindHandle ( Handle ) ;
116+ Handle = requestQueueHandle ;
117+ BoundHandle = ThreadPoolBoundHandle . BindHandle ( Handle ) ;
122118
123- if ( ! Created )
119+ if ( ! Created )
120+ {
121+ Log . AttachedToQueue ( _logger , requestQueueName ) ;
122+ }
123+ }
124+ finally
124125 {
125- Log . AttachedToQueue ( _logger , requestQueueName ) ;
126+ if ( pSecurityDescriptor is not null )
127+ {
128+ // Free the allocated memory for the security descriptor
129+ Marshal . FreeHGlobal ( pSecurityDescriptor . Value ) ;
130+ }
126131 }
127132 }
128133
0 commit comments