Answered "no parameterless constructor" error in wcf

  • Tuesday, October 31, 2006 2:59 AM
     
     

    Hi all:

      When I used wcf ,I wanted that the class which implemented the contract  had a  parameter constructor.But as I did this, I got a error.  

    Can anyone tell me how to realize what i want.

     

All Replies

  • Tuesday, October 31, 2006 4:12 AM
     
     Answered

    WCF requires a parameterless constructor to dynamically instantiate a service contract implementation.

    However, you do have some options:

    1) You can pass in a self instantiated service contract (singleton) instance to the service host.

    2) You can add a default constructor to your implementation and call a seperate init method to initialize the instance.

    Dave

  • Tuesday, October 31, 2006 4:48 AM
     
     Answered

     

    Here is an example for Singleton  with NetMsmqBinding:

    Uri queueAddress = new Uri(ImportServiceURL);

    NetMsmqBinding msmqbinding = new NetMsmqBinding(NetMsmqSecurityMode.None);

    msmqbinding.MaxReceivedMessageSize = 200 * 1024;

    msmqbinding.QueueTransferProtocol = QueueTransferProtocol.Srmp;

    //Singleton instance with a non-default constructor

    DataExchangeService dxs = new DataExchangeService(ConnectionString);

    m_hostImport = new ServiceHost(dxs, queueAddress);

    ServiceEndpoint sep = m_hostImport.AddServiceEndpoint(typeof(IDataExchange), msmqbinding, queueAddress);

    //sep.Behaviors.Add(new TransactedBatchingBehavior(1));

    m_hostImport.Open();

  • Wednesday, November 01, 2006 4:01 AM
     
     

    Got it .

    Thanks , Dave and T.Ramesh