Click here to Skip to main content
15,176,948 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys
I have been developing a self service host. this service will dynamically look for WCF dll files and then recompile it and host all available svc files.

So long has been no problem.

What i need is to be able to specify web Config file for each servicehost/servicebase object so that it will be able to inhert all appsettings and also concetionstring from it. is that possible?


This is my test code.

C#
    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;

                }


            }

        }
Posted

1 solution

Not sure if this should be a comment rather than solution.

Configuration file of the application that actually runs is the one which is loaded. Now in your case, the console application runs so you will need to keep all the configuration in app.config files. You can make use of ConfigurationManager class to read other configuration files but that you will still need to know what will be there in the web.config before you write code. Your intent looks like to create a generic host for services. These two things are contradictory IMHO.
   
Comments
Alen Toma 12-Jan-16 7:08am
   
well exakt...
I only have access to the DLL, svc and web config files. Different WCF may have same app setting index with different values and there is the problem here.
i could load WCF config file to the main application but because the service is generic so it wont really work either.
So i may need to start an instance of service for each service and have a windows form application that will keep an eye to all consule services.
please do tell if you have a better solution do this problem!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900