0

Using VS2010 and .NET v4 I created a service interface that looks like this:

[ServiceContract]
    public interface InterfaceTestService
    {
        [OperationContract]
        String GetMessage();
...

And the server implementation that looks like this:

namespace TestServices
{
    public class TestServices: InterfaceTestService
    {
        public TestServices()
        {
            ...;
        }

        public String GetMessage()
        {
            return "TEST";
        }

Works fine at its current state, but if I try to add a parameter to my initialization function:

 public TestServices(int number)

I can no longer use the service and get this message:

The service type provided could not be loaded as a service because it does not have a default (parameter-less) constructor. To fix the problem, add a default constructor to the type, or pass an instance of the type to the host.

Something that makes sense, but how can I create a constructor that accepts parameters for my service?

dearn44
  • 2,628
  • 1
  • 24
  • 52
  • 2
    you'll need to implement `IInstanceProvider` methods that are responsible for constructing the service instance, and then provide the type as the instance provider of the endpoint dispather's `DispathRuntime.InstanceProvider` property. – Amit Kumar Ghosh Feb 26 '16 at 10:49
  • 1
    This is already answered in [previous question](http://stackoverflow.com/questions/2454850/how-do-i-pass-values-to-the-constructor-on-my-wcf-service). – Simon Hardy Feb 26 '16 at 11:19
  • Where is the TestServices constructor which takes a single integer as a paramter defined? It's not in your code sample. – tom redfern Feb 26 '16 at 11:54
  • Hi, Simon I am not sure I understand how to implement what is said in that answer. I am calling this service from a client webpage and it wont work if I try `new TestServiceClient("somestring")` the exception is `Could not find endpoint element with name '26' and contract...`. Im quite new to this, moreover this technology is quite old on itself I think. Tom, its simply the same constructor just I added a string argument at it, its in my question. – dearn44 Feb 26 '16 at 12:07
  • `TestServiceClient` relies on newing up your `InterfaceTestService` with a default constructor. Just because you add a constructor to `InterfaceTestService` (which would *remove* the parameterless constructor - edit: if that's the only constructor in the class I mean) does not mean your `TestServiceClient` would know that. – Kritner Feb 26 '16 at 14:09
  • A couple of thoughts. First, why is your client trying to supply information in the construction of the service? That would seem to at least bend the precepts of SOA. Second, another option would be to create the service as a singleton (allowing you to create an instance of the service and then pass that instance to the service host) - **however** that's applicable (IMO) in only very select scenarios. I would reevaluate the need for a constructor that takes an argument first. – Tim Feb 26 '16 at 18:30
  • @Tim hi, there is no reason to be honest. I am doing some personal projects in order to learn working with wcf services and iis. During the time I was working on it I came to the realization that indeed there should be no need to do that. In any case I was curious to learn how since its clearly something that can be done. – dearn44 Feb 29 '16 at 08:17

0 Answers0