Update Reference Sources to .NET Framework 4.6.1
[mono.git] / mcs / class / referencesource / System.WorkflowServices / System / ServiceModel / WorkflowServiceHost.cs
1 //------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //------------------------------------------------------------
4 namespace System.ServiceModel
5 {
6     using System.Collections.Generic;
7     using System.Diagnostics.CodeAnalysis;
8     using System.IO;
9     using System.Runtime;
10     using System.ServiceModel.Channels;
11     using System.ServiceModel.Description;
12     using System.Workflow.ComponentModel.Compiler;
13     using System.Workflow.Runtime;
14
15     [Obsolete("The WF3 types are deprecated.  Instead, please use the new WF4 types from System.Activities.*")]
16     public class WorkflowServiceHost : ServiceHostBase
17     {
18         IList<Type> reflectedContracts;
19         WorkflowDefinitionContext workflowDefinitionContext;
20
21         public WorkflowServiceHost(Type workflowType, params Uri[] baseAddress) :
22             this(new CompiledWorkflowDefinitionContext(workflowType), baseAddress)
23         {
24
25         }
26
27         public WorkflowServiceHost(string workflowDefinitionPath, params Uri[] baseAddress) :
28             this(new StreamedWorkflowDefinitionContext(workflowDefinitionPath, null, null), baseAddress)
29         {
30
31         }
32
33         public WorkflowServiceHost(string workflowDefinitionPath, string ruleDefinitionPath, params Uri[] baseAddress)
34             : this(new StreamedWorkflowDefinitionContext(workflowDefinitionPath, ruleDefinitionPath, null), baseAddress)
35         {
36
37         }
38
39         public WorkflowServiceHost(string workflowDefinitionPath, string ruleDefinitionPath, ITypeProvider typeProvider, params Uri[] baseAddress)
40             : this(new StreamedWorkflowDefinitionContext(workflowDefinitionPath, ruleDefinitionPath, typeProvider), baseAddress)
41         {
42
43         }
44
45
46         public WorkflowServiceHost(Stream workflowDefinition, params Uri[] baseAddress) :
47             this(new StreamedWorkflowDefinitionContext(workflowDefinition, null, null), baseAddress)
48         {
49
50         }
51
52         public WorkflowServiceHost(Stream workflowDefinition, Stream ruleDefinition, params Uri[] baseAddress) :
53             this(new StreamedWorkflowDefinitionContext(workflowDefinition, ruleDefinition, null), baseAddress)
54         {
55
56         }
57
58         public WorkflowServiceHost(Stream workflowDefinition, Stream ruleDefinition, ITypeProvider typeProvider, params Uri[] baseAddress)
59             : this(new StreamedWorkflowDefinitionContext(workflowDefinition, ruleDefinition, typeProvider), baseAddress)
60         {
61
62         }
63
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)
70             : base()
71         {
72             InitializeDescription(workflowDefinitionContext, new UriSchemeKeyedCollection(baseAddress));
73         }
74
75         protected WorkflowServiceHost()
76         {
77
78         }
79
80         public ServiceEndpoint AddServiceEndpoint(Type implementedContract, Binding binding, string address)
81         {
82             if (address == null)
83             {
84                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("address"));
85             }
86
87             return this.AddServiceEndpoint(implementedContract, binding, new Uri(address, UriKind.RelativeOrAbsolute));
88         }
89
90         public ServiceEndpoint AddServiceEndpoint(Type implementedContract, Binding binding, Uri address)
91         {
92             return this.AddServiceEndpoint(implementedContract, binding, address, null);
93         }
94
95         public ServiceEndpoint AddServiceEndpoint(Type implementedContract, Binding binding, string address, Uri listenUri)
96         {
97             if (address == null)
98             {
99                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("address"));
100             }
101
102             return this.AddServiceEndpoint(implementedContract, binding, new Uri(address, UriKind.RelativeOrAbsolute), listenUri);
103         }
104
105         public ServiceEndpoint AddServiceEndpoint(Type implementedContract, Binding binding, Uri address, Uri listenUri)
106         {
107             if (implementedContract == null)
108             {
109                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("implementedContract"));
110             }
111             if (!implementedContract.IsDefined(typeof(ServiceContractAttribute), false))
112             {
113                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR2.GetString(SR2.ServiceContractAttributeNotFound, new object[] { implementedContract.FullName })));
114             }
115             if (this.reflectedContracts == null)
116             {
117                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR2.GetString(SR2.ReflectedContractsNotInitialized, new object[] { implementedContract.FullName })));
118             }
119
120             if (!reflectedContracts.Contains(implementedContract))
121             {
122                 if (ServiceMetadataBehavior.IsMetadataImplementedType(implementedContract))
123                 {
124                     if (!this.Description.Behaviors.Contains(
125                         typeof(ServiceMetadataBehavior)))
126                     {
127                         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR2.GetString(SR2.MetadataEndpointCannotBeAdded, new object[] { implementedContract.FullName })));
128                     }
129                 }
130                 else
131                 {
132                     throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR2.GetString(SR2.ReflectedContractKeyNotFound, new object[] { implementedContract.FullName, this.workflowDefinitionContext.WorkflowName })));
133                 }
134             }
135             ServiceEndpoint endpoint = base.AddServiceEndpoint(ContractDescription.GetContract(implementedContract).ConfigurationName, binding, address);
136
137             if (listenUri != null)
138             {
139                 listenUri = base.MakeAbsoluteUri(listenUri, binding);
140                 endpoint.ListenUri = listenUri;
141             }
142
143             return endpoint;
144         }
145
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)
148         {
149             Fx.Assert(this.workflowDefinitionContext != null, "Null Workflow Definition");
150             return new DescriptionCreator(this.workflowDefinitionContext).BuildServiceDescription(out implementedContracts, out this.reflectedContracts);
151         }
152
153         protected override void OnClosing()
154         {
155             WorkflowRuntimeBehavior workflowRuntimeBehavior = this.Description.Behaviors.Find<WorkflowRuntimeBehavior>();
156
157             if (workflowRuntimeBehavior != null)
158             {
159                 workflowRuntimeBehavior.WorkflowRuntime.StopRuntime();
160             }
161             base.OnClosing();
162         }
163
164         void InitializeDescription(WorkflowDefinitionContext workflowDefinitionContext, UriSchemeKeyedCollection baseAddresses)
165         {
166             this.workflowDefinitionContext = workflowDefinitionContext;
167             this.InitializeDescription(baseAddresses);
168
169             if (!this.Description.Behaviors.Contains(typeof(WorkflowRuntimeBehavior)))
170             {
171                 this.Description.Behaviors.Add(new WorkflowRuntimeBehavior());
172             }
173         }
174     }
175 }