Limiting access to fields for web service client

Discussion in 'ASP .Net' started by dev, Sep 10, 2006.

  1. dev

    dev Guest

    Hello,

    I have a web service that returns an entity (C# class). I would like to
    disallow access to some of the members in write access to the WebService
    clients. Setting them private or protected in the class definition can
    not be achieved since in the WebService they must be writeable.

    Dummy example to explain if it is not clear:

    In WebService definition:

    // ...
    class MyClass
    {
    public int id;
    public int name;
    };

    // ...
    MyClass WebService_GetMyClass()
    {
    MyClass mc = new MyClass();
    mc.id = 1; // id must be public
    mc.name = "foo";
    return mc;
    }

    void WebService_UpdateMyClass(MyClass mc)
    {
    // ...
    }

    WebService client:

    MyClass mc = WS.WebService_GetMyClass();
    mc.id = 22; // <== this is what I want to avoid *******
    WS.WebService_UpdateMyClass(mc);


    Thanks for your answers.
    dev, Sep 10, 2006
    #1
    1. Advertising

  2. Hi,

    dev wrote:
    > Hello,
    >
    > I have a web service that returns an entity (C# class). I would like to
    > disallow access to some of the members in write access to the WebService
    > clients. Setting them private or protected in the class definition can
    > not be achieved since in the WebService they must be writeable.


    <example snipped>

    Difficult. As you found out, the fields (be it attributes or properties)
    of the returned classes must be public, or else they won't even be
    passed in the SOAP response.

    I would use an intermediate layer performing the request, and then
    mapping the returned values into a client class, with the desired
    visibility.

    Something like

    public class MyClassEx
    {
    private int id;
    private string name;

    public string Name
    {
    get
    {
    return name;
    }
    }

    public MyClassEx()
    {
    MyClass mc = WS.WebService_GetMyClass();
    this.id = mc.id;
    this.name = mc.name:
    }
    }

    Note however that this only works if the web service call is
    synchronous, which is not really recommended. If the call is
    asynchronous, you'll need to modify the code.

    HTH,
    Laurent
    --
    Laurent Bugnion, GalaSoft
    Software engineering: http://www.galasoft-LB.ch
    PhotoAlbum: http://www.galasoft-LB.ch/pictures
    Support children in Calcutta: http://www.calcutta-espoir.ch
    Laurent Bugnion, Sep 10, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Taylor

    Limiting access to user files

    Dave Taylor, Apr 2, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    372
    Kevin Spencer
    Apr 2, 2004
  2. Jp Calderone
    Replies:
    0
    Views:
    920
    Jp Calderone
    Jun 28, 2005
  3. dev
    Replies:
    1
    Views:
    355
    Joerg Jooss
    Sep 19, 2006
  4. Replies:
    0
    Views:
    201
  5. John Sedlak

    Limiting a WCF Service to "On" Domain Apps

    John Sedlak, Apr 24, 2008, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    110
    John Sedlak
    Apr 24, 2008
Loading...

Share This Page