Web service - Access Local File

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • markmcgookin
    Recognized Expert Contributor
    • Dec 2006
    • 648

    Web service - Access Local File

    Folks,

    I have an ASP .Net web service that I am using for a product in work. But we need to make this a more "portable" for installation on other boxes. One of the requirements is to avoid a recompile when performing different installs. Thus we can't have any hard coded paths in the service.

    So I have some code I wrote for another application that happily parses an xml file and stores a load of local variables based on it's content, which should be sufficient... however the service doesn't seem to run from the same place each time, from what I gather the code is kept in a .dll file which is moved to a temporary directory on the server when the service is started, and is random each time.

    What I want to know is, is it possible for a web service to be aware of the file path that the web site it is running in in IIS allowing us to reference it no matter where it is installed (Like Application.Sta rtupPath in a normal app)? OR if that is not possible is it possible in either Visual Studio or IIS to ensure that the xml file is copied across to the temp directory each time, and then user a local path (again something like Application.Sta rtupPath) that will return the current temp directory to reference it... i.e. Application.Sta rtupPath + @"\Settings.xml ";

    Any help here would be greatly appreciated, thanks for you time folks!

    Mark

    EDIT: The "random" directory the files are being run from is like this (but different) every time...

    C:\WINDOWS\Micr osoft.NET\Frame work\v2.0.50727 \Temporary ASP.NET Files\root\be74 4616\404c35e0\a ssembly\dl3\6bb 73064\0006a6c3_ 7987c901
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Originally posted by markmcgookin
    ... the service doesn't seem to run from the same place each time, from what I gather the code is kept in a .dll file which is moved to a temporary directory on the server when the service is started, and is random each time.
    I'm not sure what you're talking about here?

    Originally posted by markmcgookin
    What I want to know is, is it possible for a web service to be aware of the file path that the web site it is running in in IIS allowing us to reference it no matter where it is installed (Like Application.Sta rtupPath in a normal app)?
    This is quite easy.
    Just use the Server.MapPath method to map to the root directory of the application ("~/"):
    Code:
    Imports System.Web.Services
    Imports System.Web.Services.Protocols
    Imports System.ComponentModel
    
    ' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    ' <System.Web.Script.Services.ScriptService()> _
    <System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
    <System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
    <ToolboxItem(False)> _
    Public Class Service1
        Inherits System.Web.Services.WebService
    
        <WebMethod()> _
        Public Function ServicePath(ByVal person As String) As String
            Return "My Path is: " + Server.MapPath("~/")
        End Function
    
    End Class
    Originally posted by markmcgookin
    OR if that is not possible is it possible in either Visual Studio or IIS to ensure that the xml file is copied across to the temp directory each time, and then user a local path (again something like Application.Sta rtupPath) that will return the current temp directory to reference it... i.e. Application.Sta rtupPath + @"\Settings.xml ";
    It sounds like your install program needs to be updated to make sure that the resources are in a location that your application expects them to be.

    -Frinny

    Comment

    • markmcgookin
      Recognized Expert Contributor
      • Dec 2006
      • 648

      #3
      Thanks Frin,

      I am not a web services expert by any means, I have been asked to work on this with another developer who is the guy who created the service. He seemed to think that when the service is run some files are copied to that C:\WINDOWS\Micr osoft.NET\Frame work\... etc location.

      Part of his code logs those file paths (I am not sure how, but I can't imagine it's being made up! hehe) we had used a .Net settings file because we thought it was going to be an xml file, and there is some xml to it, but VS seems to create and compile a C# file using this data and when we edit the xml on the server it doesn't affect the variables in the application.

      Sorry to sound a bit like a n00b here, but I am just a humble mobile developer hehe.

      So if we use that "Server.MapPath ("~/")" variable, will that point to the directory that the web site is in in IIS? So we could reference an xml file in there? by going
      Code:
      string path = Server.MapPath("~/") + @"\myFile.xml";
      Thanks for the help.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Hold on, back up.
        Are you talking about client code or server code?

        When you say that a file is being copied into:
        C:\WINDOWS\Micr osoft.NET\Frame work\v2.0.50727 \Temp orary ASP.NET Files\root\be74 4616\404c35e0\a ssembly\dl3\6bb 73064 \0006a6c3_7987c 901

        Is this the client computer?
        Or the server?

        Comment

        Working...