Answered by:
Could not find part of Path when trying to test a Web Service

Question
-
User1098464520 posted
I have a web service where I need to have a third party transmit XML docs to a folder on our web server. After much pounding on permissions, I can finally path to a folder at https://sierraproductioncenter.com/Hyphen/In/Order/ however the service is throwing an error that the folder path cannot be found. If I copy and paste the path in an explorer window on web server it resolves to correct folder.
Could not find a part of the path 'C:\inetpub\vhosts\sierraproductioncenter.com\HTTPDOCS\Hyphen\In\Order\'.
I even threw directory code in just in case the directory was reading some alternative location - but no. What is wrong?
WEB SERVICE:
Imports System Imports System.IO 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:="https://sierraproductioncenter.com/")> <System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _ <ToolboxItem(False)> _ Public Class WebService1 Inherits System.Web.Services.WebService <WebMethod()> Public Function SaveDocument(ByVal docbinaryarray As Byte, ByVal docname As String) As Boolean Dim docPath As String = Server.MapPath("~\Hyphen\In\Order\") Dim strdocPath As String = Server.MapPath("~\Hyphen\In\Order\") + docname If Directory.Exists(docPath) = False Then Directory.CreateDirectory(docPath) End If Dim objfilestream As FileStream = New FileStream(strdocPath, FileMode.Create, FileAccess.ReadWrite) Dim bw As BinaryWriter = New BinaryWriter(objfilestream) bw.Write(docbinaryarray) bw.Close() objfilestream.Close() Return True End Function
WEB SERVICE CALL:Imports System.IO Public Class DocumentService Inherits System.Web.UI.Page Dim sfile As String = Server.MapPath("~\Hyphen\In\Order\") Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Directory.Exists(sfile) = False Then Directory.CreateDirectory(sfile) End If End Sub Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim objfilestream As FileStream = New FileStream(sfile, FileMode.Open, FileAccess.Read) Dim len As Integer = CType(objfilestream.Length, Integer) Dim mybytearray() As Byte = New Byte(len) {} Dim myservice As localhost.WebService1 = New localhost.WebService1() objfilestream.Read(mybytearray, 0, len) myservice.SaveDocument(mybytearray(len), sfile) objfilestream.Close() End Sub
Tuesday, August 1, 2017 6:29 PM
Answers
-
User753101303 posted
Hi,
If you are 100% sure C:\inetpub\vhosts\sierraproductioncenter.com\HTTPDOCS\Hyphen\In\Order\ does exists could you check where it happens?
Wondering if it couldn't be that the file name is empty and so it would happen because you are trying to open a C:\inetpub\vhosts\sierraproductioncenter.com\HTTPDOCS\Hyphen\In\Order\ file stream (actually I jus checked and got the same error).
Not directly related but as docname is not checked :
- it is easy to overwrite an existing document
- someone could even attempt to use a relative path (..\..\folder\file.exe) and attempt to overwrite a system fileEdit: BTW if confirmed this is nice sample that shows you should also look at the call stack rather than just looking at the exception message. I'll use this as a sample if I ever blog about how to best debug an app.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, August 2, 2017 5:42 PM
All replies
-
User-943250815 posted
Just using info provided in your message
"I can finally path to a folder at https://sierraproductioncenter.com/Hyphen/In/Order/"
and
Could not find a part of the path 'C:\inetpub\vhosts\sierraproductioncenter.com\HTTPDOCS\Hypen\In\Order\'
Take some attention to bold parts above
My guess, is double check if paths are correct considering directory exist.
When fixed, verify when dir does not exist what are folder permissions when created by app
Wednesday, August 2, 2017 12:41 AM -
User1098464520 posted
Because when you are dealing with a folder that is a web address and a folder that is a target to save a document they are slightly different addresses. I web address isn't going to have the "given" structure.
The error is in the path to the target for a save. However, if I cut and paste the address into the server explorer window it is mapping correctly.
Wednesday, August 2, 2017 2:14 PM -
User475983607 posted
You're essentially concatenating the following in the service as you are passing the path from the client too.
Dim strdocPath As String = Server.MapPath("~\Hyphen\In\Order\") & "[wwwroot]\Hyphen\In\Order\"
You can use the Visual Studio debugger to single step through the code which should help find these types of errors.
Wednesday, August 2, 2017 5:26 PM -
User753101303 posted
Hi,
If you are 100% sure C:\inetpub\vhosts\sierraproductioncenter.com\HTTPDOCS\Hyphen\In\Order\ does exists could you check where it happens?
Wondering if it couldn't be that the file name is empty and so it would happen because you are trying to open a C:\inetpub\vhosts\sierraproductioncenter.com\HTTPDOCS\Hyphen\In\Order\ file stream (actually I jus checked and got the same error).
Not directly related but as docname is not checked :
- it is easy to overwrite an existing document
- someone could even attempt to use a relative path (..\..\folder\file.exe) and attempt to overwrite a system fileEdit: BTW if confirmed this is nice sample that shows you should also look at the call stack rather than just looking at the exception message. I'll use this as a sample if I ever blog about how to best debug an app.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, August 2, 2017 5:42 PM -
User1098464520 posted
Hi,
If you are 100% sure C:\inetpub\vhosts\sierraproductioncenter.com\HTTPDOCS\Hyphen\In\Order\ does exists could you check where it happens?
Wondering if it couldn't be that the file name is empty and so it would happen because you are trying to open a C:\inetpub\vhosts\sierraproductioncenter.com\HTTPDOCS\Hyphen\In\Order\ file stream (actually I jus checked and got the same error).
Not directly related but as docname is not checked :
- it is easy to overwrite an existing document
- someone could even attempt to use a relative path (..\..\folder\file.exe) and attempt to overwrite a system fileEdit: BTW if confirmed this is nice sample that shows you should also look at the call stack rather than just looking at the exception message. I'll use this as a sample if I ever blog about how to best debug an app.
PatriceSc - thank you. I spend some time this morning mapping all the way through and finally got an error message that made sense - ASP.NET cannot find C:\inetpub\vhosts\sierraproductioncenter.com\HTTPDOCS\Hyphen\In\Order\ Well when ASP can't find something that means it isn't on the web server so I looked at where the file was coming from and went looking for paths and validations... I also coded a file upload to specify the actual file and Viola!
Button for Test
Dim sfile As String = Server.MapPath("~\Hyphen\In\Order\") Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load End Sub Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click If xmlFile.HasFile Then Dim msg As Queue = New Queue msg.Enqueue(xmlFile.FileName) Label1.Text = "Received " & xmlFile.FileName & " Content Type " & xmlFile.PostedFile.ContentType & " Length " & xmlFile.PostedFile.ContentLength xmlFile.SaveAs(sfile & xmlFile.FileName) End If 'NOT USING 'Dim objfilestream As FileStream = New FileStream(sfile, FileMode.Open, FileAccess.Read) 'Dim len As Integer = CType(objfilestream.Length, Integer) 'Dim mybytearray() As Byte = New Byte(len) {} ' Dim myservice As localhost.WebService1 = New localhost.WebService1() 'objfilestream.Read(mybytearray, 0, len) 'myservice.SaveDocument(mybytearray(len), sfile) 'objfilestream.Close() End Sub
So now I just have to modify for WebMethod.
Thank you for your help.
Thursday, August 3, 2017 8:17 PM