Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8733595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:40:46+00:00 2026-06-13T09:40:46+00:00

My goal is to create a host application able to parse multiple assemblies, detect

  • 0

My goal is to create a host application able to parse multiple assemblies, detect the contracts and host the services.

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.

ServiceHost wService1Host = new ServiceHost(typeof(Service1));
wService1Host.Open();

ServiceHost wService2Host = new ServiceHost(typeof(Service2));
wService2Host.Open();

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.

This is very similar to this question, but with an added complexity for the reason mentioned above.

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.

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 && 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 <Enter> to stop services...");
      Console.ReadLine();
    }
  }

I get the following error when trying to create the serviceHost :

"It is illegal to reflect on the custom attributes of a Type loaded via ReflectionOnlyGetType (see Assembly.ReflectionOnly) -- use CustomAttributeData instead."

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.

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.

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.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>    
    <behaviors>
      <serviceBehaviors>  
        <behavior name="metadataBehavior">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>        
      </serviceBehaviors>  
    </behaviors>
    <services>
      <service name="WcfServices.Service1"
               behaviorConfiguration="metadataBehavior">

        <endpoint address="Service1Service"
                  binding="basicHttpBinding"
                  contract="WcfServices.IService1"
                  name="basicHttp"/>

        <endpoint binding="mexHttpBinding"
                  contract="IMetadataExchange"
                  name="metadataExchange"/>

        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8000/Service1"/>
          </baseAddresses>
        </host>        
      </service>

      <service name="WcfServices.Service2"
               behaviorConfiguration="metadataBehavior">

        <endpoint address="Service2Service"
                  binding="wsDualHttpBinding"
                  contract="WcfServices.IService2"/>

        <endpoint address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange"/>

        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8000/Service2"/>
          </baseAddresses>
        </host>
      </service>

    </services>    
  </system.serviceModel>
</configuration>

So, to be clear, here’s what I’m looking for :
1. Load assemblies in current app directory
2. Looks if there are any contracts implementation in it
3. If there are, instantiate those services (using app.config for the moment)

First of all, is this even possible ? (My guess would be it is since an application named wcfstorm alread seems to do this)
Obviously, How could I make the code above works ?

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:40:48+00:00Added an answer on June 13, 2026 at 9:40 am

    Here’s what I ended up doing :

    private static void LoadServices()
    {
      // find currently executing assembly
      Assembly Wcurr = Assembly.GetExecutingAssembly();
    
      // get the directory where this app is running in
      string wCurrentLocation = Path.GetDirectoryName(Wcurr.Location);
    
      // enumerate over those assemblies
      foreach (string wAssemblyName in mAssemblies)
      {
        // load assembly just for inspection
        Assembly wAssemblyToInspect = null;
        try
        {
          wAssemblyToInspect = Assembly.LoadFrom(wCurrentLocation + "\\" + wAssemblyName);
        }
        catch (System.Exception ex)
        {
          Console.WriteLine("Unable to load assembly : {0}", wAssemblyName);
        }
    
    
        if (wAssemblyToInspect != null)
        {
          // find all types with the HostService attribute
          IEnumerable<Type> wTypes = wAssemblyToInspect.GetTypes().Where(t => Attribute.IsDefined(t, typeof(HostService), false));
    
          foreach (Type wType in wTypes)
          {
            ServiceHost wServiceHost = new ServiceHost(wType);
            wServiceHost.Open();
            mServices.Add(wServiceHost);
            Console.WriteLine("New Service Hosted : {0}", wType.Name);
          }
        }
      }
    
      Console.WriteLine("Services are up and running.");
    }
    

    Note : This approach requires that the assemblies be referenced by the “host” project.

    Note2 : In order to accelerate the assembly parsing, I’ve hardcoded which assemblies to load in “mAssemblies”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

High Level Goal: Create a single Maven Web Application project that can be used
Goal: to create a percentage column based off the values of calculated columns. Here's
Duplicate: PHP validation/regex for URL My goal is create a PHP regex for website
I am very new to Json and my goal to create the Json output
My goal is to create a canned email on my server and then send
My goal is to create a list from menu.bin. This is the func: pitem
My goal is to create an entry form (addnew.php) that will allow me to
my goal is to create a sort of Javascript library, if you could call
The goal is to create a mock class which behaves like a db resultset.
My goal is to create an efficient structure to store the most relevant entries

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.