locked
Interface Properties in a WCF Service - Can properties be used in a service? RRS feed

  • Question

  •  

    Hi All,

     

    I have a WCF Service Contract with a simple interface defining a few methods and properties. When using the Service I would like the client side object to set a few properties and then call methods as needed (my instances are per session). This way they don't have to pass a ton of parameters in the methods. Is this possible? So far only the methods affixed with the "OperationContract" attribute are exposed.

     

    A possible solution to my problem is to have an InitialMethod that passes parameters which are then set to private members on the server instance. All other methods can then reference those private members. However I wonder if the latter is possible.

     

    Thanks for any info on this.

     

    Code Ex:

     

    So instead of:

     

    MyFTPService.SendFile(host, user, pass, fileToSend);

    MyFTPService.GetFIle(host, user, pass, fileToGet);

     

    I would like:

    MyFTPService.Host = "177.77.77.7";

    MyFTPService.User = "user";

    MyFTPService.PW = "pw";

    MyFTPService.SendFile(fileToSend);

    MyFTPService.GetFIle(fileToGet);

     

    My Workaround:

    MyFTPService.Initialize(host, user, pw);

    MyFTPService.SendFile(fileToSend);

    MyFTPService.GetFIle(fileToGet);

    Thursday, April 17, 2008 6:34 PM

Answers

  • Properties are not exposed by WCF on a service contract. They only handle methods.

    One thing you coud do is to make sure you set properties on the connection or in the headers so that you can pull those out service side.

    You set the properties on your proxy or channel. If you use the generated ClientBase<T> proxy, you can set username and password on the ClientCredentials property. Once that is set, the credentials can flow to the service and be accessed service side without having to pass it as an operation argument.

    If you defined headers in the messages for the Host property you show above, you could set a property on your proxy and have that get transferred behind the scenes in the message header.


    Sure its a little extra work but you don't tie yourself to a sessionful service and you keep your operations cleaner.
    Thursday, April 17, 2008 7:16 PM