How do I pass values to the constructor on my wcf service?

前端 未结 8 497
悲哀的现实
悲哀的现实 2020-11-22 13:09

I would like to pass values into the constructor on the class that implements my service.

However ServiceHost only lets me pass in the name of the type to create,

8条回答
  •  太阳男子
    2020-11-22 13:29

    We were facing this same problem and have solved it in the following manner. It is a simple solution.

    In Visual Studio just create a normal WCF service application and remove it's interface. Leave the .cs file in place (just rename it) and open that cs file and replace the name of the interface with your original class name which implements the service logic (this way the service class uses inheritance and replaces your actual implementation). Add a default constructor that calls the base class's constructors, like this:

    public class Service1 : MyLogicNamespace.MyService
    {
        public Service1() : base(new MyDependency1(), new MyDependency2()) {}
    }
    

    The MyService base class is the actual implementation of the service. This base class should not have a parameterless constructor, but only constructors with parameters that accept the dependencies.

    The service should use this class instead of the original MyService.

    It's a simple solution and works like a charm :-D

提交回复
热议问题