1 //------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //------------------------------------------------------------
4 namespace System.ServiceModel
6 using System.Collections.Generic;
7 using System.Diagnostics.CodeAnalysis;
10 using System.ServiceModel.Channels;
11 using System.ServiceModel.Description;
12 using System.Workflow.ComponentModel.Compiler;
13 using System.Workflow.Runtime;
15 [Obsolete("The WF3 types are deprecated. Instead, please use the new WF4 types from System.Activities.*")]
16 public class WorkflowServiceHost : ServiceHostBase
18 IList<Type> reflectedContracts;
19 WorkflowDefinitionContext workflowDefinitionContext;
21 public WorkflowServiceHost(Type workflowType, params Uri[] baseAddress) :
22 this(new CompiledWorkflowDefinitionContext(workflowType), baseAddress)
27 public WorkflowServiceHost(string workflowDefinitionPath, params Uri[] baseAddress) :
28 this(new StreamedWorkflowDefinitionContext(workflowDefinitionPath, null, null), baseAddress)
33 public WorkflowServiceHost(string workflowDefinitionPath, string ruleDefinitionPath, params Uri[] baseAddress)
34 : this(new StreamedWorkflowDefinitionContext(workflowDefinitionPath, ruleDefinitionPath, null), baseAddress)
39 public WorkflowServiceHost(string workflowDefinitionPath, string ruleDefinitionPath, ITypeProvider typeProvider, params Uri[] baseAddress)
40 : this(new StreamedWorkflowDefinitionContext(workflowDefinitionPath, ruleDefinitionPath, typeProvider), baseAddress)
46 public WorkflowServiceHost(Stream workflowDefinition, params Uri[] baseAddress) :
47 this(new StreamedWorkflowDefinitionContext(workflowDefinition, null, null), baseAddress)
52 public WorkflowServiceHost(Stream workflowDefinition, Stream ruleDefinition, params Uri[] baseAddress) :
53 this(new StreamedWorkflowDefinitionContext(workflowDefinition, ruleDefinition, null), baseAddress)
58 public WorkflowServiceHost(Stream workflowDefinition, Stream ruleDefinition, ITypeProvider typeProvider, params Uri[] baseAddress)
59 : this(new StreamedWorkflowDefinitionContext(workflowDefinition, ruleDefinition, typeProvider), baseAddress)
64 // Based on prior art from WCF:
65 // ServiceModel.lst:System.ServiceModel.ServiceHost..ctor(System.Object,System.Uri[])
66 // |DoNotCallOverridableMethodsInConstructors
67 // |[....]|By design, don't want to complicate ServiceHost state model
68 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
69 internal WorkflowServiceHost(WorkflowDefinitionContext workflowDefinitionContext, params Uri[] baseAddress)
72 InitializeDescription(workflowDefinitionContext, new UriSchemeKeyedCollection(baseAddress));
75 protected WorkflowServiceHost()
80 public ServiceEndpoint AddServiceEndpoint(Type implementedContract, Binding binding, string address)
84 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("address"));
87 return this.AddServiceEndpoint(implementedContract, binding, new Uri(address, UriKind.RelativeOrAbsolute));
90 public ServiceEndpoint AddServiceEndpoint(Type implementedContract, Binding binding, Uri address)
92 return this.AddServiceEndpoint(implementedContract, binding, address, null);
95 public ServiceEndpoint AddServiceEndpoint(Type implementedContract, Binding binding, string address, Uri listenUri)
99 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("address"));
102 return this.AddServiceEndpoint(implementedContract, binding, new Uri(address, UriKind.RelativeOrAbsolute), listenUri);
105 public ServiceEndpoint AddServiceEndpoint(Type implementedContract, Binding binding, Uri address, Uri listenUri)
107 if (implementedContract == null)
109 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("implementedContract"));
111 if (!implementedContract.IsDefined(typeof(ServiceContractAttribute), false))
113 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR2.GetString(SR2.ServiceContractAttributeNotFound, new object[] { implementedContract.FullName })));
115 if (this.reflectedContracts == null)
117 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR2.GetString(SR2.ReflectedContractsNotInitialized, new object[] { implementedContract.FullName })));
120 if (!reflectedContracts.Contains(implementedContract))
122 if (ServiceMetadataBehavior.IsMetadataImplementedType(implementedContract))
124 if (!this.Description.Behaviors.Contains(
125 typeof(ServiceMetadataBehavior)))
127 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR2.GetString(SR2.MetadataEndpointCannotBeAdded, new object[] { implementedContract.FullName })));
132 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR2.GetString(SR2.ReflectedContractKeyNotFound, new object[] { implementedContract.FullName, this.workflowDefinitionContext.WorkflowName })));
135 ServiceEndpoint endpoint = base.AddServiceEndpoint(ContractDescription.GetContract(implementedContract).ConfigurationName, binding, address);
137 if (listenUri != null)
139 listenUri = base.MakeAbsoluteUri(listenUri, binding);
140 endpoint.ListenUri = listenUri;
146 [SuppressMessage("Microsoft.Design", "CA1021:AvoidOutParameters", MessageId = "0#", Justification = "This is defined by the ServiceHost base class")]
147 protected override ServiceDescription CreateDescription(out IDictionary<string, ContractDescription> implementedContracts)
149 Fx.Assert(this.workflowDefinitionContext != null, "Null Workflow Definition");
150 return new DescriptionCreator(this.workflowDefinitionContext).BuildServiceDescription(out implementedContracts, out this.reflectedContracts);
153 protected override void OnClosing()
155 WorkflowRuntimeBehavior workflowRuntimeBehavior = this.Description.Behaviors.Find<WorkflowRuntimeBehavior>();
157 if (workflowRuntimeBehavior != null)
159 workflowRuntimeBehavior.WorkflowRuntime.StopRuntime();
164 void InitializeDescription(WorkflowDefinitionContext workflowDefinitionContext, UriSchemeKeyedCollection baseAddresses)
166 this.workflowDefinitionContext = workflowDefinitionContext;
167 this.InitializeDescription(baseAddresses);
169 if (!this.Description.Behaviors.Contains(typeof(WorkflowRuntimeBehavior)))
171 this.Description.Behaviors.Add(new WorkflowRuntimeBehavior());