Solved

Overwrite InfoProperty of generic wcf client

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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

623 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