Search code examples
visual-studio-2010silverlightconfigurationxap

Edit Service References client config file in silverlight by programmatcally


i am making an application in silverlight.The XAP folder of that application contains ServiceReferencesClientConfig file.I have deployed that application on webserver and whenever i am accessing that website from other machine like (http://192.168.1.15/SampleApplication/Login.aspx), I want to write that IP address(192.168.1.15) into ServiceReferencesClientConfig and after that the Xap file should be downloaded to client side. But i am not getting idea of editing the ServiceReferencesClientConfig file by programmatically. (I want to do that change as i change the IP address of webserver on which application is deployed, it should change the ServiceReferencesClientConfig automatically so there is no need to to change ServiceReferencesClientConfig file manually.)


Solution

  • As an option, you can configure your service proxies dinamically, changing the default constructor to use dinamically generated endpoints and bindings, or using a factory to do the same:

    public MyService()
            : base(ServiceEx.GetBasicHttpBinding(), ServiceEx.GetEndpointAddress<T>())
    {
    }
    
    
    
    public static class ServiceEx
    {
        private static string hostBase;
        public static string HostBase
        {
            get
            {
                if (hostBase == null)
                {
                    hostBase = System.Windows.Application.Current.Host.Source.AbsoluteUri;
                    hostBase = hostBase.Substring(0, hostBase.IndexOf("ClientBin"));
                    hostBase += "Services/";
                }
                return hostBase;
            }
        }
    
        public static EndpointAddress GetEndpointAddress<TServiceContractType>()
        {
            var contractType = typeof(TServiceContractType);
    
            string serviceName = contractType.Name;
    
            // Remove the 'I' from interface names
            if (contractType.IsInterface && serviceName.FirstOrDefault() == 'I')
                serviceName = serviceName.Substring(1);
    
            serviceName += ".svc";
    
            return new EndpointAddress(HostBase + serviceName);
        }
    
        public static Binding GetBinaryEncodedHttpBinding()
        {
            // Binary encoded binding
            var binding = new CustomBinding(
               new BinaryMessageEncodingBindingElement(),
               new HttpTransportBindingElement()
               {
                   MaxReceivedMessageSize = int.MaxValue,
                   MaxBufferSize = int.MaxValue
               }
            );
    
            SetTimeouts(binding);
    
            return binding;
        }
    
        public static Binding GetBasicHttpBinding()
        {
            var binding = new BasicHttpBinding();
            binding.MaxBufferSize = int.MaxValue;
            binding.MaxReceivedMessageSize = int.MaxValue;
    
            SetTimeouts(binding);
    
            return binding;
        }
    }