none
WCF 4.0 svc file less activation and the ServiceHostFactory RRS feed

  • Question

  • I was wondering if it was possible to use WCF 4.0 svc file less activation if you are creating your own custom ServiceHost using the ServiceHostFactory.

    In WCF 3.5 we use to have to put the following <% @ServiceHost Factory=”DerivedFactory” Service=”MyService” %> in an svc file do you still need to do this with WCF 4.0? Can you achieve fileless activation when implementing your own ServiceHostFactory?

    Tuesday, April 20, 2010 2:22 AM

Answers

  • Yes, you can use the "factory" attribute of the system.serviceModel/serviceHostingEnvironment/serviceActivation/add element.

    <configuration>
      <system.serviceModel>
        <serviceHostingEnvironment>
          <serviceActivations>
            <add service="MyTest.Service" factory="MyTest.MyServiceHostFactory" relativeAddress="/Hello.svc" />
          </serviceActivations>
        </serviceHostingEnvironment>
      </system.serviceModel>
    </configuration>

    using System;
    using System.ServiceModel;
    using System.ServiceModel.Activation;
    
    namespace MyTest
    {
      [ServiceContract]
      public interface ITest
      {
        [OperationContract]
        string Hello(string name);
      }
      public class Service : ITest
      {
        public string Hello(string name)
        {
          return "Hello " + name.ToString();
        }
      }
      public class MyServiceHostFactory : ServiceHostFactory
      {
        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
          return new MyServiceHost(serviceType, baseAddresses);
        }
      }
      public class MyServiceHost : ServiceHost
      {
        public MyServiceHost(Type serviceType, Uri[] baseAddresses) : base(serviceType, baseAddresses) { }
    
        protected override void InitializeRuntime()
        {
          this.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), "basic");
          base.InitializeRuntime();
        }
      }
    }
    
    • Marked as answer by Rob4321 Tuesday, April 20, 2010 10:44 PM
    Tuesday, April 20, 2010 6:59 PM
  • This is discussed in the docs at: http://msdn.microsoft.com/en-us/library/ee358764(v=VS.100).aspx We'd like to make this topic easier for customers to find, so if you did try searching for it, may I ask what search terms you used?

    Thanks,

    Michael Green [MSFT]

    • Marked as answer by Rob4321 Tuesday, April 20, 2010 10:44 PM
    Tuesday, April 20, 2010 8:27 PM

All replies

  • how to define the factory?

    I am not sure WCF4.0 provides the API?

    I d like to know more detail about this issue

     

    I have written a blog for it (IIS hosting without an SVC file )  and a demo you can download here,/Files/frank_xl/8.FileLessHost.zip


    Frank Xu Lei--谦卑若愚,好学若饥
    专注于.NET平台下分布式应用系统开发和企业应用系统集成
    Focus on Distributed Applications Development and EAI based on .NET
    欢迎访问老徐的中文技术博客:Welcome to My Chinese Technical Blog
    欢迎访问微软WCF中文技术论坛:Welcome to Microsoft Chinese WCF Forum
    欢迎访问微软WCF英文技术论坛:Welcome to Microsoft English WCF Forum
    Tuesday, April 20, 2010 5:28 AM
  • Yes, you can use the "factory" attribute of the system.serviceModel/serviceHostingEnvironment/serviceActivation/add element.

    <configuration>
      <system.serviceModel>
        <serviceHostingEnvironment>
          <serviceActivations>
            <add service="MyTest.Service" factory="MyTest.MyServiceHostFactory" relativeAddress="/Hello.svc" />
          </serviceActivations>
        </serviceHostingEnvironment>
      </system.serviceModel>
    </configuration>

    using System;
    using System.ServiceModel;
    using System.ServiceModel.Activation;
    
    namespace MyTest
    {
      [ServiceContract]
      public interface ITest
      {
        [OperationContract]
        string Hello(string name);
      }
      public class Service : ITest
      {
        public string Hello(string name)
        {
          return "Hello " + name.ToString();
        }
      }
      public class MyServiceHostFactory : ServiceHostFactory
      {
        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
          return new MyServiceHost(serviceType, baseAddresses);
        }
      }
      public class MyServiceHost : ServiceHost
      {
        public MyServiceHost(Type serviceType, Uri[] baseAddresses) : base(serviceType, baseAddresses) { }
    
        protected override void InitializeRuntime()
        {
          this.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), "basic");
          base.InitializeRuntime();
        }
      }
    }
    
    • Marked as answer by Rob4321 Tuesday, April 20, 2010 10:44 PM
    Tuesday, April 20, 2010 6:59 PM
  • This is discussed in the docs at: http://msdn.microsoft.com/en-us/library/ee358764(v=VS.100).aspx We'd like to make this topic easier for customers to find, so if you did try searching for it, may I ask what search terms you used?

    Thanks,

    Michael Green [MSFT]

    • Marked as answer by Rob4321 Tuesday, April 20, 2010 10:44 PM
    Tuesday, April 20, 2010 8:27 PM
  • Sure I searched a lot mainly file less activation, ServiceHostFactory. No svc ServiceHostFactory that sort of thing
    Tuesday, April 20, 2010 10:46 PM