title: How to bind IConfiguration to class having parameters in constructor
body: <p>I am using standard configuration pattern for ASP.NET Core applications and I can not bind configuration to my class as it has construtor with parameters.</p>
  
  <p>In appsettings.json I included desired config:</p>
  
  <pre><code>    "MyServiceConfig": {
        "Identity": {
          "Version": "1.0",
          "ComplicatedUri": {
            "Scheme": "http",
            "Authority": "localhost",
            "Path": "SuperService"
          }
        }
      },
  </code></pre>
  
  <p>My config class and it's dependencies look like that:</p>
  
  <pre><code>        public class MyServiceConfig
          {
              public MyIdentity Identity { get; set; }
          }
  
          public class MyIdentity
          {
              public string IdentityName { get; set; }
              public string Version { get; set; }
              public MyComplicatedUri ComplicatedProperty { get; set; }
  
              public MyIdentity(string version, MyComplicatedUri complicatedProperty)
              {
                  Version = version;
                  ComplicatedProperty = complicatedProperty;
                  IdentityName = complicatedProperty.Path;
              }
          }
  
          public class MyComplicatedUri
          {
              public string Scheme { get; set; }
              public string Authority { get; set; }
              public string Path { get; set; }
          }
  </code></pre>
  
  <p>I have already tried code like that:</p>
  
  <pre><code>        private MyServiceConfig GetMyConfig(IConfiguration configuration)
          {
              var config = new MyServiceConfig();
              configuration.GetSection("MyServiceConfig").Bind(config);
              return config;
          }
  </code></pre>
  
  <p>It throws exception:</p>
  
  <pre><code>'Cannot create instance of type 'MyIdentity' because it is missing
  a public parameterless constructor.'
  </code></pre>
  
  <p>That behaviour can make sense in some cases but in that particular one not so much. Mappings could be straightforward - by property names which have public setters or by constructor parameter names. </p>
  
  <p>Another idea would be adding converter in AddJsonOptions in Startup class for my types - IConfiguration.Bind could infer how to construct it but I also tried that with no success.</p>
  
  <p>Have you encoutered similar problems and found some reasonable solution to that?</p>
  
  <p><strong>Edit:</strong> Adding parameterless constructor will work of course, but sometimes I need to deal with some classes from external packages I'd like to use as parts of my config class so let's assume we can not modify them. I'd like to avoid adding new types for mapping only as well. Ideally I'd like to force ASP.NET Core engine to use existing constructor with parameters and by parameter name map with json properties - which currently is not working.</p>