How to open all my service''s hosts dynamically
I have a ConsoleApp which for now is my WCF server. It's app.config has all my services configured.
The thing is, as Edwin mentioned in thispostwith no answer, I don't want to write to code to create all my hosts and open them. I need to find a more dynamic approach.
In remoting I used the Syste.Runtime.RemotingConfiguration.Configure(), do I have anything like it in WCF?
TIA
Regards,
Sebastian
On the post you referenced, did Ed Pinto's suggestion help at all? I think you will still have to write code to create ServiceHosts, but I think this can be done rather abstractly using Ed's approach, allowing you to add new service configurations to App.config without needing edit the code.
-James
So what you're saying is there's nothing like it and I have to write the code myself.
There's no native function that I can use as I did with remoting then, right?
Regards,
Seba
Well, you could webhost it. Then the services you have in config would be run for you by the w3p process. But then you wouldn't be using a console app to be your server. If you require using a console app to be the server, I don't think there's anyway to open servicehosts via config. You have to write code for that.
As for what functionality is analogous to remoting, I'm not really sure, since I don't have experience with remoting.
HTH,
James
In remoting I had a remoting.config file and in my Console app I had a call to a Config method that did everthing for me.
I guess I'll have to write it my self now.
Thanks,
Seba
Maybe something like this (without proper exception handling):
public class ServiceHostGroup<TServiceHost>
where TServiceHost : ServiceHost
{
static List<TServiceHost> hosts = new List<TServiceHost>();
public static void StartServices()
{
Configuration conf =
ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
ServiceModelSectionGroup svcmod =
(ServiceModelSectionGroup)conf.GetSectionGroup("system.serviceModel");
foreach (ServiceElement el in svcmod.Services.Services)
{
Type svcType = Type.GetType(el.Name);
if (svcType == null)
throw new Exception("Invalid Service Type " + el.Name + " in configuration file.");
OpenHost(svcType);
}
}
public static void CloseServices()
{
foreach (TServiceHost hst in hosts)
{
hst.Close();
}
}
private static void OpenHost(Type t)
{
TServiceHost hst = (TServiceHost)Activator.CreateInstance(
typeof(TServiceHost), new object[] { t });
hst.Open();
hosts.Add(hst);
}
}
Hm.
ServiceHostGroup.StartServices();
...
ServiceHostGroup.CloseServices();
works like a charm with something like
Code Snippet
<services>
<service behaviorConfiguration="metadataBehavior" name="WcfPlayground.Dummy">
<host>
<baseAddresses>
<add baseAddress="net.tcp://pcbrick:7777/MB" />
<add baseAddress="http://pcbrick/MB" />
</baseAddresses>
</host>
<endpoint
address="dummy"
binding="basicHttpBinding"
contract="WcfPlayground.IDummy" />
<endpoint
address="dummyInternal"
binding="netTcpBinding" bindingNamespace="AAA"
contract="WcfPlayground.IDummy" />
</service>
</services>
If I call them the way you do (ServiceHostGroup.StartServices();), I get the following error:
Using the generic type 'WCF.WCFServer.ServiceHostGroup<TServiceHost>' requires '1' type arguments
I'm lost 
This definitely works.
ServiceHostGroup<ServiceHost>.StartServices();
ServiceHostGroup<ServiceHost>.CloseServices();
If you still have problems, pls contact me offline.