/// <summary> /// 静态构造函数只执行一次 /// </summary> static ServiceRegister() { wshf = new MyServiceHostFactory(); }
private ServiceHostBase CreateService(string normalizedVirtualPath) { string virtualPath; string serviceType; string str2 = ""; ServiceHostBase base2 = null; ServiceHostFactoryBase serviceHostFactory = null; string[] strArray = null; string compiledCustomString = ""; if (System.ServiceModel.Activation.TD.CompilationStartIsEnabled()) { System.ServiceModel.Activation.TD.CompilationStart(); } if (isAspNetRoutedRequest && isConfigurationBased) { if (!RouteTable.Routes.RouteExistingFiles) { ServiceRouteHandler.MarkARouteAsInactive(normalizedVirtualPath); isAspNetRoutedRequest = false; } else { isConfigurationBased = false; } } if (!isAspNetRoutedRequest) { compiledCustomString = this.GetCompiledCustomString(normalizedVirtualPath); if (string.IsNullOrEmpty(compiledCustomString)) { string name = System.ServiceModel.HostingEnvironmentWrapper.GetServiceFile(normalizedVirtualPath).Name; string str6 = normalizedVirtualPath.Substring(0, normalizedVirtualPath.LastIndexOf('/') + 1); normalizedVirtualPath = string.Format(CultureInfo.CurrentCulture, "{0}{1}", new object[] { str6, name }); serviceType = virtualPath = normalizedVirtualPath; serviceHostFactory = this.CreateWorkflowServiceHostFactory(normalizedVirtualPath); } else { strArray = compiledCustomString.Split("|".ToCharArray()); if (strArray.Length < 3) { throw System.ServiceModel.Activation.FxTrace.Exception.AsError(new InvalidOperationException(System.ServiceModel.Activation.SR.Hosting_CompilationResultInvalid(normalizedVirtualPath))); } virtualPath = strArray[0]; str2 = strArray[1]; serviceType = strArray[2]; } } else { ServiceDeploymentInfo serviceInfo = ServiceRouteHandler.GetServiceInfo(normalizedVirtualPath); virtualPath = serviceInfo.VirtualPath; serviceType = serviceInfo.ServiceType; serviceHostFactory = serviceInfo.ServiceHostFactory; } normalizedVirtualPath = virtualPath; virtualPath = VirtualPathUtility.ToAbsolute(virtualPath); Uri[] baseAddresses = HostedTransportConfigurationManager.GetBaseAddresses(virtualPath); Uri[] prefixFilters = ServiceHostingEnvironment.PrefixFilters; if ((!this.multipleSiteBindingsEnabled && (prefixFilters != null)) && (prefixFilters.Length > 0)) { baseAddresses = FilterBaseAddressList(baseAddresses, prefixFilters); } fullVirtualPath = virtualPath; if (fullVirtualPath.Length == 0) { fullVirtualPath = "/"; } currentVirtualPath = virtualPath.Substring(0, virtualPath.LastIndexOf('/')); if (currentVirtualPath.Length == 0) { currentVirtualPath = "/"; xamlFileBaseLocation = "~/"; } else { xamlFileBaseLocation = VirtualPathUtility.AppendTrailingSlash(currentVirtualPath); } if (isConfigurationBased) { xamlFileBaseLocation = "~/"; if (System.ServiceModel.Activation.TD.CBAMatchFoundIsEnabled()) { System.ServiceModel.Activation.TD.CBAMatchFound(normalizedVirtualPath); } } if (System.ServiceModel.Activation.TD.ServiceHostFactoryCreationStartIsEnabled()) { System.ServiceModel.Activation.TD.ServiceHostFactoryCreationStart(); } if (serviceHostFactory == null) { if (string.IsNullOrEmpty(str2)) { serviceHostFactory = new ServiceHostFactory(); } else { Type c = Type.GetType(str2); if ((c == null) && isConfigurationBased) { ServiceHostingEnvironment.EnsureAllReferencedAssemblyLoaded(); Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); for (int i = 0; i < assemblies.Length; i++) { c = assemblies[i].GetType(str2, false); if (c != null) { break; } } } if (c == null) { throw System.ServiceModel.Activation.FxTrace.Exception.AsError(new InvalidOperationException(System.ServiceModel.Activation.SR.Hosting_FactoryTypeNotResolved(str2))); } if (!typeof(ServiceHostFactoryBase).IsAssignableFrom(c)) { throw System.ServiceModel.Activation.FxTrace.Exception.AsError(new InvalidOperationException(System.ServiceModel.Activation.SR.Hosting_IServiceHostNotImplemented(str2))); } ConstructorInfo constructor = c.GetConstructor(new Type[0]); if (constructor == null) { throw System.ServiceModel.Activation.FxTrace.Exception.AsError(new InvalidOperationException(System.ServiceModel.Activation.SR.Hosting_NoDefaultCtor(str2))); } serviceHostFactory = (ServiceHostFactoryBase) constructor.Invoke(new object[0]); } } if (System.ServiceModel.Activation.TD.ServiceHostFactoryCreationStopIsEnabled()) { System.ServiceModel.Activation.TD.ServiceHostFactoryCreationStop(); } if (((serviceHostFactory is ServiceHostFactory) && !isConfigurationBased) && !isAspNetRoutedRequest) { for (int j = 3; j < strArray.Length; j++) { ((ServiceHostFactory) serviceHostFactory).AddAssemblyReference(strArray[j]); } } if (System.ServiceModel.Activation.TD.CreateServiceHostStartIsEnabled()) { System.ServiceModel.Activation.TD.CreateServiceHostStart(); } base2 = serviceHostFactory.CreateServiceHost(serviceType, baseAddresses); if (System.ServiceModel.Activation.TD.CreateServiceHostStopIsEnabled()) { System.ServiceModel.Activation.TD.CreateServiceHostStop(); } if (base2 == null) { throw System.ServiceModel.Activation.FxTrace.Exception.AsError(new InvalidOperationException(System.ServiceModel.Activation.SR.Hosting_ServiceHostBaseIsNull(serviceType))); } base2.Extensions.Add(new VirtualPathExtension(normalizedVirtualPath, ServiceHostingEnvironment.ApplicationVirtualPath, ServiceHostingEnvironment.SiteName)); if (base2.Description != null) { base2.Description.Behaviors.Add(new ApplyHostConfigurationBehavior()); if (this.multipleSiteBindingsEnabled && (base2.Description.Behaviors.Find<UseRequestHeadersForMetadataAddressBehavior>() == null)) { base2.Description.Behaviors.Add(new UseRequestHeadersForMetadataAddressBehavior()); } } if (System.ServiceModel.Activation.TD.CompilationStopIsEnabled()) { System.ServiceModel.Activation.TD.CompilationStop(); } return base2; }