Skip to content

Instantly share code, notes, and snippets.

@rahulsahay19
Created February 27, 2017 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rahulsahay19/791980d94953132d4fd1278fb718e2ea to your computer and use it in GitHub Desktop.
Save rahulsahay19/791980d94953132d4fd1278fb718e2ea to your computer and use it in GitHub Desktop.
CreateGenericFactory
private static object CreateGenericFactory(Type t)
{
Type factoryType = typeof(ChannelFactory<>);
factoryType = factoryType.MakeGenericType(t);
//Call the Binding section to get the associtaed binding
var section = GetBindingsSection();
Binding binding = null;
string bindingName = null;
List<string> contractCollection;
object address;
ClientSection clientSection;
var addressCollection = ApplyAddressCollection(out contractCollection, out address, out clientSection);
//Loop through the address collection and check for required contract
for (int k = 0; k < addressCollection.Count; k++)
{
//Check for contract, if found, then pull corresponding address
if (contractCollection[k].Contains(t.FullName))
{
address = addressCollection[k].AbsoluteUri;
bindingName = clientSection.Endpoints[k].Binding;
//Now loop through collected binding name and check for configured bindings
binding = ApplyBindingConfiguration(section, bindingName, binding);
}
}
var factory = Activator.CreateInstance(factoryType, binding, address);
ApplyEndPointBehaviors(factory);
MethodInfo method = factoryType.GetMethod("CreateChannel", new Type[] { });
return method.Invoke(factory, null);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment