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.
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.