Self-host (No IIS or WAS) WCF with a service that requires parameters

Go To StackoverFlow.com

3

Hopefully this is an easy one. I'm wondering if this is possible - perhaps it is not. I'm attempting to self-host a WCF service (in my example below it is a console application). The service does not have a default constructor. It only contains a single parameter signature constructor. I need the service to be able to handle user sessions. Currently I am using Ninject DI. Here is a simple code solution I came up with to demonstrate my issue:

using System;
using System.ServiceModel;
using System.ServiceModel.Web;
using Ninject.Modules;

namespace ConsoleApplication1
{
    public class Program
    {
        static void Main()
        {
            using (var webServiceHost = new WebServiceHost(typeof(MyWcf)))
            {
                var webHttpBinding = new WebHttpBinding();
                var uri = new Uri("http://localhost:8000/");
                webServiceHost.AddServiceEndpoint(typeof(IMyWcf), webHttpBinding, uri);
                webServiceHost.Open();
                Console.WriteLine("Service is ready...");
                Console.ReadKey();
            }
        }
    }

    [ServiceContract]
    public interface IMyWcf
    {
        [OperationContract, WebGet(UriTemplate = "")]
        string HelloWorld();
    }

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    public class MyWcf : IMyWcf
    {
        private readonly IMessage _customMessage = new Message("Default Message.");

        public MyWcf(IMessage message)
        {
            _customMessage = message;
        }

        public string HelloWorld()
        {
            return _customMessage.Text;
        }
    }

    public interface IMessage
    {
        string Text { get; }
    }

    public class Message : IMessage
    {
        public Message (string message)
        {
            Text = message;
        }
        public string Text { get; set; }
    }

    public class NinjectSetup : NinjectModule
    {
        public override void Load()
        {
            Bind<IMessage>().To<Message>()
                .WithConstructorArgument("message", "Injected String Message.");
        }
    }
}

Obviously commenting out the parameterized constructor allows the service to run. But that does me no good. I don't want to use ServiceHostFactory because that apparently requires me to have a .svc/IIS. Is there a way around this? Can I just create a new MyWebServiceHost that inherits from WebServiceHost and override some method that will create a instance for the service?

2012-04-04 19:05
by ymerej
Have you looked in the example in Ninject.Extensions.wcf? Pretty sure the self host example does this - Ruben Bartelink 2012-04-04 19:35
Do you have one in mind that you could link? Every example I've found has it being hosted in IIS and using ServiceHostFactory or ServiceBehavior.. - ymerej 2012-04-04 19:39
Prety sure there are two in the examples dir. One is called self host or sometthing (All obv on github in the Ninject.Extensions.Wcf repository - Ruben Bartelink 2012-04-04 19:41
The self-hosting sample I saw contained the comment "When self-hosting and injecting the service instance, the InstanceContextMode must be set to Single." I need PerSession support though - ymerej 2012-04-04 20:03
Hi Ruben, I'm digging a bit more into, but I just happened to change it to InstanceContextMode.PerSession and it seems to be loading just fine. Hopefully I can come to a reasoning as to why - ymerej 2012-04-04 20:12
This comment is outdated. It seems it was not removed when the support for all instance context modes was added - Remo Gloor 2012-04-04 21:47
To save this from a run-on comment thread. I'll go ahead and mark this resolved. The example does seem sufficient. I was hoping I'd a SessionID back for the OperationContext, but that didn't do it for me. I'll open a new question for that. Thanks Ruben - ymerej 2012-04-05 14:07


1

Using Ruben's suggestion (in the comments) above, I was able to locate a working example within the Ninject.Extensions.Wcf source repository.

2012-04-05 14:08
by ymerej
Ads