# Subscribe to direct messages with RequestConfirm and correlationKey (.NET)

**URL:** <https://community.solace.com/t/subscribe-to-direct-messages-with-requestconfirm-and-correlationkey-net/1313>\
**Category:** General Discussions\
**Tags:** net\
**Created:** [June 29, 2022, 12:25pm UTC](https://community.solace.com/t/subscribe-to-direct-messages-with-requestconfirm-and-correlationkey-net/1313 "2022-06-29T12:25:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![radekm](https://yyz1.discourse-cdn.com/solace/user_avatar/community.solace.com/radekm/32/901_2.png) [@radekm](https://community.solace.com/u/radekm)\
**Post date:** [June 29, 2022, 12:25pm UTC](https://community.solace.com/t/subscribe-to-direct-messages-with-requestconfirm-and-correlationkey-net/1313/1 "2022-06-29T12:25:39Z")

</div>

Hi,  
I’m using .NET. I would like to subscribe to direct messages.  
I have set session property SubscribeBlocking to false . I plan to use TaskCompletionSource as correlationKey and complete it when handling SubscriptionOk or SubscriptionError .  
Unfortunately I can’t find suitable overload of ISession.Subscribe .  
There are 3 overloads. Only 2 allow me to specify correlationKey . Of those 2 one takes IDispatchTarget but I have topic dispatch turned off. And the last candidate needs IEndpoint and I don’t know where to get IEndpoint for current session ( null doesn’t work).  
Thanks

---

<div class="post-metadata">

**Author:** ![nicholasdgoodman](https://yyz1.discourse-cdn.com/solace/user_avatar/community.solace.com/nicholasdgoodman/32/1724_2.png) [@nicholasdgoodman](https://community.solace.com/u/nicholasdgoodman)\
**Post date:** [June 29, 2022, 6:02pm UTC](https://community.solace.com/t/subscribe-to-direct-messages-with-requestconfirm-and-correlationkey-net/1313/2 "2022-06-29T18:02:36Z")

</div>

At a glance it appears your observations are correct - supplying a subscription correlationKey is only available when using a topic dispatch target or when subscribing directly to an IQueue or IClientName \> instance.  
One workaround would be to enable topic-dispatch, but if you are not interested in using that feature, you can decorate global message handler and pass that in as the topic handler for all subscriptions:  
var sessionProperties = new SessionProperties()

> {  
> // other…  
> SubscribeBlocking = false,  
> TopicDispatch = true﻿  
> };  
> using var context = ContextFactory.Instance.CreateContext(contextProperties, null);  
> using var session = context.CreateSession(sessionProperties, MessageEvent, SessionEvent);  
> session.Connect();  
> using var topic = ContextFactory.Instance.CreateTopic(topicString);  
> // This could be wrapped up into a helper or extension method, .SubscribeAsync(topic)  
> var tsc = new TaskCompletionSource();  
> var target = session.CreateDispatchTarget(topic, (s,e) =\> MessageEvent(session, e));  
> session.Subscribe(target, SubscribeFlag.RequestConfirm, tsc);  
> // Now it is awaitable  
> var result = await tsc.Task;  
> Meanwhile elsewhere:  
> private void SessionEvent(object sender, MessageEventArgs e)  
> {  
> var tsc = e.CorrelationKey as TaskCompletionSource;  
> switch(e.Event)  
> {  
> case SessionEvent.SubscriptionOk:  
> tsc?.SetResult(ReturnCode.SOLCLIENT\_OK);  
> break;  
> case SessionEvent.SubscriptionError:  
> tsc?.SetResult(ReturnCode.SOLCLIENT\_FAIL); // Or .SetException(…)  
> break;  
> }  
> }
