How to properly define a variable to be used in multiple webmethods?

Discussion in 'ASP .Net Web Services' started by Jeffery Tyree, Oct 17, 2005.

  1. The webservice code:

    public class WebService1:yadayada
    {
    public string sFN;

    [WebMethod]
    public void CreateFile (string strFileName)
    {
    sFN = "C:\\inetpub\\" + strFileName;
    }

    [WebMethod]
    public void WriteFile(string strText)
    {
    using (StreamWriter sw = File.AppendText(sFN))
    {
    sw.WriteLine(strText);
    }
    }
    }

    Although "sFN" is defined even as public within the class and assigned a
    value in CreateFile, the variable goes NULL when it is used in WriteFile.
    The client application does not use this variable in its code. How do I
    properly define "sFN" so that it can be used in multiple webmethods?

    TIA,
    -Jeff
    Jeffery Tyree, Oct 17, 2005
    #1
    1. Advertising

  2. Jeffery Tyree

    Peter Kelcey Guest

    Jeff

    The issue isn't caused by your code. Your problem is caused by the fact
    that web services are stateless are you are trying to use them as if
    they are stateful. With the design that you have, you are making two
    separate calls to your web service. The web service will not maintain
    the state of you class variable between calls. Each time you call the
    web service, the class is instantiated again and all variables are
    created new.

    You can get hack your way around this by storing the value of
    strFileName in a session variable. (which can be maintained between
    calls if you want). However I would recommend that you take the time
    now to re-think you overall design and use some service design best
    practices.

    With web services, you need to put assigned the old "CRUD" method
    (Create, Read, Update and Delete) and you need to begin to design you
    methods based on a service approach. CRUD results in "chatty" code
    that doesn't work well within a services based world.

    Best practices for services design state that your service should
    accept all of the parameters that it needs to complete its own work and
    shouldn't rely on information sent to other methods. Under this
    design principle, your method would like the following

    [WebMethod]
    public void WriteFile(string, strFileName, string strText)
    {
    using (StreamWriter sw = File.AppendText(sFN))
    {
    sw.WriteLine(strText);
    }
    }

    I'd also recommend the following article that reviews some patterns
    to follow when designing your services.

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/SOADesign.asp

    MSDN has lots of other useful articles about designing web services
    that you can check out.

    Hope that helps,

    Peter Kelcey
    Peter Kelcey, Oct 17, 2005
    #2
    1. Advertising

  3. Peter-

    Thank you very much for the detailed explanation. It is greatly appreciated.

    -Jeff

    "Peter Kelcey" <> wrote in message
    news:...
    > Jeff
    >
    > The issue isn't caused by your code. Your problem is caused by the fact
    > that web services are stateless are you are trying to use them as if
    > they are stateful. With the design that you have, you are making two
    > separate calls to your web service. The web service will not maintain
    > the state of you class variable between calls. Each time you call the
    > web service, the class is instantiated again and all variables are
    > created new.
    >
    > You can get hack your way around this by storing the value of
    > strFileName in a session variable. (which can be maintained between
    > calls if you want). However I would recommend that you take the time
    > now to re-think you overall design and use some service design best
    > practices.
    >
    > With web services, you need to put assigned the old "CRUD" method
    > (Create, Read, Update and Delete) and you need to begin to design you
    > methods based on a service approach. CRUD results in "chatty" code
    > that doesn't work well within a services based world.
    >
    > Best practices for services design state that your service should
    > accept all of the parameters that it needs to complete its own work and
    > shouldn't rely on information sent to other methods. Under this
    > design principle, your method would like the following
    >
    > [WebMethod]
    > public void WriteFile(string, strFileName, string strText)
    > {
    > using (StreamWriter sw = File.AppendText(sFN))
    > {
    > sw.WriteLine(strText);
    > }
    > }
    >
    > I'd also recommend the following article that reviews some patterns
    > to follow when designing your services.
    >
    > http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/SOADesign.asp
    >
    > MSDN has lots of other useful articles about designing web services
    > that you can check out.
    >
    > Hope that helps,
    >
    > Peter Kelcey
    >
    Jeffery Tyree, Oct 17, 2005
    #3
    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. mdh
    Replies:
    0
    Views:
    939
  2. Manco

    can webmethods be polymorphic?

    Manco, Feb 3, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    321
    Lionel LASKE
    Feb 3, 2005
  3. godwin
    Replies:
    0
    Views:
    441
    godwin
    Apr 15, 2005
  4. Daniel Barisch

    How to define paramter-types for WebMethods AND Client?

    Daniel Barisch, Aug 22, 2003, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    111
    Daniel Barisch
    Aug 22, 2003
  5. Farooq Khan
    Replies:
    3
    Views:
    396
    Simon Gorski
    Dec 22, 2003
Loading...

Share This Page