0

i have a webservice project c# , when i upload my project @live i get this error :

System.IO.IOException: The process cannot access the file 'E:\Sites\www.bivolino.com\bivolino3D\bivo\imgGal\ProductFeedBeslist.xml' because it is being used by another process. 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) at System.Xml.XmlTextWriter..ctor(String filename, Encoding encoding)
at ws_og_bivolinogallery.GetItemsBeslist() in e:\Sites\www.bivolino.com\bivolino3D\bivo\OpenGarments\og_webservice\App_Code\ws_og_bivolinogallery.cs:line 1186

in my local (local server)all is ok when i run my project but @live(live server) all my methods dosen't work any more. here is my code (for exemple for my new method):

[WebMethod(MessageName = "GetItemsBeslist", Description = "Get a list of GAL shirts", CacheDuration = 3600)]
   public XmlDocument GetItemsBeslist()
   {
   XmlTextWriter textWriter = new XmlTextWriter("E:/Sites/www.bivolino.com/bivolino3D/bivo/imgGal/ProductFeedBeslist.xml", Encoding.UTF8);
   //E:/Sites/www.bivolino.com/bivolino3D/bivo/imgGal
   try
   {
       if (bRegisterIP)
       {
           try { LogFiler.ToLog("### IP ### - [" + sRemoteAddress + "]"); }
           catch { }
       }
       XmlDocument xProducts = new XmlDocument();
       XmlElement subElm;
       XmlElement elmAttr;
       XmlNode elmValue;


       xProducts.CreateXmlDeclaration("1.0", "utf-8", null);
       XmlElement topElm = xProducts.CreateElement("ProductFeed");
       topElm.SetAttribute("version", "1.0");
       topElm.SetAttribute("timestamp", System.DateTime.Now.ToString().Replace(" ", ":"));
       xProducts.AppendChild(topElm);

       List<string[]> strarrVelden = new List<string[]>();
       strarrVelden.AddRange(DB.GetItemsBeslist());
       foreach (string[] rij in strarrVelden)
       {

           subElm = xProducts.CreateElement("Product");

           elmAttr = xProducts.CreateElement("ProductTitle");
           elmValue = xProducts.CreateNode(XmlNodeType.Text, "ProductTitle", null); elmValue.Value = "Herenoverhemd Bivolino " + rij[5].ToString();
           elmAttr.AppendChild(elmValue);
           subElm.AppendChild(elmAttr);

           elmAttr = xProducts.CreateElement("Price");
           elmValue = xProducts.CreateNode(XmlNodeType.Text, "Price", null); elmValue.Value = rij[6].ToString().Replace(",", ".");
           elmAttr.AppendChild(elmValue);
           subElm.AppendChild(elmAttr);


           elmAttr = xProducts.CreateElement("productURL");
           elmValue = xProducts.CreateNode(XmlNodeType.CDATA, "productURL", null); elmValue.Value = rij[1].ToString();
           elmAttr.AppendChild(elmValue);
           subElm.AppendChild(elmAttr);


           elmAttr = xProducts.CreateElement("Category");
           elmValue = xProducts.CreateNode(XmlNodeType.Text, "Category", null); elmValue.Value = "Herenoverhemd ";
           elmAttr.AppendChild(elmValue);
           subElm.AppendChild(elmAttr);


           elmAttr = xProducts.CreateElement("ProductDescription");
           elmValue = xProducts.CreateNode(XmlNodeType.Text, "ProductDescription", null); elmValue.Value = rij[2].ToString();
           elmAttr.AppendChild(elmValue);
           subElm.AppendChild(elmAttr);



           topElm.AppendChild(subElm);
       }

       textWriter.WriteStartDocument();

       xProducts.Save(textWriter);
       textWriter.WriteEndDocument();
       textWriter.Close();

       return xProducts;


   }
   catch (Exception ex)
   {
       return ErrHandle("ERROR - GetItemsBeslist - " + ex.Message, "ERROR - GetItemsBeslist");
   }

}

Normally these errors come from unclosed file streams, but I've taken care of that. I guess I've forgotten an important step but cannot figure out where. Thank you very much for your help

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
user2170016
  • 1
  • 1
  • 1

5 Answers5

1

I'd create the XmlTextWriter only at the end of the method(when we actually need it) and I'd use a using block as well. using (var textWriter = new XmlTextWriter ("")) { ... } Moreover, can this method be called by different threads at the same time? If so, you have to handle concurrency.

Luke
  • 22,826
  • 31
  • 110
  • 193
laszlokiss88
  • 4,001
  • 3
  • 20
  • 26
0

