internal static void ApplyDispatchBehavior(Type type, string threadName, DispatchRuntime dispatch) { Debug.Assert(dispatch.SynchronizationContext == null); if (m_Contexts.ContainsKey(type) == false) { m_Contexts[type] = new AffinitySynchronizer(threadName); } dispatch.SynchronizationContext = m_Contexts[type]; }
/// <summary> /// Can only call before openning the host /// </summary> public void SetThreadAffinity(string threadName) { if (State == CommunicationState.Opened) { throw new InvalidOperationException("Host is already opened"); } Debug.Assert(m_AffinitySynchronizer == null);//Can call only once m_AffinitySynchronizer = new AffinitySynchronizer(threadName); SynchronizationContext.SetSynchronizationContext(m_AffinitySynchronizer); }
/// <summary> /// Can only call before openning the host /// </summary> public static void SetThreadAffinity(this ServiceHost host,string threadName) { if(host.State == CommunicationState.Opened) { throw new InvalidOperationException("Host is already opened"); } Debug.Assert(SynchronizationContext.Current == null);//Can call only once AffinitySynchronizer affinitySynchronizer = new AffinitySynchronizer(threadName); SynchronizationContext.SetSynchronizationContext(affinitySynchronizer); host.Closing += delegate { using(affinitySynchronizer) {} }; }
/// <summary> /// Can only call before openning the host /// </summary> public static void SetThreadAffinity(this ServiceHost host, string threadName) { if (host.State == CommunicationState.Opened) { throw new InvalidOperationException("Host is already opened"); } Debug.Assert(SynchronizationContext.Current == null);//Can call only once AffinitySynchronizer affinitySynchronizer = new AffinitySynchronizer(threadName); SynchronizationContext.SetSynchronizationContext(affinitySynchronizer); host.Closing += delegate { using (affinitySynchronizer) {} }; }