?
Solved

Server.MapPath - Throws an exception in Windows Application ( Server is  not declared)

Posted on 2010-01-11
17
Medium Priority
?
661 Views
Last Modified: 2013-11-18

Server.MapPath - Throws an exception in Windows Application ( Server is  not declared)

I am working on a Migration / Conversion Project from Webservice to Class Layer ..!

In our existing webservice project - We have mentioned Server.MapPath("F I L E N AM E")

Now, i have commented  - Inherits System.Web.Services.WebService - On doing so, I am not able to fix the Server.MapPath - Syntax issue.
0
Comment
Question by:chokka
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 8
17 Comments
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 26287561
In what context is the code that's throwing the exception, in a WebMethod body?  Can you post your code?
0
 

Author Comment

by:chokka
ID: 26287797


Please find the code.

At present, I am passing the direct file path
'Return New MedISDAL().GetMembersAddressAsXML(filter, Server.MapPath("MemberAddressReport.xslt"))
            Return New MedISDAL().GetMembersAddressAsXML(filter, "C:\\ClassProject\\MedISServices\\MedISServices\\App_Code\\MemberAddressReport.xslt")

Open in new window

0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 26287809
Can you post the complete code, including Imports statements.
0
WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

 

Author Comment

by:chokka
ID: 26287858


Here i have posted the exact code ..!

Among these codes , I have commented following codes (As we need to migrate from Webservice to Class Layer)


'Imports System.Web.Caching
'Imports System.Web.Services
'Imports System.Web.Services.Protocols

 '<System.Web.Services.WebService(Namespace:="http://tempuri.org/MedIS.Web/MedISServices")> _
'Inherits System.Web.Services.WebService

 'Return New MedISDAL().GetMembersAddressAsXML(filter, Server.MapPath("MemberAddressReport.xslt"))
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web


'Imports System.Web.Caching
'Imports System.Web.Services
'Imports System.Web.Services.Protocols


Namespace MedIS.Web



    '<System.Web.Services.WebService(Namespace:="http://tempuri.org/MedIS.Web/MedISServices")> _
    Public Class MedISServices
        'Inherits System.Web.Services.WebService




 '<WebMethod(Description:="Get Member Address report in MS Excel Format."), SoapHeader("Credentials")> _
        Public Function GetMembersAddressAsXML(ByVal filter As String) As Byte()
            SecurityHelper.VerifyCredentials(Me)

            'Return New MedISDAL().GetMembersAddressAsXML(filter, Server.MapPath("MemberAddressReport.xslt"))
            Return New MedISDAL().GetMembersAddressAsXML(filter, "C:\\ClassProject\\MedISServices\\MedISServices\\App_Code\\MemberAddressReport.xslt")

        End Function

Open in new window

0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 26287976
I don't understand why you've commented all those lines out. "Server" is defined in System.Web.Services.WebService, and since you've commented out the "Inherits" line you've also removed everything defined in System.Web.Services.WebService, including "Server"

You can use System.Web.HttpContext.Current.Server.MapPath() instead, but it will only work if it's used inside of some sort of web application or web service.
0
 

Author Comment

by:chokka
ID: 26288034


Ok .. as i said you before that i am migrating or converting a Webservice Project to Normal Class layer Project.

We are removing off - Webservice Layer Permanently.

When i am undergoing the migration, I have to comment  those Web dll's.

Now, i am looking for alternate syntax instead of Server.MapPath("") to place it in Class layer.

I have an option - By mentioning the file path in Config file and passing the Key.

But i am looking for perfect syntax replacement for Server.MapPath("")
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 26288101
Is this class going to be used in a web application or web service?
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 26288107
...or are you moving to a Windows Forms or Console based application?
0
 

Author Comment

by:chokka
ID: 26288138


Our existing application is Smart Client - Which is depend on Webservice layer.

Now, we are moving to Windows Forms Application ..! We are removing Smart Client feature .. by remodifying the Webservice Layer ..!
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 26288195
The reason I ask is that Server.MapPath specifically converts a web page style path into a physical path on the server. For example, I can use Server.MapPath("/images/picture.gif") to get "C:\InetPub\wwwroot\myWebAppFolder\image\picture.gif".  So, you can see Server.MapPath() is useless outside of a web-based application of some sort.  There is no Server.MapPath outside of a web service or web application.

Where do you intend to keep MemberAddressReport.xslt? If it's in the same folder as the program (or the current working director), you should be able to refer to it by filename without any path information.

If you want to keep MemberAddressReport.xslt in the same folder as the program is running from, you can use Application.ExecutablePath to get, for example, "C:\Program Files\Your Company Name\Your Product Name"
0
 

Author Comment

by:chokka
ID: 26288285


 "C:\Program Files\Your Company Name\Your Product Name" - This is going to be in deployment file path ?

I think - Once we go for Class Layer Project - We will be using Dll's as Project Reference to the Main Project or Start up - Project.

So, we just need to keep "MemberAddressReport.xslt" in local working directory folder,



0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 26291740
If you want the .xslt in the current working directory, then you simply remove Server.MapPath (and make sure the Member...xslt is in the current working directory): Return New MedISDAL().GetMembersAddressAsXML(filter, "MemberAddressReport.xslt")

0
 

Author Comment

by:chokka
ID: 26294449
(filter, "MemberAddressReport.xslt") - Incorrect file path ..!

Though i have placed in the working directory ..! It is expecting the file path ...!
0
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 2000 total points
ID: 26295823
1. Store the file in the same folder as the executable (not the class library).  Use Application.ExecutablePath + "\MemberAddressReport.xslt"

2. Store the file in the current working directory (which is not necessarily the same folder as where the exe is installed) and use "MemberAddressReport.xslt". To see the current working directory, try MessageBox.Show(System.IO.Directory.GetCurrentDirectory()).

3. Store the file in the same folder as the class library and use System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase.Replace("file:///", "").Replace("/", "\")) + "\MemberAddressReport.xslt"
0
 

Author Comment

by:chokka
ID: 26296726
i am just checking ..!   You have mentioned 3 different options.

lets make it crystal clear .. a simplest way .. what you suggest ..
i just want to put in the project working folder and generate a dll and use it as reference
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 26296803
Option 3.  Make sure the file is really in the same folder as the DLL.
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web


Namespace MedIS.Web



    '<System.Web.Services.WebService(Namespace:="http://tempuri.org/MedIS.Web/MedISServices")> _ 
    Public Class MedISServices
        'Inherits System.Web.Services.WebService 




        '<WebMethod(Description:="Get Member Address report in MS Excel Format."), SoapHeader("Credentials")> _ 
        Public Function GetMembersAddressAsXML(ByVal filter As String) As Byte()
            SecurityHelper.VerifyCredentials(Me)

            'Return New MedISDAL().GetMembersAddressAsXML(filter, Server.MapPath("MemberAddressReport.xslt")) 
            Return New MedISDAL().GetMembersAddressAsXML(filter, GetCurrentDLLPath() + "\MemberAddressReport.xslt")

        End Function

        Private Shared Function GetCurrentDLLPath() As String
            Return System.IO.Path.GetDirectoryName( _
                System.Reflection.Assembly.GetExecutingAssembly().CodeBase.Replace("file:///", "").Replace("/", "\"))
        End Function
    End Class
End Namespace

Open in new window

0
 

Author Closing Comment

by:chokka
ID: 31675739
Helpful,
Thanks
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question