none
how to pass parameterst to a Service? RRS feed

  • Question

  • Have a WCF ServiceLibrary, and the following code, instantiated with the following code:

                    serviceType = typeof(Service1);
                    IPAddress ip = Dns.GetHostAddresses(Dns.GetHostName()).Where(address => address.AddressFamily == AddressFamily.InterNetwork).First();
                    String port = ImplementationLayer.Properties.AppSettings.Default.APIPort;
                    if (!port.Equals(""))
                    {
                        hostAddress = "http://" + ip + ":" + port + "/CommunicationLayer/";
                        Uri baseAddress = new Uri(hostAddress);
    
                        if (host == null)
                        {
                            host = new ServiceHost(serviceType, baseAddress);
                            host.Open();
                        }
                    }
    At the point where this class is instantiated, I need to pass a class reference in as well, in order for the methods in my my Service1 to have access to this particular class. How do I do this? Say my class is CallModel;

    Thursday, October 10, 2013 2:11 PM

Answers

  • So what you need, is what I told you 4 hours ago :-)

        [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single, InstanceContextMode = InstanceContextMode.Single)]
        public class Service1 : IMyServiceContract
        {
            private IMyObject myObject;
    
            public Service1(IMyObject myObject)
            {
                this.myObject; = myObject;
            }
    
            public static void StartWcfSample()
            {
                Uri httpAddr = new Uri(whatever);
    	    var instance = new Service1(new MyObject());
                host = new ServiceHost(instance, httpAddr);
                host.Open();
            }
    
       ...

    The difference - from what you do - is that I give the host a concrete instance - not just a type.

    Btw, I would always use dependency injection.

    /Peter


    Friday, October 11, 2013 10:38 AM

All replies

  • Hi,

    Welcome to MSDN Forum!

    I have moved this thread to WCF forum for better response.

    Thanks for your understanding.

    Best regards,
    Franklin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, October 11, 2013 5:47 AM
  • Hi,

    I can't see how you configured the service-contract, but if you can live with a single instance of your service, you should set InstanceContextMode to Single and create the service yourself:

    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single, IncludeExceptionDetailInFaults = true, InstanceContextMode = InstanceContextMode.Single)]
    public class MyService{}
    
    

    -and in the service class (or somewhere else), create the service using the already created instance:

    var host = new ServiceHost(this, baseAddress);

    Hope this is what you need :-)

    /Peter

    Friday, October 11, 2013 6:31 AM
  • Hello Peter,

    This is not quite what I asked...

    I have a class called CallModel. This class is instantiated in my WPF project, and I would like to pass a reference parameter to this class, to my WCF Service1 class.

    I was wondering whether this is possible.

    Friday, October 11, 2013 7:29 AM
  • I have a class called CallModel. This class is instantiated in my WPF project, and I would like to pass a reference parameter to this class, to my WCF Service1 class.
    Please show me a sample...
    How would you like to pass the reference - in the constructor (var host = new ServiceHost(new Service1(new CallModel)), baseAddress);) or when a call is received or ??


    Friday, October 11, 2013 7:36 AM
  • OK, In the code snippet that I posted above, I would like to pass some parameter (CallModel object) to the Service1 that is called:

    serviceType = typeof(Service1(----CallModel----));


    So, how can I make Service1 so that it will take a CallModel parameter? 

    When i try to add this parameter to the Service1 call, I get an error msg, saying that a method name is required....

    • Edited by harriet_001 Friday, October 11, 2013 10:24 AM
    Friday, October 11, 2013 10:20 AM
  • So what you need, is what I told you 4 hours ago :-)

        [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single, InstanceContextMode = InstanceContextMode.Single)]
        public class Service1 : IMyServiceContract
        {
            private IMyObject myObject;
    
            public Service1(IMyObject myObject)
            {
                this.myObject; = myObject;
            }
    
            public static void StartWcfSample()
            {
                Uri httpAddr = new Uri(whatever);
    	    var instance = new Service1(new MyObject());
                host = new ServiceHost(instance, httpAddr);
                host.Open();
            }
    
       ...

    The difference - from what you do - is that I give the host a concrete instance - not just a type.

    Btw, I would always use dependency injection.

    /Peter


    Friday, October 11, 2013 10:38 AM
  • what is dependency injection?
    Friday, October 11, 2013 12:03 PM
  • what is dependency injection?

    That's another question, but you may read more about it here: http://stackoverflow.com/questions/130794/what-is-dependency-injection.

    It doesn't help you here since it's just a conceptual thing...

    Friday, October 11, 2013 12:16 PM