Solved

Overwrite InfoProperty of generic wcf client

Posted on 2012-04-13
1
330 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

734 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