Solved

Overwrite InfoProperty of generic wcf client

Posted on 2012-04-13
1
323 Views
Last Modified: 2012-06-27
Hi all
following question:
How do I overwrite a property of a instance of a wcf client which is instantiated as follow:
public abstaract class Manager<T> : IDisposable where T : class, ICommunicationObject, new()
{
    protected T wcfClient;
    public ConnectionManager()
    {
            wcfClient = new T();
    }
}

Open in new window

The reason I'm using the Manager is because I have several services which I use and wanted to centralize dispose and create method...

Now the Manager code is run as user@domain but the wcfClient must authenticate on the server side with otherUser@otherDomain which forces me to do some thing like:

check this: Impersonate a totally different Windows User in a client

Now I wanted to keep the Manager and add something like
Type cType = wcfClient.GetType().getBaseType;

PropertyInfo[] props = cType.GetProperty("ClientCredentials")
							.PropertyType.GetProperty("Windows")
							.PropertyType.GetProperty("ClientCredential")
							.PropertyType.GetProperties();


foreach (PropertyInfo p in props)
{
    if (p.Name.Equals("Domain"))
    {	
	/*
	 * How do I instantiate the the outerprop ?? */
	object outerProp = p.PropertyType.GetConstructor(new Type[] { }).Invoke(new object[] { });
        if (p.CanWrite)
        {
            p.SetValue(outerProp, "otherDomain", null);
        }
    }
	
	if (p.Name.Equals("UserName"))
    {	
	/*
	* How do I instantiate the the outerprop ?? */
	object outerProp = p.PropertyType.GetConstructor(new Type[] { }).Invoke(new object[] { });
        if (p.CanWrite)
        {
            p.SetValue(outerProp, "username", null);
        }
    }
	
	if (p.Name.Equals("Password"))
    {	
	/*
	* How do I instantiate the the outerprop ?? */
	object outerProp = p.PropertyType.GetConstructor(new Type[] { }).Invoke(new object[] { });
        if (p.CanWrite)
        {
            p.SetValue(outerProp, "xxxxxx", null);
        }
    }
}

Open in new window

to add/overwrite the client credential but got stuck.
How do I instantiate the outerProp to correctly use the SetValue method on the specific InfoProperty of the wcfClient.

Or is there a way to get the wcfClient instance knowing the type?

Any better way to reset/overwrite the wcfClient credential could also be a solution to my problem...

Thanks for your support
th*
0
Comment
Question by:thomasbau65
1 Comment
 
LVL 1

Accepted Solution

by:
thomasbau65 earned 0 total points
ID: 37850081
Well then
As state before "one is never better served as by one self" :-)
Here my solution:

Fist the property I wanted to overwrite is not writable at run time.
Second I don't need to go to the base property using :
PropertyInfo[] props = cType.GetProperty("ClientCredentials")
							.PropertyType.GetProperty("Windows")
							.PropertyType.GetProperty("ClientCredential")
							.PropertyType.GetProperties();

Open in new window

I only have to instantiate the new "ClientCredentials" and try to replace it on the dynamically created object...
ClientCredentials cred = new ClientCredentials();
    cred.Windows.ClientCredential.Domain = "otherDomain";
    cred.Windows.ClientCredential.UserName = "username";
    cred.Windows.ClientCredential.Password = "xxxxxx";
    cred.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;

	object oclient = Activator.CreateInstance(wcfClient.GetType());
    PropertyInfo credProp = oclient.GetType().GetProperty("ClientCredentials");
    if (credProp.CanWrite)
    {
        credProp.SetValue(client, cred, null);
    }

Open in new window

But as I bitterly found out the "credProp" is not writable and thus can't be replaced
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

New Relic recently released its Synthetics product that allows for the creation of performance monitors that periodically test a site's performance. If you wish to test an interactive workflow New Relic employs Selenium WebDriverJS to run those test…
To properly understand GitHub, let’s divide it into two words ‘Git’ and ‘Hub’. Git is basically a ‘Distribution Version Control’ (DVC) and ‘Source Code Management’ (SCM) system widely used by software programmers while Hub means the efficient centre…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now