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 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
|
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace System.ServiceModel.Description
{
using System.Collections.ObjectModel;
using System.IO;
using System.Runtime;
using System.ServiceModel.Administration;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using System.Workflow.ComponentModel;
using System.Workflow.Runtime;
using System.Workflow.Runtime.Hosting;
using System.Xml;
class WorkflowServiceBehavior : IServiceBehavior, IContextSessionProvider, IWmiInstanceProvider
{
static readonly object[] emptyObjectArray = new object[] { };
AddressFilterMode addressFilterMode;
string configurationName;
bool ignoreExtensionDataObject;
bool includeExceptionDetailInFaults;
int maxItemsInObjectGraph = -1;
string name;
string nameSpace;
bool useSynchronizationContext;
bool validateMustUnderstand;
WorkflowDefinitionContext workflowDefinitionContext;
string workflowDefinitionPath;
string workflowRulesPath;
public WorkflowServiceBehavior(Type workflowType) :
this(new CompiledWorkflowDefinitionContext(workflowType))
{
}
public WorkflowServiceBehavior(string workflowDefinitionPath)
:
this(workflowDefinitionPath, null)
{
}
public WorkflowServiceBehavior(string workflowDefinitionPath, string ruleDefinitionPath)
:
this(new StreamedWorkflowDefinitionContext(workflowDefinitionPath, ruleDefinitionPath, null))
{
this.workflowDefinitionPath = workflowDefinitionPath;
this.workflowRulesPath = ruleDefinitionPath;
}
public WorkflowServiceBehavior(Stream workflowDefinitionStream)
: this(new StreamedWorkflowDefinitionContext(workflowDefinitionStream, null, null))
{
}
public WorkflowServiceBehavior(Stream workflowDefinitionStream, Stream ruleDefinitionStream)
: this(new StreamedWorkflowDefinitionContext(workflowDefinitionStream, ruleDefinitionStream, null))
{
}
internal WorkflowServiceBehavior(WorkflowDefinitionContext workflowDefinitionContext)
{
if (workflowDefinitionContext == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("workflowDefinitionContext");
}
this.workflowDefinitionContext = workflowDefinitionContext;
this.name = this.workflowDefinitionContext.WorkflowName;
this.configurationName = this.workflowDefinitionContext.ConfigurationName;
}
public AddressFilterMode AddressFilterMode
{
get
{
return this.addressFilterMode;
}
set
{
if (!AddressFilterModeHelper.IsDefined(value))
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value"));
}
this.addressFilterMode = value;
}
}
public string ConfigurationName
{
get
{
return this.configurationName;
}
set
{
if (value == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
}
this.configurationName = value;
}
}
public bool IgnoreExtensionDataObject
{
get
{
return this.ignoreExtensionDataObject;
}
set
{
this.ignoreExtensionDataObject = value;
}
}
public bool IncludeExceptionDetailInFaults
{
get
{
return this.includeExceptionDetailInFaults;
}
set
{
this.includeExceptionDetailInFaults = value;
}
}
public int MaxItemsInObjectGraph
{
get
{
return this.maxItemsInObjectGraph;
}
set
{
this.maxItemsInObjectGraph = value;
}
}
public string Name
{
get
{
return this.name;
}
set
{
if (value == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
}
this.name = value;
}
}
public string Namespace
{
get
{
return this.nameSpace;
}
set
{
if (value == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
}
this.nameSpace = value;
}
}
public bool UseSynchronizationContext
{
get
{
return this.useSynchronizationContext;
}
set
{
this.useSynchronizationContext = value;
}
}
public bool ValidateMustUnderstand
{
get
{
return this.validateMustUnderstand;
}
set
{
this.validateMustUnderstand = value;
}
}
public void AddBindingParameters(ServiceDescription description, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection parameters)
{
}
public void ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase serviceHostBase)
{
if (description == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("description");
}
if (serviceHostBase == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("serviceHostBase");
}
if (description.Behaviors == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("description", SR2.GetString(SR2.NoBehaviors));
}
if (description.Endpoints == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("description", SR2.GetString(SR2.NoEndpoints));
}
bool syncContextRegistered = false;
WorkflowRuntimeBehavior workflowRuntimeBehavior = description.Behaviors.Find<WorkflowRuntimeBehavior>();
if (workflowRuntimeBehavior == null)
{
workflowRuntimeBehavior = new WorkflowRuntimeBehavior();
description.Behaviors.Add(workflowRuntimeBehavior);
}
WorkflowPersistenceService persistenceService = workflowRuntimeBehavior.WorkflowRuntime.GetService<WorkflowPersistenceService>();
if (persistenceService != null)
{
bool wasRuntimeStarted = workflowRuntimeBehavior.WorkflowRuntime.IsStarted;
if (wasRuntimeStarted)
{
workflowRuntimeBehavior.WorkflowRuntime.StopRuntime();
}
workflowRuntimeBehavior.WorkflowRuntime.RemoveService(persistenceService);
workflowRuntimeBehavior.WorkflowRuntime.AddService(new SkipUnloadOnFirstIdleWorkflowPersistenceService(persistenceService));
if (wasRuntimeStarted)
{
workflowRuntimeBehavior.WorkflowRuntime.StartRuntime();
}
}
this.workflowDefinitionContext.Register(workflowRuntimeBehavior.WorkflowRuntime, workflowRuntimeBehavior.ValidateOnCreate);
WorkflowInstanceContextProvider instanceContextProvider = new WorkflowInstanceContextProvider(
serviceHostBase,
false,
this.workflowDefinitionContext
);
WorkflowInstanceContextProvider singleCallInstanceContextProvider = null;
IInstanceProvider instanceProvider = new WorkflowInstanceProvider(instanceContextProvider);
ServiceDebugBehavior serviceDebugBehavior = description.Behaviors.Find<ServiceDebugBehavior>();
bool includeExceptionDetailsInFaults = this.IncludeExceptionDetailInFaults;
if (serviceDebugBehavior != null)
{
includeExceptionDetailsInFaults |= serviceDebugBehavior.IncludeExceptionDetailInFaults;
}
IErrorHandler workflowOperationErrorHandler = new WorkflowOperationErrorHandler(includeExceptionDetailsInFaults);
foreach (ChannelDispatcherBase channelDispatcherBase in serviceHostBase.ChannelDispatchers)
{
ChannelDispatcher channelDispatcher = channelDispatcherBase as ChannelDispatcher;
if (channelDispatcher != null && channelDispatcher.HasApplicationEndpoints())
{
channelDispatcher.IncludeExceptionDetailInFaults = includeExceptionDetailsInFaults;
channelDispatcher.ErrorHandlers.Add(workflowOperationErrorHandler);
foreach (EndpointDispatcher endPointDispatcher in channelDispatcher.Endpoints)
{
if (endPointDispatcher.IsSystemEndpoint)
{
continue;
}
ServiceEndpoint serviceEndPoint = description.Endpoints.Find(new XmlQualifiedName(endPointDispatcher.ContractName, endPointDispatcher.ContractNamespace));
if (serviceEndPoint != null)
{
DispatchRuntime dispatchRuntime = endPointDispatcher.DispatchRuntime;
dispatchRuntime.AutomaticInputSessionShutdown = true;
dispatchRuntime.ConcurrencyMode = ConcurrencyMode.Single;
dispatchRuntime.ValidateMustUnderstand = this.ValidateMustUnderstand;
if (!this.UseSynchronizationContext)
{
dispatchRuntime.SynchronizationContext = null;
}
else if (!syncContextRegistered)
{
SynchronizationContextWorkflowSchedulerService syncSchedulerService = workflowRuntimeBehavior.WorkflowRuntime.GetService<SynchronizationContextWorkflowSchedulerService>();
Fx.Assert(syncSchedulerService != null, "Wrong Synchronization Context Set");
syncSchedulerService.SetSynchronizationContext(dispatchRuntime.SynchronizationContext);
syncContextRegistered = true;
}
if (!endPointDispatcher.AddressFilterSetExplicit)
{
EndpointAddress endPointAddress = endPointDispatcher.OriginalAddress;
if ((endPointAddress == null) || (this.AddressFilterMode == AddressFilterMode.Any))
{
endPointDispatcher.AddressFilter = new MatchAllMessageFilter();
}
else if (this.AddressFilterMode == AddressFilterMode.Prefix)
{
endPointDispatcher.AddressFilter = new PrefixEndpointAddressMessageFilter(endPointAddress);
}
else if (this.AddressFilterMode == AddressFilterMode.Exact)
{
endPointDispatcher.AddressFilter = new EndpointAddressMessageFilter(endPointAddress);
}
}
if (serviceEndPoint.Contract.SessionMode != SessionMode.NotAllowed)
{
endPointDispatcher.DispatchRuntime.InstanceContextProvider = instanceContextProvider;
}
else
{
if (singleCallInstanceContextProvider == null)
{
singleCallInstanceContextProvider = new WorkflowInstanceContextProvider(
serviceHostBase,
true,
this.workflowDefinitionContext);
}
endPointDispatcher.DispatchRuntime.InstanceContextProvider = singleCallInstanceContextProvider;
}
endPointDispatcher.DispatchRuntime.MessageInspectors.Add(new DurableMessageDispatchInspector(serviceEndPoint.Contract.SessionMode));
endPointDispatcher.DispatchRuntime.InstanceProvider = instanceProvider;
SetContractFilterToIncludeAllOperations(endPointDispatcher, serviceEndPoint.Contract);
}
}
}
}
DataContractSerializerServiceBehavior.ApplySerializationSettings(description, this.ignoreExtensionDataObject, this.maxItemsInObjectGraph);
}
void IWmiInstanceProvider.FillInstance(IWmiInstance wmiInstance)
{
wmiInstance.SetProperty("AddressFilterMode", this.AddressFilterMode.ToString());
wmiInstance.SetProperty("ConfigurationName", this.ConfigurationName);
wmiInstance.SetProperty("IgnoreExtensionDataObject", this.IgnoreExtensionDataObject);
wmiInstance.SetProperty("IncludeExceptionDetailInFaults", this.IncludeExceptionDetailInFaults);
wmiInstance.SetProperty("MaxItemsInObjectGraph", this.MaxItemsInObjectGraph);
wmiInstance.SetProperty("Name", this.Name);
wmiInstance.SetProperty("Namespace", this.Namespace);
wmiInstance.SetProperty("UseSynchronizationContext", this.UseSynchronizationContext);
wmiInstance.SetProperty("ValidateMustUnderstand", this.ValidateMustUnderstand);
wmiInstance.SetProperty("WorkflowType", this.workflowDefinitionContext.WorkflowName);
wmiInstance.SetProperty("WorkflowDefinitionPath", this.workflowDefinitionPath);
wmiInstance.SetProperty("WorkflowRulesPath", this.workflowRulesPath);
}
string IWmiInstanceProvider.GetInstanceType()
{
return "WorkflowServiceBehavior";
}
public void Validate(ServiceDescription description, ServiceHostBase serviceHostBase)
{
ContextBindingElement.ValidateContextBindingElementOnAllEndpointsWithSessionfulContract(description, this);
}
internal static void SetContractFilterToIncludeAllOperations(EndpointDispatcher dispatcher, ContractDescription contract)
{
if (dispatcher == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("dispatcher");
}
if (contract == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("contract");
}
if (contract.SessionMode == SessionMode.Required)
{
EndpointFilterProvider provider = new EndpointFilterProvider();
foreach (OperationDescription operation in contract.Operations)
{
if (!operation.IsServerInitiated())
{
provider.InitiatingActions.Add(operation.Messages[0].Action);
}
}
int priority;
dispatcher.ContractFilter = provider.CreateFilter(out priority);
dispatcher.FilterPriority = priority;
}
}
class SkipUnloadOnFirstIdleWorkflowPersistenceService : WorkflowPersistenceService
{
WorkflowPersistenceService inner;
public SkipUnloadOnFirstIdleWorkflowPersistenceService(WorkflowPersistenceService inner)
{
if (inner == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("inner");
}
this.inner = inner;
}
protected internal override Activity LoadCompletedContextActivity(Guid scopeId, Activity outerActivity)
{
return this.inner.LoadCompletedContextActivity(scopeId, outerActivity);
}
protected internal override Activity LoadWorkflowInstanceState(Guid instanceId)
{
return this.inner.LoadWorkflowInstanceState(instanceId);
}
protected internal override void SaveCompletedContextActivity(Activity activity)
{
this.inner.SaveCompletedContextActivity(activity);
}
protected internal override void SaveWorkflowInstanceState(Activity rootActivity, bool unlock)
{
this.inner.SaveWorkflowInstanceState(rootActivity, unlock);
}
protected internal override void Start()
{
this.inner.SetRuntime(this.Runtime);
this.inner.Start();
}
protected internal override void Stop()
{
this.inner.Stop();
this.inner.SetRuntime(null);
}
protected internal override bool UnloadOnIdle(Activity activity)
{
if (WorkflowDispatchContext.Current != null && WorkflowDispatchContext.Current.IsWorkflowStarting)
{
return false;
}
else
{
return this.inner.UnloadOnIdle(activity);
}
}
protected internal override void UnlockWorkflowInstanceState(Activity rootActivity)
{
this.inner.UnlockWorkflowInstanceState(rootActivity);
}
}
}
}
|