public void Start(string configurationName = null) { if (string.IsNullOrEmpty(_basePath)) Load(WCFObject); List<Type> wTypes = new List<Type>(); List<Type> wItypes = new List<Type>(); foreach (var ass in AppDomain.CurrentDomain.GetAssemblies()) { var sub = new List<Type>(); var isub = new List<Type>(); try { sub.AddRange(ass.GetTypes().Where(t => Attribute.IsDefined(t, typeof(HostService), false))); } catch (ReflectionTypeLoadException ex) { foreach (Type theType in ex.Types) { try { if (theType != null && Attribute.IsDefined(theType, typeof(HostService), false)) sub.Add(theType); } // This exception list is not exhaustive, modify to suit any reasons // you find for failure to parse a single assembly catch { // Type not in this assembly - reference to elsewhere ignored } } } try { isub.AddRange(ass.GetTypes().Where(t => Attribute.IsDefined(t, typeof(ServiceContractAttribute), false))); } catch (ReflectionTypeLoadException ex) { foreach (Type theType in ex.Types) { try { if (theType != null && Attribute.IsDefined(theType, typeof(ServiceContractAttribute), false)) isub.Add(theType); } // This exception list is not exhaustive, modify to suit any reasons // you find for failure to parse a single assembly catch { // Type not in this assembly - reference to elsewhere ignored } } } if (!sub.Any() && isub.Any()) { foreach (var type in isub) { try { sub.AddRange(ass.GetTypes().Where(t => t.Name == type.Name.Substring(1))); } catch (ReflectionTypeLoadException ex) { foreach (Type theType in ex.Types) { try { if (theType != null && theType.Name == type.Name.Substring(1)) sub.Add(theType); } // This exception list is not exhaustive, modify to suit any reasons // you find for failure to parse a single assembly catch { // Type not in this assembly - reference to elsewhere ignored } } } } } wTypes.AddRange(sub); wItypes.AddRange(isub); } Message = ""; foreach (Type wType in wTypes) { try { if (!string.IsNullOrEmpty(configurationName) && string.Compare(wType.Name, configurationName.Substring(configurationName.LastIndexOf('.')).Replace(".", ""), true) > 0) continue; if (ExceptionServices.Any(x => x.Contains(wType.Name))) continue; var interfaceName = "I" + wType.Name; ServiceHost wServiceHost = new ServiceHost(wType, GetUri(wType.Name)); AddEndPoint(wServiceHost, wItypes.Find(x => x.Name == interfaceName), GetUri(wType.Name)); BaseAddres.Add(GetUri(wType.Name).AbsoluteUri); wServiceHost.Open(); Message += "\nNew Service Hosted : " + wType.Name + ". Uri : " + GetUri(wType.Name).AbsoluteUri; if (!Services.Any(x => x.Contains(wType.Name))) Services.Add(wType.Namespace + "." + wType.Name); ServiceHostList.RemoveAll(x => !x.BaseAddresses.Any() || x.BaseAddresses[0].AbsoluteUri == GetUri(wType.Name).AbsoluteUri); ServiceHostList.Add(wServiceHost); } catch (Exception ex) { if (ex.Message.Contains("add a default constructor to the type")) { Message += "\nError: Could not start " + wType.Name + " This Service wont load agin until next restart. Exception:" + ex.Message; if (!ExceptionServices.Any(x => x.Contains(wType.Name))) ExceptionServices.Add(wType.Name); } else if (!ServiceHostList.Any(x => x.BaseAddresses.Any(a => a.AbsoluteUri.Contains(wType.Name)) && x.State == CommunicationState.Opened)) Message += "\nError: Could not start " + wType.Name + " Exception:" + ex.Message; } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)