1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
#pragma warning disable 1634, 1691
using System;
using System.Diagnostics;
using System.Collections;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.Remoting.Messaging;
using System.Workflow.Runtime;
using System.Security.Principal;
using System.Threading;
using System.Globalization;
namespace System.Workflow.Activities
{
internal interface IMethodResponseMessage
{
void SendResponse(ICollection outArgs);
void SendException(Exception exception);
Exception Exception { get; }
ICollection OutArgs { get; }
}
[Serializable]
internal sealed class MethodMessage : IMethodMessage, IMethodResponseMessage
{
[NonSerialized]
Type interfaceType;
[NonSerialized]
string methodName;
[NonSerialized]
object[] args;
[NonSerialized]
ManualResetEvent returnValueSignalEvent;
object[] clonedArgs;
LogicalCallContext callContext;
ICollection outArgs;
Exception exception;
[NonSerialized]
bool responseSet = false;
Guid callbackCookie;
[NonSerialized]
MethodMessage previousMessage = null;
static Dictionary<Guid, MethodMessage> staticMethodMessageMap = new Dictionary<Guid, MethodMessage>();
static Object syncRoot = new Object();
internal MethodMessage(Type interfaceType, string methodName,
object[] args, String identity) :
this(interfaceType, methodName, args, identity, false)
{
}
internal MethodMessage(Type interfaceType, string methodName,
object[] args, String identity, bool responseRequired)
{
this.interfaceType = interfaceType;
this.methodName = methodName;
this.args = args;
callContext = GetLogicalCallContext();
if (responseRequired)
returnValueSignalEvent = new ManualResetEvent(false);
PopulateIdentity(callContext, identity);
Clone();
}
[OnSerializing]
void OnSerializing(StreamingContext context)
{
if (returnValueSignalEvent != null && !responseSet)
{
callbackCookie = Guid.NewGuid();
lock (syncRoot)
{
staticMethodMessageMap.Add(callbackCookie, previousMessage ?? this);
}
}
}
[OnDeserialized]
void OnDeserialized(StreamingContext context)
{
if (callbackCookie != Guid.Empty)
{
lock (syncRoot)
{
if (staticMethodMessageMap.TryGetValue(callbackCookie, out previousMessage))
staticMethodMessageMap.Remove(callbackCookie);
}
if (previousMessage != null)
{
this.responseSet = previousMessage.responseSet;
this.returnValueSignalEvent = previousMessage.returnValueSignalEvent;
}
}
callbackCookie = Guid.Empty;
}
string IMethodMessage.GetArgName(int index)
{
throw new NotImplementedException();
}
object IMethodMessage.GetArg(int argNum)
{
return this.clonedArgs[argNum];
}
string IMethodMessage.Uri
{
#pragma warning disable 56503
// not implemented
get { throw new NotImplementedException(); }
#pragma warning restore 56503
}
string IMethodMessage.MethodName
{
get { return this.methodName; }
}
string IMethodMessage.TypeName
{
get
{
return (this.interfaceType.ToString());
}
}
object IMethodMessage.MethodSignature
{
#pragma warning disable 56503
get { throw new NotImplementedException(); }
#pragma warning restore 56503
}
object[] IMethodMessage.Args
{
get
{
return this.clonedArgs;
}
}
object Clone()
{
object[] clones = new object[this.args.Length];
for (int i = 0; i < this.args.Length; i++)
{
clones[i] = Clone(this.args[i]);
}
this.clonedArgs = clones;
return clones;
}
object Clone(object source)
{
if (source == null || source.GetType().IsValueType)
return source;
ICloneable clone = source as ICloneable;
if (clone != null)
return clone.Clone();
BinaryFormatter formatter = new BinaryFormatter();
System.IO.MemoryStream stream = new System.IO.MemoryStream(1024);
try
{
formatter.Serialize(stream, source);
}
catch (SerializationException e)
{
throw new InvalidOperationException(SR.GetString(SR.Error_EventArgumentSerializationException), e);
}
stream.Position = 0;
object cloned = formatter.Deserialize(stream);
return cloned;
}
int IMethodMessage.ArgCount
{
get { return this.clonedArgs.Length; }
}
bool IMethodMessage.HasVarArgs
{
#pragma warning disable 56503
get { throw new NotImplementedException(); }
#pragma warning restore 56503
}
LogicalCallContext IMethodMessage.LogicalCallContext
{
get { return callContext; }
}
MethodBase IMethodMessage.MethodBase
{
#pragma warning disable 56503
get { throw new NotImplementedException(); }
#pragma warning restore 56503
}
IDictionary System.Runtime.Remoting.Messaging.IMessage.Properties
{
#pragma warning disable 56503
get { throw new NotImplementedException(); }
#pragma warning restore 56503
}
void PopulateIdentity(LogicalCallContext callContext, String identity)
{
callContext.SetData(IdentityContextData.IdentityContext, new IdentityContextData(identity));
}
static LogicalCallContext singletonCallContext;
static Object syncObject = new Object();
static LogicalCallContext GetLogicalCallContext()
{
lock (syncObject)
{
if (singletonCallContext == null)
{
CallContextProxy contextProxy = new CallContextProxy(typeof(IDisposable));
IDisposable disposable = (IDisposable)contextProxy.GetTransparentProxy();
disposable.Dispose();
singletonCallContext = contextProxy.CallContext;
}
return singletonCallContext.Clone() as LogicalCallContext;
}
}
#region IMethodResponseMessage implementation
internal IMethodResponseMessage WaitForResponseMessage()
{
// todo wait for certain timeout
this.returnValueSignalEvent.WaitOne();
this.returnValueSignalEvent = null;
return this;
}
public void SendResponse(ICollection outArgs)
{
if (this.returnValueSignalEvent == null)
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, SR.GetString(SR.Error_WorkflowInstanceDehydratedBeforeSendingResponse)));
if (!this.responseSet)
{
this.OutArgs = outArgs;
this.returnValueSignalEvent.Set();
this.responseSet = true;
}
}
public void SendException(Exception exception)
{
if (this.returnValueSignalEvent == null)
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, SR.GetString(SR.Error_WorkflowInstanceDehydratedBeforeSendingResponse)));
if (!this.responseSet)
{
this.Exception = exception;
this.returnValueSignalEvent.Set();
this.responseSet = true;
}
}
public Exception Exception
{
get
{
return this.exception;
}
private set
{
if (previousMessage != null)
previousMessage.Exception = value;
this.exception = value;
}
}
public ICollection OutArgs
{
get
{
return this.outArgs;
}
private set
{
if (previousMessage != null)
previousMessage.OutArgs = value;
this.outArgs = value;
}
}
#endregion
private sealed class CallContextProxy : System.Runtime.Remoting.Proxies.RealProxy
{
LogicalCallContext callContext;
internal LogicalCallContext CallContext
{
get
{
return callContext;
}
}
internal CallContextProxy(Type proxiedType)
: base(proxiedType)
{
}
public override System.Runtime.Remoting.Messaging.IMessage Invoke(System.Runtime.Remoting.Messaging.IMessage msg)
{
IMethodCallMessage methodCallMessage = msg as IMethodCallMessage;
this.callContext = methodCallMessage.LogicalCallContext.Clone() as LogicalCallContext;
return new ReturnMessage(null, null, 0, methodCallMessage.LogicalCallContext, methodCallMessage);
}
}
}
}
|