Search code examples
c#wcfgenericstypeloadexception

Referencing (loading) a generic type from its name in a WCF Svc markup file


In a WCF Svc markup file, I would like to use a generic type as a factory :

<%@ ServiceHost Language="C#" Debug="true" Factory="MY_GENERIC_TYPE_GOES_HERE" Service="WebSrv.MyService" CodeBehind="MyService.svc.cs" %>

The definition of my generic class is like this :

namespace Tools.Services
{
   public class ServiceHostFactory<TService> : System.ServiceModel.Activation.ServiceHostFactoryBase
   {
          //...
   }
}

How can I reference this type with a given Type as the template type, I mean something like this :

Tools.Services.ServiceHostFactory<WebSrv.MyService>

By looking at the MSDN Type.GetType documentation, I tried the following with no luck :

<%@ ServiceHost Language="C#" Debug="true" Service="WebSrv.MyService" Factory="Tools.Services.ServiceHostFactory`1[WebSrv.MyService]"  CodeBehind="MyService.svc.cs" %>  

Output:

Server Error in '/' Application. Parser Error Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

Parser Error Message: The CLR Type 'Tools.Services.ServiceHostFactory`1[WebSrv.MyService]' could not be loaded during service compilation. Verify that this type is either defined in a source file located in the application's \App_Code directory, contained in a compiled assembly located in the application's \bin directory, or present in an assembly installed in the Global Assembly Cache. Note that the type name is case-sensitive and that the directories such as \App_Code and \bin must be located in the application's root directory and cannot be nested in subdirectories.

Output:

Server Error in '/' Application. Could not load file or assembly 'Tools.Services' or one of its dependencies. The system cannot find the file specified.

<%@ ServiceHost Language="C#" Debug="true" Service="WebSrv.MyService" Factory="Tools.Services.ServiceHostFactory`1[WebSrv.MyService,WebSrv ],Tools.Services"  CodeBehind="MyService.svc.cs" %>

Output:

Server Error in '/' Application. Could not load file or assembly 'Tools.Services' or one of its dependencies. The system cannot find the file specified.

Is this even possible to achieve in a generic way ??


Solution

  • I just found the answer :
    I had to use double brackets here.
    And because two types from two different assemblies were involved, I had to fully qualified each of them the following way :

    Tools.Services.ServiceHostFactory`1[[WebSrv.MyService, WebSrv]], Tools.Services
    

    Hope this will help someone.