Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / WCF / System.ServiceModel.Activation / System / ServiceModel / Activities / Activation / WorkflowServiceHostFactory.cs / 1305376 / WorkflowServiceHostFactory.cs
//------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------- namespace System.ServiceModel.Activities.Activation { using System.Activities; using System.Activities.XamlIntegration; using System.IO; using System.Globalization; using System.Reflection; using System.Runtime; using System.Runtime.DurableInstancing; using System.Security; using System.Security.Permissions; using System.ServiceModel.Activation; using System.Web; using System.Web.Compilation; using System.Web.Hosting; using System.Xaml; using System.Xml; using System.Xml.Linq; public class WorkflowServiceHostFactory : ServiceHostFactoryBase { public override ServiceHostBase CreateServiceHost(string constructorString, Uri[] baseAddresses) { WorkflowServiceHost serviceHost = null; if (string.IsNullOrEmpty(constructorString)) { throw FxTrace.Exception.AsError(new InvalidOperationException(SR.WorkflowServiceHostFactoryConstructorStringNotProvided)); } if (baseAddresses == null) { throw FxTrace.Exception.ArgumentNull("baseAddresses"); } if (baseAddresses.Length == 0) { throw FxTrace.Exception.AsError(new InvalidOperationException(SR.BaseAddressesNotProvided)); } if (!HostingEnvironment.IsHosted) { throw FxTrace.Exception.AsError(new InvalidOperationException(SR.Hosting_ProcessNotExecutingUnderHostedContext("WorkflowServiceHostFactory.CreateServiceHost"))); } // We expect most users will use .xamlx file instead of precompiled assembly // Samples of xamlVirtualPath under all scenarios // constructorString XamlFileBaseLocation xamlVirtualPath // 1. Xamlx direct ~/sub/a.xamlx ~/sub/ ~/sub/a.xamlx // 2. CBA with precompiled servicetypeinfo ~/sub/servicetypeinfo ~/sub/servicetypeinfo * no file will be found // 3. CBA with xamlx sub/a.xamlx ~/ ~/sub/a.xamlx // 4. Svc with precompiled servicetypeinfo ~/sub/servicetypeinfo ~/sub/servicetypeinfo * no file will be found // 5. Svc with Xamlx ../a.xamlx ~/sub/ ~/a.xamlx string xamlVirtualPath = VirtualPathUtility.Combine(AspNetEnvironment.Current.XamlFileBaseLocation, constructorString); Stream activityStream; string compiledCustomString; if (GetServiceFileStreamOrCompiledCustomString(xamlVirtualPath, baseAddresses, out activityStream, out compiledCustomString)) { object serviceObject; using (activityStream) { // BuildManager.GetReferencedAssemblies(); //XmlnsMappingHelper.EnsureMappingPassed(); XamlXmlReaderSettings xamlXmlReaderSettings = new XamlXmlReaderSettings(); xamlXmlReaderSettings.ProvideLineInfo = true; XamlReader wrappedReader = ActivityXamlServices.CreateReader(new XamlXmlReader(XmlReader.Create(activityStream), xamlXmlReaderSettings)); if (TD.XamlServicesLoadStartIsEnabled()) { TD.XamlServicesLoadStart(); } serviceObject = XamlServices.Load(wrappedReader); if (TD.XamlServicesLoadStopIsEnabled()) { TD.XamlServicesLoadStop(); } } WorkflowService service = null; if (serviceObject is Activity) { service = new WorkflowService { Body = (Activity)serviceObject }; } else if (serviceObject is WorkflowService) { service = (WorkflowService)serviceObject; } // If name of the service is not set // service name = xaml file name with extension // service namespace = IIS virtual path // service config name = Activity.DisplayName if (service != null) { if (service.Name == null) { string serviceName = VirtualPathUtility.GetFileName(xamlVirtualPath); string serviceNamespace = String.Format(CultureInfo.InvariantCulture, "/{0}{1}", ServiceHostingEnvironment.SiteName, VirtualPathUtility.GetDirectory(ServiceHostingEnvironment.FullVirtualPath)); service.Name = XName.Get(XmlConvert.EncodeLocalName(serviceName), serviceNamespace); if (service.ConfigurationName == null && service.Body != null) { service.ConfigurationName = XmlConvert.EncodeLocalName(service.Body.DisplayName); } } serviceHost = CreateWorkflowServiceHost(service, baseAddresses); } } else { Type activityType = this.GetTypeFromAssembliesInCurrentDomain(constructorString); if (null == activityType) { activityType = GetTypeFromCompileCustomString(compiledCustomString, constructorString); } if (null == activityType) { //for file-less cases, try in referenced assemblies as CompileCustomString assemblies are empty. BuildManager.GetReferencedAssemblies(); activityType = this.GetTypeFromAssembliesInCurrentDomain(constructorString); } if (null != activityType) { if (!TypeHelper.AreTypesCompatible(activityType, typeof(Activity))) { throw FxTrace.Exception.AsError(new InvalidOperationException(SR.TypeNotActivity(activityType.FullName))); } Activity activity = (Activity)Activator.CreateInstance(activityType); serviceHost = CreateWorkflowServiceHost(activity, baseAddresses); } } if (serviceHost == null) { throw FxTrace.Exception.AsError( new InvalidOperationException(SR.CannotResolveConstructorStringToWorkflowType(constructorString))); } //The Description.Name and Description.NameSpace aren't included intentionally - because //in farm scenarios the sole and unique identifier is the service deployment URL ((IDurableInstancingOptions)serviceHost.DurableInstancingOptions).SetScopeName( XName.Get(XmlConvert.EncodeLocalName(VirtualPathUtility.GetFileName(ServiceHostingEnvironment.FullVirtualPath)), String.Format(CultureInfo.InvariantCulture, "/{0}{1}", ServiceHostingEnvironment.SiteName, VirtualPathUtility.GetDirectory(ServiceHostingEnvironment.FullVirtualPath)))); return serviceHost; } Type GetTypeFromAssembliesInCurrentDomain(string typeString) { Type activityType = Type.GetType(typeString, false); if (null == activityType) { foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) { activityType = assembly.GetType(typeString, false); if (null != activityType) { break; } } } return activityType; } Type GetTypeFromCompileCustomString(string compileCustomString, string typeString) { if (string.IsNullOrEmpty(compileCustomString)) { return null; } string[] components = compileCustomString.Split('|'); if (components.Length < 3) { throw FxTrace.Exception.AsError(new InvalidOperationException(SR.InvalidCompiledString(compileCustomString))); } Type activityType = null; for (int i = 3; i < components.Length; i++) { Assembly assembly = Assembly.Load(components[i]); activityType = assembly.GetType(typeString, false); if (activityType != null) { break; } } return activityType; } protected virtual WorkflowServiceHost CreateWorkflowServiceHost(Activity activity, Uri[] baseAddresses) { return new WorkflowServiceHost(activity, baseAddresses); } protected virtual WorkflowServiceHost CreateWorkflowServiceHost(WorkflowService service, Uri[] baseAddresses) { return new WorkflowServiceHost(service, baseAddresses); } // The code is optimized for reducing impersonation // true means serviceFileStream has been set; false means CompiledCustomString has been set [Fx.Tag.SecurityNote(Critical = "Calls SecurityCritical method HostingEnvironmentWrapper.UnsafeImpersonate().", Safe = "Demands unmanaged code permission, disposes impersonation context in a finally, and tightly scopes the usage of the umpersonation token.")] [SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)] [SecuritySafeCritical] bool GetServiceFileStreamOrCompiledCustomString(string virtualPath, Uri[] baseAddresses, out Stream serviceFileStream, out string compiledCustomString) { IDisposable unsafeImpersonate = null; compiledCustomString = null; serviceFileStream = null; try { try { try { } finally { unsafeImpersonate = HostingEnvironmentWrapper.UnsafeImpersonate(); } if (HostingEnvironment.VirtualPathProvider.FileExists(virtualPath)) { serviceFileStream = HostingEnvironment.VirtualPathProvider.GetFile(virtualPath).Open(); return true; } else { if (!AspNetEnvironment.Current.IsConfigurationBased) { compiledCustomString = BuildManager.GetCompiledCustomString(baseAddresses[0].AbsolutePath); } return false; } } finally { if (null != unsafeImpersonate) { unsafeImpersonate.Dispose(); } } } catch { throw; } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- OverflowException.cs
- BitmapEditor.cs
- TableAdapterManagerMethodGenerator.cs
- InfiniteTimeSpanConverter.cs
- BamlMapTable.cs
- DataGridViewLinkColumn.cs
- BitmapSourceSafeMILHandle.cs
- EventLogRecord.cs
- FaultContractAttribute.cs
- SplineKeyFrames.cs
- WindowsGraphicsCacheManager.cs
- XmlExpressionDumper.cs
- DocumentEventArgs.cs
- TimeoutValidationAttribute.cs
- GZipStream.cs
- FixedSOMSemanticBox.cs
- Oid.cs
- AdornerPresentationContext.cs
- EntityContainer.cs
- Model3D.cs
- TextBox.cs
- DataGridViewRow.cs
- SiteMembershipCondition.cs
- DeflateStream.cs
- BooleanSwitch.cs
- ListControlBoundActionList.cs
- DataGridPagerStyle.cs
- TextBox.cs
- Utils.cs
- Pair.cs
- ToolStripStatusLabel.cs
- NotificationContext.cs
- ISAPIApplicationHost.cs
- LicFileLicenseProvider.cs
- AutoResizedEvent.cs
- SqlNodeTypeOperators.cs
- TypedRowGenerator.cs
- sqlpipe.cs
- BinaryWriter.cs
- SystemIcmpV6Statistics.cs
- TraversalRequest.cs
- SecurityUtils.cs
- MultiAsyncResult.cs
- ObjectDataSourceMethodEditor.cs
- DocumentSequenceHighlightLayer.cs
- DataRelationCollection.cs
- FrameworkElementFactory.cs
- StoryFragments.cs
- BamlLocalizer.cs
- TreePrinter.cs
- PathSegmentCollection.cs
- ReliabilityContractAttribute.cs
- CodeMemberMethod.cs
- TagMapCollection.cs
- AddInEnvironment.cs
- PropertySourceInfo.cs
- _DisconnectOverlappedAsyncResult.cs
- IIS7WorkerRequest.cs
- EntityClientCacheEntry.cs
- ScriptModule.cs
- Lasso.cs
- Double.cs
- EditorZoneBase.cs
- EncoderBestFitFallback.cs
- Utils.cs
- ListViewItemMouseHoverEvent.cs
- ExceptionHelpers.cs
- ContractAdapter.cs
- SatelliteContractVersionAttribute.cs
- BridgeDataReader.cs
- DoubleAverageAggregationOperator.cs
- Vector.cs
- SqlGenerator.cs
- HandlerBase.cs
- CustomErrorsSectionWrapper.cs
- EventDescriptorCollection.cs
- ExpandSegmentCollection.cs
- ObjectViewQueryResultData.cs
- HttpListenerPrefixCollection.cs
- ResXResourceWriter.cs
- SqlWebEventProvider.cs
- AnimationTimeline.cs
- UIElement.cs
- GroupStyle.cs
- UniformGrid.cs
- OpCodes.cs
- AnnotationResource.cs
- PageThemeCodeDomTreeGenerator.cs
- SubMenuStyle.cs
- CommandHelper.cs
- ZoneMembershipCondition.cs
- OwnerDrawPropertyBag.cs
- StrokeDescriptor.cs
- DbException.cs
- ProviderBase.cs
- Scheduler.cs
- DataGridView.cs
- PrivilegeNotHeldException.cs
- _HeaderInfoTable.cs
- DesignTimeVisibleAttribute.cs