none
Need help on Self Hosting Using channel Factory RRS feed

  • Question

  • Hello,

         Need Help.

         I am trying to self host WCF service using cutom Channel Factory class. but I am getting exception.

     The scenario is something like below this  : -

    I want to create the endpoint connection to WCF service. I want to achieve this  using custom channel factory(not using default channel factory API provided in .Net Framework).

    I have written one class and it is derived from channel factory and we are reading the service endpoints, behaviors, bindings from external configuration file added in the project.

    1. I am able to connect to server side service hosted on different machine using following sample code successfully.
    2. In the case of “self-hosting”  I am receiving following error : --

         "There was an error reading from the pipe: The pipe has been ended. (109, 0x6d)."

    Below is the code which I have written for self hosting WCF service using custom channel factory.

    // Method where I am calling CustomChannel Factory constructor

    internal void ProxyCall(string msgEndpointConfigurationName, string url)
      {
                string appConfigDirName, extConfigFilePath;
                FileInfo configFileInfo = new FileInfo(appConfigName);
                appConfigDirName = configFileInfo.DirectoryName;
                extConfigFilePath = appConfigDirName + "\\" + extConfigFileName;
       
                this.messageEndpointConfigurationName = msgEndpointConfigurationName;
           
                if (string.IsNullOrEmpty(url))
                {
                    this.messageFactory = new CustomChannelFactory<IServerOperationsMessage>(extConfigFilePath, this.messageEndpointConfigurationName);
                }
                else
                {
                   //EndpointAddress e = new EndpointAddress(url + ClientToServerOperations.ServiceAddress);
                   //this.messageFactory = new ChannelFactory<IServerOperationsMessage>(this.messageEndpointConfigurationName, e);

                   this.messageFactory = new CustomSelfHostingChannelFactory<IServerOperationsMessage>(extConfigFilePath, this.messageEndpointConfigurationName, url + ClientToServerOperations.ServiceAddress);

                   //this.messageFactory = new CustomSelfHostingChannelFactory<IServerOperationsMessage>(extConfigFilePath, this.messageEndpointConfigurationName, e);
                  
                }

       ClientMessageFormatterBehavior formatterBehavior = new ClientMessageFormatterBehavior();
       foreach (OperationDescription operation in this.messageFactory.Endpoint.Contract.Operations)
       {
        if (!operation.Behaviors.Contains(typeof(ClientMessageFormatterBehavior)))
        {
         operation.Behaviors.Add(formatterBehavior);
        }
       }

       this.messageFactory.Endpoint.Behaviors.Add(new MessageStatisticsBehavior());
       this.messageFactory.Open();
      }

    // Custom Channel Factory Class

    internal class CustomSelfHostingChannelFactory<T> : ChannelFactory<T>
        {
            /// Custom client channel. Allows to specify a different configuration file
            /// <typeparam name="T"></typeparam>

            private string configurationPath, selectedMsgEndPoint, uriAddress;

            public PPCCustomSelfHostingChannelFactory(string configurationPath, string selectedEndPoint, string uriAddress)
                : base(typeof(T))
            {
                this.configurationPath = configurationPath;
                this.selectedMsgEndPoint = selectedEndPoint;
                this.uriAddress = uriAddress;
                EndpointAddress e = new EndpointAddress(this.uriAddress);
                base.InitializeEndpoint(selectedEndPoint,e);
            }

            //As you can see, a call to the method InitialiazeEndpoint of the base class is required.
            //That method will automatically call to our CreateDescription method to configure the service endpoint.
            ///Loads the serviceEndpoint description from the specified configuration file
            protected override ServiceEndpoint CreateDescription()
            {
                ServiceEndpoint serviceEndpoint = base.CreateDescription();
               
                return serviceEndpoint;
            }

            private EndpointIdentity GetIdentity(IdentityElement element)
            {
                EndpointIdentity identity = null;
                PropertyInformationCollection properties = element.ElementInformation.Properties;
                if (properties["userPrincipalName"].ValueOrigin != PropertyValueOrigin.Default)
                {
                    return EndpointIdentity.CreateUpnIdentity(element.UserPrincipalName.Value);
                }
                if (properties["servicePrincipalName"].ValueOrigin != PropertyValueOrigin.Default)
                {
                    return EndpointIdentity.CreateSpnIdentity(element.ServicePrincipalName.Value);
                }
                if (properties["dns"].ValueOrigin != PropertyValueOrigin.Default)
                {
                    return EndpointIdentity.CreateDnsIdentity(element.Dns.Value);
                }
                if (properties["rsa"].ValueOrigin != PropertyValueOrigin.Default)
                {
                    return EndpointIdentity.CreateRsaIdentity(element.Rsa.Value);
                }
                return identity;
            }

            private Binding GetBinding(IBindingConfigurationElement configurationElement)
            {
                if (configurationElement is CustomBindingElement) return new CustomBinding();
                else if (configurationElement is BasicHttpBindingElement) return new BasicHttpBinding();
                else if (configurationElement is NetMsmqBindingElement) return new NetMsmqBinding();
                else if (configurationElement is NetNamedPipeBindingElement) return new NetNamedPipeBinding();
                else if (configurationElement is NetPeerTcpBindingElement) return new NetPeerTcpBinding();
                else if (configurationElement is NetTcpBindingElement) return new NetTcpBinding();
                else if (configurationElement is WSDualHttpBindingElement) return new WSDualHttpBinding();
                else if (configurationElement is WSHttpBindingElement) return new WSHttpBinding();
                else if (configurationElement is WSFederationHttpBindingElement) return new WSFederationHttpBinding();
                return null;
            }

            private Binding CreateBinding(string bindingName, ServiceModelSectionGroup group)
            {
                BindingCollectionElement bindingElementCollection = group.Bindings[bindingName];
                if (bindingElementCollection.ConfiguredBindings.Count > 0)
                {
                    IBindingConfigurationElement be = bindingElementCollection.ConfiguredBindings[0];
                    Binding binding = GetBinding(be);
                    if (be != null)
                    {
                        be.ApplyConfiguration(binding);
                    }
                    return binding;
                }
                return null;
            }

            private void AddBehaviors(string behaviorConfiguration, ServiceEndpoint serviceEndpoint, ServiceModelSectionGroup group)
            {
                EndpointBehaviorElement behaviorElement = group.Behaviors.EndpointBehaviors[behaviorConfiguration];
                for (int i = 0; i < behaviorElement.Count; i++)
                {
                    BehaviorExtensionElement behaviorExtension = behaviorElement[i];
                    object extension = behaviorExtension.GetType().InvokeMember("CreateBehavior", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, behaviorExtension, null);
                    if (extension != null)
                    {
                        serviceEndpoint.Behaviors.Add((IEndpointBehavior)extension);
                    }
                }
            }
        }

    Thanks in advance !! :-)


    • Edited by MSR09 Monday, February 23, 2015 12:08 PM
    Monday, February 23, 2015 11:44 AM

Answers