Passing Database Credentials in WCF Header RRS feed

  • Question

  • I have a Silverlight application which will connect with different database depending on the Company selected by the User from combo box, this means that i have to make database connection dynamic i.e. passing all database credentials to WCF services which make connection with the database using Entity Framework.

    I dont want all data contracts contain some properties which sends database info on every WCF call because this is just a redundancy. So i decided to use WCF header for this..? Can someone give me an idea How to do this..? 

    Note: I have to do this with Plain WCF not RIA SERVICES or Other...!!!

    Your any feedback can help me to solve this problem....!!! Thanx

    Friday, October 7, 2011 5:43 AM


All replies

  • You can refer URL - http://blogs.microsoft.co.il/blogs/bursteg/archive/2006/04/23/141.aspx which gives information how to pass and extract message from message header.

    Friday, October 7, 2011 9:39 AM
  • Wasim i have seen this post earlier but the problem is if i will use this approach then i have to repeat the using OperationContextScope and ChannelFactory on each and every call that will be make from my client application and i think this is only redundant code..!! I think i have to use IClientMessageInspector and IDispatchMessageInspector to modify the header before send to the service and fetch it after receiving it in service respectively.
    Friday, October 7, 2011 12:45 PM
  • Adding this data to the message header using a MessageInspector would work best for your case. Use this as a reference. My only recommendation for you will be that you create the classes that implement IClientMessageInspector to do what you need to do, but hooking up the inspector should all be done through configuration only, and don't resort to code to do this hooking. I mean, do create the custom BehaviorExtensionElement too, as given in the link. It will save you a lot of trouble and will offer a lot of flexibility going forward.
    Friday, October 7, 2011 2:34 PM
  • Saurabh Thanx for your link it was very nice unabke to solve my problem.

    I have made a sample project just for implementing a test case for my requirement. I added two projects in my solution One is WCF application (service) and other in console application (client). In client I added two classes one Implements

    BehaviorExtensionElement and IEndpointBehavior and other implements IClientMessageInspector it works fine i.e. my message passing in header and i am getting this message in my service project as well.

    Now problem is if i am trying to do the same thing in Silverlight project i am unable to implement BehaviorExtensionElement class in my project because it is i framework 4.0 (i guess) and my silverlight application only shows framework 2 assemblies. How to i do this...?

    • Edited by Raza Haider Monday, October 10, 2011 7:02 AM
    Monday, October 10, 2011 6:59 AM
  • Which version of Silverlight are you using?
    Monday, October 10, 2011 1:20 PM
  • Silverlight 4....!!
    Monday, October 10, 2011 6:15 PM
  • Hi Raza,

    Most of the time custom header is the best option for passing additional parameters. I would suggest you to read my blog posts on the following topic that will guide you in using Silverlight client and Custom Headers (WCF).

    1. Client Additional Parameters using Custom Headers in WCF
    2. WCF Custom Header with Silverlight

    Hope this helps. Please feel free to discuss further.

    Adil Mughal - http://AdilMughal.com
    • Proposed as answer by Adil Mughal Tuesday, October 18, 2011 5:30 PM
    • Marked as answer by Raza Haider Wednesday, October 19, 2011 10:25 AM
    Tuesday, October 18, 2011 5:30 PM
  • Thanx Adil after a long long time finally you made my day....!!! Thanx again..!!
    Wednesday, October 19, 2011 10:26 AM