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
|
//----------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------
namespace System.ServiceModel.Activities
{
using System.Diagnostics;
using System.Globalization;
using System.ServiceModel;
using System.ServiceModel.Activities.Description;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.Runtime;
using System.Threading;
using System.Security;
using System.Security.Permissions;
[Fx.Tag.XamlVisible(false)]
public class WorkflowControlEndpoint : ServiceEndpoint
{
static Uri defaultBaseUri;
// Double-checked locking pattern requires volatile for read/write synchronization
static volatile ContractDescription workflowControlServiceBaseContract;
static volatile ContractDescription workflowControlServiceContract;
static object workflowContractDescriptionLock = new object();
public WorkflowControlEndpoint()
: this(WorkflowControlEndpoint.GetDefaultBinding(),
new EndpointAddress(new Uri(WorkflowControlEndpoint.DefaultBaseUri, new Uri(Guid.NewGuid().ToString(), UriKind.Relative))))
{
}
public WorkflowControlEndpoint(Binding binding, EndpointAddress address)
: base(WorkflowControlEndpoint.WorkflowControlServiceContract, binding, address)
{
this.IsSystemEndpoint = true;
}
internal static ContractDescription WorkflowControlServiceBaseContract
{
get
{
if (workflowControlServiceBaseContract == null)
{
lock (workflowContractDescriptionLock)
{
if (workflowControlServiceBaseContract == null)
{
foreach (OperationDescription operation in WorkflowControlServiceContract.Operations)
{
if (operation.DeclaringContract != WorkflowControlServiceContract)
{
workflowControlServiceBaseContract = operation.DeclaringContract;
break;
}
}
}
}
}
return workflowControlServiceBaseContract;
}
}
internal static ContractDescription WorkflowControlServiceContract
{
get
{
if (workflowControlServiceContract == null)
{
lock (workflowContractDescriptionLock)
{
if (workflowControlServiceContract == null)
{
ContractDescription tempControlServiceContract = ContractDescription.GetContract(
typeof(IWorkflowUpdateableInstanceManagement));
tempControlServiceContract.Behaviors.Add(new ServiceMetadataContractBehavior(true));
ApplyOperationBehaviors(tempControlServiceContract);
// For back-compat, need to support existing code which expects the old contract type
tempControlServiceContract.ContractType = typeof(IWorkflowInstanceManagement);
workflowControlServiceContract = tempControlServiceContract;
}
}
}
return workflowControlServiceContract;
}
}
[Fx.Tag.SecurityNote(Critical = "Critical because it retreives the Process Id via Process.Id, which has a link demand for Full Trust.",
Safe = "Safe because it demands FullTrust")]
[SecuritySafeCritical]
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
static void RetrieveProcessIdAndAppDomainId(out int processId, out int appDomainId)
{
processId = Process.GetCurrentProcess().Id;
appDomainId = AppDomain.CurrentDomain.Id;
}
static Uri DefaultBaseUri
{
get
{
if (defaultBaseUri == null)
{
// If we are running in full trust, use the ProcessId and AppDomainId. If partial trust, use a new guid to make the URI unique and avoid
// the usage of ProcessId and AppDomainId. We are doing this for back compat.
if (PartialTrustHelpers.AppDomainFullyTrusted)
{
int processId;
int appDomainId;
RetrieveProcessIdAndAppDomainId(out processId, out appDomainId);
defaultBaseUri = new Uri(string.Format(CultureInfo.InvariantCulture, "net.pipe://localhost/workflowControlServiceEndpoint/{0}/{1}",
processId,
appDomainId));
}
else
{
Uri tempUri = new Uri(string.Format(CultureInfo.InvariantCulture, "net.pipe://localhost/workflowControlServiceEndpoint/{0}",
Guid.NewGuid().ToString()));
// Using Interlocked.CompareExchange because new Guid.NewGuid is not atomic.
Interlocked.CompareExchange(ref defaultBaseUri, tempUri, null);
}
}
return defaultBaseUri;
}
}
static Binding GetDefaultBinding()
{
return new NetNamedPipeBinding(NetNamedPipeSecurityMode.None) { TransactionFlow = true };
}
static void ApplyOperationBehaviors(ContractDescription contractDescription)
{
foreach (OperationDescription operationDescription in contractDescription.Operations)
{
//Except "Abandon" all the operations in this contract are Async.
//All Transacted* operation are Transacted & Async.
switch (operationDescription.Name)
{
case XD2.WorkflowInstanceManagementService.Abandon:
case XD2.WorkflowInstanceManagementService.Cancel:
case XD2.WorkflowInstanceManagementService.Run:
case XD2.WorkflowInstanceManagementService.Suspend:
case XD2.WorkflowInstanceManagementService.Terminate:
case XD2.WorkflowInstanceManagementService.Unsuspend:
case XD2.WorkflowInstanceManagementService.Update:
EnsureDispatch(operationDescription);
break;
case XD2.WorkflowInstanceManagementService.TransactedCancel:
case XD2.WorkflowInstanceManagementService.TransactedRun:
case XD2.WorkflowInstanceManagementService.TransactedSuspend:
case XD2.WorkflowInstanceManagementService.TransactedTerminate:
case XD2.WorkflowInstanceManagementService.TransactedUnsuspend:
case XD2.WorkflowInstanceManagementService.TransactedUpdate:
EnsureDispatch(operationDescription);
EnsureTransactedInvoke(operationDescription);
break;
}
}
}
static void EnsureDispatch(OperationDescription operationDescription)
{
operationDescription.Behaviors.Add(new ControlOperationBehavior(false));
}
static void EnsureTransactedInvoke(OperationDescription operationDescription)
{
OperationBehaviorAttribute operationAttribute = operationDescription.Behaviors.Find<OperationBehaviorAttribute>();
operationAttribute.TransactionScopeRequired = true;
}
}
}
|