tags:

views:

1185

answers:

1

Hi All, I have created a class library called AddServiceLibrary in which I have a method called AssemblyLoader the code is below:

string executingAssemblyName =  Application.ExecutablePath;
        AssemblyName asmName = AssemblyName.GetAssemblyName(executingAssemblyName);
        AppDomain appDomain = AppDomain.CurrentDomain;
        Assembly assembly = appDomain.Load(asmName);
        _assemblyTypes = assembly.GetTypes().ToList();            
        LoadAppConfig();

this method loads the executing assembly in the current appdomain. I have another method called LoadAppConfig()

ServicesSection  serviceSection = ConfigurationManager.GetSection("system.serviceModel/services") as ServicesSection;

            ServiceElementCollection sereleColl = serviceSection.Services;


            string endPointAddress = string.Empty ;
            foreach (var ele in sereleColl)
            {
               _serviceType = GetServiceType((System.ServiceModel.Configuration.ServiceElement)(ele)).Name);

               break;

            }

            ServiceHoster.HostService(_serviceType);

This method reads the app.config file and finds the type of wcf service . I have one more class ServiceHoster in which I have a method HostService :

public static void HostService(Type serviceType)
        {

            using (ServiceHost host = new ServiceHost(serviceType))
            {
                host.Open();
            }

        }

now , I have a different project called MyWCFService and I add the reference of AddServiceLibrary in this project and call the method;

AddServiceLibrary.LoadLibrary lb = new AddServiceLibrary.LoadLibrary();
            lb.AssemblyLoader();

I hope at this point that my service is hosted properly , but when i want AddServiceReference in my client project it tell's me that No connection could be made because the target machine actively refused it prob in While if I don't use my AddServiceLibrary , it's finding the service and working fine. Please any one have a look on it and suggest me what could be wrong in my approach.

+2  A: 

You are disposing the ServiceHost instance as soon as you open it. Remove the using block, and handle disposal of this host separately.

Your design decision to have a static ServiceHoster is probably an issue here. Whatever is hosting the service by instantiating the ServiceHost instance needs to manage the life of this instance and its disposal.

David M
thank you sir , it works when i remove the using block , but sir can you pls guide me how can I implement the disposal of service host separately ?
Praveen
What method do you currently have in place to stop the hosting of the service? Can you post more of your ServiceHoster class?
David M
The complete class is as follows:class ServiceHoster { public static void HostService(Type serviceType) { ServiceHost host = new ServiceHost(serviceType); try { host.Open(); } } }
Praveen
You don't seem to have considered shutting down these services anywhere. When you instantiate your LoadLibrary, how long does that object live for?
David M
I instantiate LoadLibrary object and call the AddServiceLibrary method of it , from this methos I call ServiceHoster.HostService method , I am sorry but i don't know how long does it live ?
Praveen
Perhaps if you explain the broader context of what you are trying to do as well I could make some suggestions as to how you would need to modify your approach?
David M
Your ServiceHost object will fall out of scope once your constructor completes execution. You need to keep it alive longer. These are basic C# / OO concepts you'll need to learn to be successful. More on object lifetime here: http://darkcodingzone.blogspot.com/2008/11/basics-of-object-lifetime-in-cnet.html
Anderson Imes
Thanks for this answer! I had been looking at my code for ages before I read this! +1
RemotecUk
Glad it helped!
David M