none
svcutil vs ChannelFactory best practices and not generating DataContract that is used in messageheader RRS feed

  • Question

  • Am I the only one who doesn't know that svcutil doesn't seem to generate a datacontract for a datacontract that isn't referenced in teh service contract...

    It kind of makes sense, since I think the contract is "the boss" and owns all the "children messages and data contracts"... are there settings to get around this? (I'm dynamically adding messages via: OperationContext.Current.OutgoingMessageHeaders.Add(MyContextDataForCorrellation); and getting them back out in a similar manner - is there a better way?)  I'm taking some of thsi correlation data and shoving it into the "CurrentPrincipal" (creating a genericPrincipal for Roles purposes) is this a terrible idea does it matter that I'm flattening the "identity" of the server in doing this?

    While I'm on this point, is there any reason NOT to use shared assemblies and ChannelFactory<T> over the frustrations of "Update Service Reference" and having to be concious of IIS/IIS Express and its cache/memory locks... becasue the above issue was masked by me stuffing around with these things greatly, where as if I had just shared the assembly I would not have had this issue, however I would more likely have had runtime errors when things didn't serialize properly (lack of DataContract/Datamember attribues - maybe, I'm hypothesising/guessing)?!?

    so in summary:

    1) is there a "sure fire" way to check your "Add Service Reference" is looking at the version of "bits" that is in your VS IDE so you aren't chasing a "false" bug that just hasn't made it to the thing that svcutil is looking at?

    2) is my assumption of contracts aren't "svcutil"d unless they are referenced in the ServiceContract directly and if this is true then how should I be passing complex "Header" information if not via "GetHeader<MyContextDataForCorrellation>()"

    3) is there a better way to propogate correlation info (in particular the users details into "CurrentPrincipal"?

    4) is there "current thinking" of using ChannelFactory<T> vs "Add Service Referece" (i've read various things about how they are pretty much the same thing, so I'm thinking purely from a DEVELOPMENT workflow/ease the pain perspective here).

    I've read things like this:http://blogs.msdn.com/b/wenlong/archive/2007/10/26/best-practice-always-open-wcf-client-proxy-explicitly-when-it-is-shared.aspx and other posts on these forums but just want to know if these things are a) still valid and b) are there better/lessons learned for how to work with these technologies.

    Thanks in advance.


    Bazinga!

    Tuesday, December 9, 2014 7:12 AM

Answers

  • Hi Jieng,

    Thanks for the info, unfortunately I was aware of all of that, good to know that I agree/knew it tho (just incase I was going off the rails :), but you haven't really addressed my 4 issues... :( Although I guess you may have answered #4 - if you are saying that the Development ease is "different" depending on what you are going for... eg: svcutil is EASIER if you just want to add the service client and not have to worry much more about it, but ChannelFactory is easier if you need to manipulate or "do more stuff" with the service connection... like im wanting to add message headers in my current project...

    If that is so then

    a) do you have any input in the other 3 issues?

    b) do you think its fair to say that (in general) if you control both ends (client and service) then ChannelFactory is the way to go? or are there problems/gotchas to this approach?

    Thanks


    Bazinga!

    • Marked as answer by noJedi Sunday, December 21, 2014 11:50 AM
    Wednesday, December 10, 2014 1:06 PM
  • yes, We can use the ChannelFactory can control both ends (client and service). If the client has no config file, in this way we have to use the ChannelFactory.
    • Proposed as answer by Mankdng Nef Friday, December 19, 2014 4:05 AM
    • Marked as answer by noJedi Sunday, December 21, 2014 11:50 AM
    Friday, December 19, 2014 4:05 AM
  • I wrote a blog about 2 years ago, about a generic WCF Proxy class that I wrote using the ChannelFactory methodology. It works great and is easy to use. I may be using a slightly different version than what I posted (can't remember off the top of my head, I'd have to look at my production code). I wrote the class because I didn't like having to add Service References.

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    • Marked as answer by noJedi Sunday, December 21, 2014 11:50 AM
    Friday, December 19, 2014 3:34 PM
    Moderator

All replies

  • A client created through a service reference is something like a "ready to go", "out of the box" solution. It's extremely easy to use and simplifies getting started with WCF.

    Svcutil is simply the command-line version of "Add service reference".

    Using a ChannelFactory is slightly more effort e.g. to instantiate one you have to specifiy its configuration manually. Under the hood, a client created through the service reference does all that stuff for you. A Channelfactory, however, is more flexible and efficent. I personally only use ChannelFactories because they allow me to inject them into a component for unit testing (with some wrappers). Furthermore, creating a ChannelFactory is quite expensive. Because of that a client caches those factories. As for me I prefer to control the lifecycle of the factory explicitly. It makes me more aware about when/how the factories are created. In order to use a ChannelFactory, you would still add a service reference (or through svcutil).
    • Proposed as answer by Mankdng Nef Friday, December 19, 2014 4:06 AM
    Wednesday, December 10, 2014 6:11 AM
  • Hi Jieng,

    Thanks for the info, unfortunately I was aware of all of that, good to know that I agree/knew it tho (just incase I was going off the rails :), but you haven't really addressed my 4 issues... :( Although I guess you may have answered #4 - if you are saying that the Development ease is "different" depending on what you are going for... eg: svcutil is EASIER if you just want to add the service client and not have to worry much more about it, but ChannelFactory is easier if you need to manipulate or "do more stuff" with the service connection... like im wanting to add message headers in my current project...

    If that is so then

    a) do you have any input in the other 3 issues?

    b) do you think its fair to say that (in general) if you control both ends (client and service) then ChannelFactory is the way to go? or are there problems/gotchas to this approach?

    Thanks


    Bazinga!

    • Marked as answer by noJedi Sunday, December 21, 2014 11:50 AM
    Wednesday, December 10, 2014 1:06 PM
  • yes, We can use the ChannelFactory can control both ends (client and service). If the client has no config file, in this way we have to use the ChannelFactory.
    • Proposed as answer by Mankdng Nef Friday, December 19, 2014 4:05 AM
    • Marked as answer by noJedi Sunday, December 21, 2014 11:50 AM
    Friday, December 19, 2014 4:05 AM
  • I wrote a blog about 2 years ago, about a generic WCF Proxy class that I wrote using the ChannelFactory methodology. It works great and is easy to use. I may be using a slightly different version than what I posted (can't remember off the top of my head, I'd have to look at my production code). I wrote the class because I didn't like having to add Service References.

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    • Marked as answer by noJedi Sunday, December 21, 2014 11:50 AM
    Friday, December 19, 2014 3:34 PM
    Moderator