Quantcast
Channel: C# webservice referencing dll cannot access external xml file - Stack Overflow
Viewing all articles
Browse latest Browse all 3

C# webservice referencing dll cannot access external xml file

$
0
0

I'm building a web service in C#.On this web service I have the following code:

    public class Service1 : System.Web.Services.WebService        {            [WebMethod]            public decimal GetTaxAmountx(decimal amount, int year)            {                decimal result;                result = LB.GetTaxAmount(amount, year);                return result;            }        }

LB is a static class providing a static method GetTaxAmount(decimal amount, int year).This class is in a dll which I reference from my web service.The LB.GetTaxAmount method uses linq to xml to load some data, like this:

    var name = from nm in XElement.Load("Taxes.xml").Elements("Year").Elements("Scale")                   where nm.Parent.Attribute("id").Value == year.ToString()&& (decimal)nm.Element("MoreThan") <= amount&& (decimal)nm.Element("NotMoreThan") >= amount                   select new                    {                        TaxAmount =  nm.Element("TaxAmount"),                        Percentage = nm.Element("Percentage"),                        MoreThan = nm.Element("MoreThan")                   };

Finally LB.GetTaxAmount returns a decimal.

When testing a call to LB.GetTaxAmount by referencing the dll from code in a plain vanilla class, it all works.But when I test the web service by entering the parameters and clicking the INVOKE button, it finds the dll but cannot find the XMl file which I put in the same folder as the ddl file. I get the following error message:

System.IO.FileNotFoundException: Could not find file 'C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\Taxes.xml'. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize) at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials) at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn) at System.Xml.XmlReader.Create(String inputUri, XmlReaderSettings settings, XmlParserContext inputContext) at System.Xml.XmlReader.Create(String inputUri, XmlReaderSettings settings) at System.Xml.Linq.XElement.Load(String uri, LoadOptions options) at System.Xml.Linq.XElement.Load(String uri) at AtYourServiceSoftware.LB.GetTaxAmount(Decimal amount, Int32 year) at sxm3.Service1.GetTaxAmountx(Decimal amount, Int32 year) in C:\Documents and Settings\user\My Documents\Visual Studio 2008\Projects\sxm3\sxm3\Service1.asmx.cs:line 29

Hope someone knows an answer, thanks in advance. Joost

Oh and btw, I want the xml to be external and not embedded, so it can be updated from time to time by my client (client as in the guy who pays me).

UPDATE: Found it thanks to Mike and Kyle and this post: http://www.stackoverflow.com/a/283917/243557

I changed it to:

    static public string AssemblyDirectory           {               get               {                   string codeBase = Assembly.GetExecutingAssembly().CodeBase;                   UriBuilder uri = new UriBuilder(codeBase);                   string path = Uri.UnescapeDataString(uri.Path);                   return Path.GetDirectoryName(path);               }           }           public static decimal GetTaxAmount(decimal amount, int year)            {                var name = from nm in XElement.Load(AssemblyDirectory +         @"\Taxes.xml").Elements("Year").Elements("Scale")                           where nm.Parent.Attribute("id").Value == year.ToString()&& (decimal)nm.Element("MoreThan") <= amount&& (decimal)nm.Element("NotMoreThan") >= amount                           select new                            {                                TaxAmount =  nm.Element("TaxAmount"),                                Percentage = nm.Element("Percentage"),                                MoreThan = nm.Element("MoreThan")                           };

Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images