1

i am trying to inject a string into structuremap registry at run time. i have successfully done it with a static string. like this

For<TestDAL>().Use<TestDAL>().Ctor<string>("connectionString").Is("randomStringData");

but when i am trying to make the string dynamic at runtime i am not able to figure out how to send it, i have tried HttpContext and Session but they are always empty like the example below:

HttpContext.Current.GetOwinContext().Environment.TryGetValue("dynamicString", out object dynString);  

For<TestDAL>().Use<TestDAL>().Ctor<string>("connectionString").Is(dynString);

Please anyone has anyidea on how to do this?

wandos
  • 1,581
  • 2
  • 20
  • 39

1 Answers1

1

You're only evaluating the dynamicString completely upfront. You might need to do it lazy so it's evaluated just in time like so:

For<TestDAL>().Use<TestDAL>().Ctor<string>("connectionString").Is(() => {
    string dynString = null;

    HttpContext.Current.GetOwinContext()
        .Environment.TryGetValue("dynamicString", out object dynString); 

    return dynString;
});
pckill
  • 3,709
  • 36
  • 48
  • thank you for your help but this solution is not feasible since lambda expression with a statement body cannot be converted to an expression tree. and it wont work – wandos Mar 29 '17 at 11:05
  • Use the **other** overload that lets you supply a description as a string and a Func instead of the expression. – Jeremy D. Miller Mar 29 '17 at 14:26
  • Can you please give me an example on how to – wandos Mar 29 '17 at 20:07