0

This question is related to an older question:

Is it possible to specify proxy credentials in your web.config?

I created a custom web proxy class and using it in the section as shown in the above question's answer. I have a constructor in the custom web proxy class that accepts the proxy URL, username, password and domain values. However, I couldn't figure out how to have .NET call this parameterized constructor because I am not explicitly creating a web proxy object. I have added the following configuration setting in my web.config file

<system.net>
   <defaultProxy enabled="true" useDefaultCredentials="false">
       <module type="ISO.Prometrix.CommunityMitigation.Common.Impl.ProxyModule, 
          ISO.Prometrix.CommunityMitigation.Common"/>
   </defaultProxy>
</system.net>

I am using spring.net for DI. I added the object definition for my custom class in my spring config file but when I run the project it seems to always call the default constructor. In order to make this work, I am reading the proxy URI, username, password and domain from the config app settings inside my custom web proxy.

I would like to inject these values into my custom class from outside. Here is my code:

public class ProxyModule : IWebProxy
{
    private readonly string _username;
    private readonly string _password;
    private readonly string _domain;
    private readonly string _proxyAddress;

    public ProxyModule()
    {
        // Unable to figure out how to invoke the parameterized constructor on this 
        // class that's why doing this workaround.
        _username = ConfigurationManager.AppSettings["ProxyUsername"];
        _password = ConfigurationManager.AppSettings["ProxyPassword"];
        _domain = ConfigurationManager.AppSettings["ProxyDomain"];
        _proxyAddress = ConfigurationManager.AppSettings["ProxyAddress"];
    }

    public ProxyModule(
        string username,
        string password,
        string domain,
        string proxyAddress)
    {
        _username = username;
        _password = password;
        _domain = domain;
        _proxyAddress = proxyAddress;
    }

    public ICredentials Credentials
    {
        get
        {
            return new NetworkCredential(_username, _password, _domain);
        }

        set {}
    }

    public Uri GetProxy(Uri destination)
    {
        return new Uri(_proxyAddress);
    }

    public bool IsBypassed(Uri host)
    {
        return false;
    }
}
Community
  • 1
  • 1
manu79
  • 303
  • 1
  • 3
  • 12

1 Answers1

0

.NET will not allow you to control which instance will be used. The parameter set in the web.config is the type so you have no control over how it will be instantiated.

I suggest you create a configuration object and instantiate it through Spring. Then from inside the ProxyModule constructor retrieve that object calling Spring directly and configure the Proxy accordingly.

Alex
  • 1,366
  • 19
  • 22