title: Wcf Dynamic Hosting using reflection
body: <p>My goal is to create a host application able to parse multiple assemblies, detect the contracts and host the services.</p>
  
  <p>In order to load a service, we usually need to hardcode the servicehost instantiation. the following code is working despite not being the behaviour I'm looking for.</p>
  
  <pre><code>ServiceHost wService1Host = new ServiceHost(typeof(Service1));
  wService1Host.Open();
  
  ServiceHost wService2Host = new ServiceHost(typeof(Service2));
  wService2Host.Open();
  </code></pre>
  
  <p>However, this mean I know in advance what the services would be. 
  I don't mind having a reference to the assemblies containing the services. I just want the host not knowing about what services are contained within the assemblies. For example, if I add a new Service to one of the assemblies, no changes would be needed on the host side.</p>
  
  <p>This is very similar to this <a href="https://stackoverflow.com/questions/5993563/can-i-automatically-host-all-services-in-app-config-when-using-selfhosting?lq=1">question</a>, but with an added complexity for the reason mentioned above.</p>
  
  <p>Here is the host code I've come with so far. I don't mind managing the services at the moment, I simply want them to be loaded properly.</p>
  
  <pre><code>class Program
    {
      static void Main(string[] args)
      {
  
        // find currently executing assembly
        Assembly curr = Assembly.GetExecutingAssembly();
  
        // get the directory where this app is running in
        string currentLocation = Path.GetDirectoryName(curr.Location);
  
        // find all assemblies inside that directory
        string[] assemblies = Directory.GetFiles(currentLocation, "*.dll");
  
        // enumerate over those assemblies
        foreach (string assemblyName in assemblies)
        {
          // load assembly just for inspection
          Assembly assemblyToInspect = Assembly.ReflectionOnlyLoadFrom(assemblyName);
  
          // I've hardcoded the name of the assembly containing the services only to ease debugging
          if (assemblyToInspect != null &amp;&amp; assemblyToInspect.GetName().Name == "WcfServices")
          {
            // find all types
            Type[] types = assemblyToInspect.GetTypes();
  
            // enumerate types and determine if this assembly contains any types of interest
            // you could e.g. put a "marker" interface on those (service implementation)
            // types of interest, or you could use a specific naming convention (all types
            // like "SomeThingOrAnotherService" - ending in "Service" - are your services)
            // or some kind of a lookup table (e.g. the list of types you need to find from
            // parsing the app.config file)
            foreach (Type ty in types)
            {
              Assembly implementationAssembly = Assembly.GetAssembly(ty);
              // When loading the type for the service, load it from the implementing assembly.
              Type implementation = implementationAssembly.GetType(ty.FullName);
  
              ServiceHost wServiceHost = new ServiceHost(implementation); // FAIL
              wServiceHost.Open();
            }
          }
        }
        Console.WriteLine("Service are up and running.");
        Console.WriteLine("Press &lt;Enter&gt; to stop services...");
        Console.ReadLine();
      }
    }
  </code></pre>
  
  <p>I get the following error when trying to create the serviceHost :</p>
  
  <pre><code>"It is illegal to reflect on the custom attributes of a Type loaded via ReflectionOnlyGetType (see Assembly.ReflectionOnly) -- use CustomAttributeData instead."
  </code></pre>
  
  <p>In the link given above, the guy seems to have solved its problem using typeof since he knows in advance what service he wants to expose. Unfortunately, this is not my case.</p>
  
  <p>Note : For the hosting part, I actually have 3 projects. The first one is the host application (see above), the second one, is an assembly containing all my service's contract (the interfaces) and the last assembly contains the services implementation.</p>
  
  <p>Here is the app.config I actually use for hosting the services. The assembly containing the implementation is named "WcfServices" and contains 2 services. One is exposing callbacks and the other only basic services.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;configuration&gt;
    &lt;system.serviceModel&gt;    
      &lt;behaviors&gt;
        &lt;serviceBehaviors&gt;  
          &lt;behavior name="metadataBehavior"&gt;
            &lt;serviceMetadata httpGetEnabled="true"/&gt;
          &lt;/behavior&gt;        
        &lt;/serviceBehaviors&gt;  
      &lt;/behaviors&gt;
      &lt;services&gt;
        &lt;service name="WcfServices.Service1"
                 behaviorConfiguration="metadataBehavior"&gt;
  
          &lt;endpoint address="Service1Service"
                    binding="basicHttpBinding"
                    contract="WcfServices.IService1"
                    name="basicHttp"/&gt;
  
          &lt;endpoint binding="mexHttpBinding"
                    contract="IMetadataExchange"
                    name="metadataExchange"/&gt;
  
          &lt;host&gt;
            &lt;baseAddresses&gt;
              &lt;add baseAddress="http://localhost:8000/Service1"/&gt;
            &lt;/baseAddresses&gt;
          &lt;/host&gt;        
        &lt;/service&gt;
  
        &lt;service name="WcfServices.Service2"
                 behaviorConfiguration="metadataBehavior"&gt;
  
          &lt;endpoint address="Service2Service"
                    binding="wsDualHttpBinding"
                    contract="WcfServices.IService2"/&gt;
  
          &lt;endpoint address="mex"
                    binding="mexHttpBinding"
                    contract="IMetadataExchange"/&gt;
  
          &lt;host&gt;
            &lt;baseAddresses&gt;
              &lt;add baseAddress="http://localhost:8000/Service2"/&gt;
            &lt;/baseAddresses&gt;
          &lt;/host&gt;
        &lt;/service&gt;
  
      &lt;/services&gt;    
    &lt;/system.serviceModel&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>So, to be clear, here's what I'm looking for :<br>
   1. Load assemblies in current app directory<br>
   2. Looks if there are any contracts implementation in it<br>
   3. If there are, instantiate those services (using app.config for the moment)  </p>
  
  <p>First of all, is this even possible ? (My guess would be it is since an application named wcfstorm alread seems to do this)<br>
  Obviously, How could I make the code above works ?</p>
  
  <p>Thank you!</p>