0
votes

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

5
You're not disposing the TextWriter.Dustin Kingen
never disclose server directory structure "E:/Sites/www.bivolino.com/bivolino3D/bivo/imgGal/ProductFeedBeslist.xm"TalentTuner
Why are you saving it to disk anyway? You are returning XmlDocument from your method and never use the file...Ilia G
its a webmethod , most probably multiple request is trying to access the path simulteneously and you are overwriting the file each time , is there any specific reasonsTalentTuner

5 Answers

1
votes

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.

0
votes

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.

0
votes

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
votes

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;
}
0
votes

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();


     }

 }