Solved

Overwrite InfoProperty of generic wcf client

Posted on 2012-04-13
1
325 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Online collaboration is quickly becoming embedded in the workplace, and its benefits are tangible. See what the current landscape looks like and what the future holds for collaboration tools and the future of work.
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
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…
A short film showing how OnPage and Connectwise integration works.

914 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

19 Experts available now in Live!

Get 1:1 Help Now