WCF service - initialization service parameter issue
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: WCF service - initialization service parameter issue

  1. #1
    Join Date
    May 2012
    Location
    Earth!
    Posts
    9

    WCF service - initialization service parameter issue

    I am new to WCF. I am currently trying to create a WCF service and then host it from a commandLine application. During hosting of the service, I am trying to initialize the service with an instance of an object called MyProxy. All calls to the service should delegate to MyProxy.

    I have created a service and made calls. But I am not able to initialize the MyProxy instance in the service. It is always null. So when any calls to the service is made, I am not able to delegate it to the proxy.

    I have been trying to get this work since last two days with not much progress.
    Kindly help.

    Following is the code that I have tried.
    The service is in one component. The ServiceLauncher is in the host console application. MyProxy resides in a third component.
    Code:
    using System;
    using System.ServiceModel;
    using System.ServiceModel.Dispatcher;
    using System.ServiceModel.Channels;
    using System.ServiceModel.Description;
    using System.Collections.ObjectModel;
    
    namespace WcfConsoleApp
    {
        [ServiceContract]
        public interface IMasOperations
        {
            [OperationContract]
            CoAuthorSearchResult ExtractCoAuthorsFromAuthor(long AuthorCellId, uint LevelsToExtract);
        }
    
        public class MasOperationsService : IMasOperations
        {
            //This MyProxy instance should be used to delegate all calls to service.
            public MyProxy myProxyInstance;
    
            public MasOperationsService()
            {
                myProxyInstance = null;
            }
            public MasOperationsService(MyProxy proxy)
            {
                myProxyInstance = proxy;
            }
    
            public CoAuthorSearchResult ExtractCoAuthorsFromAuthor(long authorCellId, uint levelsToExtract)
        {
            //The service will delegate the call to MyProxy.
            //myProxyInstance is always null
    
            return myProxyInstance.GetProxyData();
        }
        }
    
        public class MyInstanceProvider : IInstanceProvider
        {
            public object GetInstance(InstanceContext instanceContext, Message message)
            {
                MyProxy name = message.Headers.GetHeader<MyProxy>("Name", "http://my.namespace");
                if (name != null)
                {
                    return new MasOperationsService(name);
                }
                return null;
            }
            public object GetInstance(InstanceContext instanceContext)
            {
                return new MasOperationsService(null);
            }
            public void ReleaseInstance(InstanceContext instanceContext, object instance)
            {
            }
        }
    
        public class MyServiceBehavior : IServiceBehavior
        {
            MyInstanceProvider myProvider = new MyInstanceProvider();
            public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters) { }
            public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
            {
                foreach (ChannelDispatcher cd in serviceHostBase.ChannelDispatchers)
                {
                    foreach (EndpointDispatcher ed in cd.Endpoints)
                    {
                        ed.DispatchRuntime.InstanceProvider = this.myProvider;
                    }
                }
            }
            public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) { }
        }  
    }
    This class will try to start and host the wcf service. It will also create a MyProxy class and try to initialize the service with this instance.
    Code:
    using System;
    using System.ServiceModel;
    using System.ServiceModel.Channels;
    
    namespace WcfConsoleApp
    {
        class Program
        {
            static void Main(string[] args)
            {
                MyServiceLauncher launcher = new MyServiceLauncher();
                launcher.StartService(new MyProxy());
            }
        }
    
        class MyServiceLauncher
        {
            ServiceHost host;
            IMasOperations proxy;
            ChannelFactory<IMasOperations> factory;
    
            public void StartService(MyProxy proxyInstance)
            {
                string baseAddress = "http://localhost:8730/Design_Time_Addresses/MASService/Service1";
                host = new ServiceHost(typeof(MasOperationsService), new Uri(baseAddress));
                host.AddServiceEndpoint(typeof(IMasOperations), GetBinding(), "");
                host.Description.Behaviors.Add(new MyServiceBehavior());
                host.Open();
                Console.WriteLine("Host opened");
    
                factory = new ChannelFactory<IMasOperations>(GetBinding(), new EndpointAddress(baseAddress));
                proxy = factory.CreateChannel();
    
                using (OperationContextScope scope = new OperationContextScope((IContextChannel)proxy))
                {
                    OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("Name", "http://my.namespace", proxyInstance));
                }
            }
    
            public void ShutDownService()
            {
                ((IClientChannel)proxy).Close();
                factory.Close();
                host.Close();
            }
    
            static Binding GetBinding()
            {
                BasicHttpBinding result = new BasicHttpBinding();
                return result;
            }
        }
    }
    This is the proxy
    Code:
    public class MyProxy
    {
        public CoAuthorSearchResult GetProxyData()
        {
             return new CoAuthorSearchResult();
        }
     }
    
     public class CoAuthorSearchResult
    { }
    Last edited by ShaQ.Blogs; August 24th, 2012 at 02:08 PM.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,201

    Re: WCF service - initialization service parameter issue

    Use a different ServiceHost constructor. Use the one that takes an object rather than a Type.

    Code:
    public void StartService(MyProxy proxyInstance)
    {
      string baseAddress = "http://localhost:8730/Design_Time_Addresses/MASService/Service1";
      host = new ServiceHost( new MasOperationsService( proxyInstance ), new Uri(baseAddress));
      ...
    }
    Also, set a breakpoint inside each of the MasOperationsService constructors to make sure the correct constructor is getting called.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center