编辑服务通过编程方式引用silverlight中的客户端配置文件

时间:2013-01-23 10:02:05

标签: visual-studio-2010 silverlight configuration xap

我正在Silverlight中创建一个应用程序。该应用程序的XAP文件夹包含ServiceReferencesClientConfig文件。我已经在Web服务器上部署了该应用程序,每当我从(http://192.168.1.15/SampleApplication/Login.aspx)等其他机器访问该网站时,我想写一下将IP地址(192.168.1.15)放入ServiceReferencesClientConfig中,然后将Xap文件下载到客户端。但我没有想到通过编程方式编辑ServiceReferencesClientConfig文件。 (我希望在更改部署了应用程序的Web服务器的IP地址时进行更改,它应该自动更改ServiceReferencesClientConfig,因此无需手动更改ServiceReferencesClientConfig文件。)

1 个答案:

答案 0 :(得分:1)

作为一个选项,您可以动态配置服务代理,更改默认构造函数以使用动态生成的端点和绑定,或使用工厂执行相同操作:

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;
    }
}