try wrapping your stream ctor in `using:

using (XmlTextWriter textWriter = new XmlTextWriter("E:/Sites/www.bivolino.com/bivolino3D/bivo/imgGal/ProductFeedBeslist.xml", Encoding.UTF8)){

...
}

Your code doesn't seem to close the stream upon exceptions, using takes care of that. Even though XmlTextWriter is IDisposable it may take some time before it's disposed, you may be hitting your method before that happens.

Also - learn about serialization, chances are there's no need to create the XML manually like you do.

Sten Petrov
  • 10,943
  • 1
  • 41
  • 61
0

Consider using Server.MapPath or HostingEnvironment to get your file system links. A using statement should take care of the issue.

[WebMethod(MessageName = "GetItemsBeslist", Description = "Get a list of GAL shirts", CacheDuration = 3600)]
public XmlDocument GetItemsBeslist()
{
    if (bRegisterIP)
    {
       try { LogFiler.ToLog("### IP ### - [" + sRemoteAddress + "]"); }
       catch { }
    }

    try
    {
       var xProducts = GetProducts();
       string file = Server.MapPath("/bivolino3D/bivo/imgGal/ProductFeedBeslist.xml");

       using(XmlTextWriter textWriter = new XmlTextWriter(file, Encoding.UTF8))
       {
           textWriter.WriteStartDocument();
           xProducts.Save(textWriter);
           textWriter.WriteEndDocument();
       }

       return xProducts;
    }
    catch (Exception ex)
    {
       return ErrHandle("ERROR - GetItemsBeslist - " + ex.Message, "ERROR - GetItemsBeslist");
    }
}

private XmlDocument GetProducts()
{
    XmlDocument xProducts = new XmlDocument();
    XmlElement subElm;
    XmlElement elmAttr;
    XmlNode elmValue;


    xProducts.CreateXmlDeclaration("1.0", "utf-8", null);
    XmlElement topElm = xProducts.CreateElement("ProductFeed");
    topElm.SetAttribute("version", "1.0");
    topElm.SetAttribute("timestamp", System.DateTime.Now.ToString().Replace(" ", ":"));
    xProducts.AppendChild(topElm);

    List<string[]> strarrVelden = new List<string[]>();
    strarrVelden.AddRange(DB.GetItemsBeslist());
    foreach (string[] rij in strarrVelden)
    {

       subElm = xProducts.CreateElement("Product");

       elmAttr = xProducts.CreateElement("ProductTitle");
       elmValue = xProducts.CreateNode(XmlNodeType.Text, "ProductTitle", null); elmValue.Value = "Herenoverhemd Bivolino " + rij[5].ToString();
       elmAttr.AppendChild(elmValue);
       subElm.AppendChild(elmAttr);

       elmAttr = xProducts.CreateElement("Price");
       elmValue = xProducts.CreateNode(XmlNodeType.Text, "Price", null); elmValue.Value = rij[6].ToString().Replace(",", ".");
       elmAttr.AppendChild(elmValue);
       subElm.AppendChild(elmAttr);


       elmAttr = xProducts.CreateElement("productURL");
       elmValue = xProducts.CreateNode(XmlNodeType.CDATA, "productURL", null); elmValue.Value = rij[1].ToString();
       elmAttr.AppendChild(elmValue);
       subElm.AppendChild(elmAttr);


       elmAttr = xProducts.CreateElement("Category");
       elmValue = xProducts.CreateNode(XmlNodeType.Text, "Category", null); elmValue.Value = "Herenoverhemd ";
       elmAttr.AppendChild(elmValue);
       subElm.AppendChild(elmAttr);


       elmAttr = xProducts.CreateElement("ProductDescription");
       elmValue = xProducts.CreateNode(XmlNodeType.Text, "ProductDescription", null); elmValue.Value = rij[2].ToString();
       elmAttr.AppendChild(elmValue);
       subElm.AppendChild(elmAttr);



       topElm.AppendChild(subElm);
    }

    return xProducts;
}
Community
  • 1
  • 1
Dustin Kingen
  • 20,677
  • 7
  • 52
  • 92
0

I would also call textWriter.Flush() before textWriter.Close() or as the last line your using block. Sometimes .NET takes a while to write from the buffer to the underlying stream on IO operations on a close and that can lock your file.

0

you shd try to open the file when you really need it and serialize the access for multiple threads.

rather than opening the textwriter at start of method , open it where you need it i.e. at the end of method also define static global object objLock on which you can take the lock. this should work

 private static  object objLock = new object();

 lock(objLock)
 {
   using (XmlTextWriter textWriter = new    XmlTextWriter("E:/Sites/www.bivolino.com/bivolino3D/bivo/imgGal/ProductFeedBeslist.xml", Encoding.UTF8))
    {
       textWriter.WriteStartDocument();
       xProducts.Save(textWriter);
       textWriter.WriteEndDocument();
       textWriter.Close();


     }

 }
TalentTuner
  • 17,262
  • 5
  • 38
  • 63