We are working on an asp.net application which is a 3-tier application.I was
aksed to create a component which monitors a folder and gets the file and
pass them to a class library in our business logic layer(so far so good and
easy).I initialize my class which is using a FileSystemWatcher in my
Global.asax and everything works fine.I have found FileSystemWatcher class
not very reliable and sometimes it behavies unexpectedly.I'm afriad that it
brings down the whole application.Is there a better way of doing this
**Inside Asp.Net application**.I can't use antother application like windows
service or schedault taks.Everything needs to be done is ASP.NET
application.
Thanks a lot 20 4415
Hi J-T, I have found FileSystemWatcher class not very reliable and sometimes it behavies unexpectedly.I'm afriad that it brings down the whole application.
The FileSystemWatcher class is very reliable. However, it uses events, which
are asynchronous to your application. Without knowing anything more about
how your code is written, I can't tell you much more. For example,
"Global.asax" is a file. The Global class that is generated when it is
compiled is a class. It contains a number of events, and you can't store a
class in an event. So, I have no idea where you're persisting your class, or
how. Also, you mentioned that you created a "component," which most probably
is not a "Component" in the literal .Net sense of the term. So, I know
nothing about that either.
--
HTH,
Kevin Spencer
Microsoft MVP
..Net Developer
Sometimes you eat the elephant.
Sometimes the elephant eats you.
"J-T" <Ra****@microsft.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl... We are working on an asp.net application which is a 3-tier application.I was aksed to create a component which monitors a folder and gets the file and pass them to a class library in our business logic layer(so far so good and easy).I initialize my class which is using a FileSystemWatcher in my Global.asax and everything works fine.I have found FileSystemWatcher class not very reliable and sometimes it behavies unexpectedly.I'm afriad that it brings down the whole application.Is there a better way of doing this **Inside Asp.Net application**.I can't use antother application like windows service or schedault taks.Everything needs to be done is ASP.NET application.
Thanks a lot
Are you sure you are on the right track? A FileSystemWatcher object has to
exist somewhere to be able to watch. An asp.net application doesn't exist
anywhere but between a client http request and the server response. A very
short time. Do you expect the watcher to catch something only when the
server is busy serving client requests?
Eliyahu
"J-T" <Ra****@microsft.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl... We are working on an asp.net application which is a 3-tier application.I
was aksed to create a component which monitors a folder and gets the file and pass them to a class library in our business logic layer(so far so good
and easy).I initialize my class which is using a FileSystemWatcher in my Global.asax and everything works fine.I have found FileSystemWatcher
class not very reliable and sometimes it behavies unexpectedly.I'm afriad that
it brings down the whole application.Is there a better way of doing this **Inside Asp.Net application**.I can't use antother application like
windows service or schedault taks.Everything needs to be done is ASP.NET application.
Thanks a lot
I forgot to say that directory which I monitor is a shared folder on the
netwrok.
Thanks
"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message
news:ud**************@TK2MSFTNGP09.phx.gbl... Are you sure you are on the right track? A FileSystemWatcher object has to exist somewhere to be able to watch. An asp.net application doesn't exist anywhere but between a client http request and the server response. A very short time. Do you expect the watcher to catch something only when the server is busy serving client requests?
Eliyahu
"J-T" <Ra****@microsft.com> wrote in message news:%2****************@TK2MSFTNGP15.phx.gbl... We are working on an asp.net application which is a 3-tier application.I was aksed to create a component which monitors a folder and gets the file and pass them to a class library in our business logic layer(so far so good and easy).I initialize my class which is using a FileSystemWatcher in my Global.asax and everything works fine.I have found FileSystemWatcher class not very reliable and sometimes it behavies unexpectedly.I'm afriad that it brings down the whole application.Is there a better way of doing this **Inside Asp.Net application**.I can't use antother application like windows service or schedault taks.Everything needs to be done is ASP.NET application.
Thanks a lot
Here is what I have done .I have created a class and I instanciate that
class in Application_Start method of my Global.asax ere is the code:
********Gloabl.asax:
//Sets up a wacher on an specific shared folder to pickup zip files
IFPWatcherComponent ifpWacher=new
IFPWatcherComponent(ConfigurationSettings.AppSetti ngs[ "MonitorPath"]);
********MyClass:
/// </summary>
public class IFPWatcherComponent:BusinessObject
{
string PathToMonitor=null;
public IFPWatcherComponent(string PathToMonitor)
{
try
{
this.PathToMonitor = PathToMonitor;
//Check to see if Web.config has appropriate settings
if (this.PathToMonitor.Trim().Length==0 ) return;
// Make a reference to a directory.
DirectoryInfo di = new DirectoryInfo(this.PathToMonitor);
// Create the directory only if it does not already exist.
if (di.Exists == false)
di.Create();
// create an instance of FileSystemWatcher object and assign path to
monitor
FileSystemWatcher watcher = new FileSystemWatcher();
// set necessary filters
watcher.Path = this.PathToMonitor;
//Watch for changes in FileName
watcher.NotifyFilter = NotifyFilters.FileName;
// watch zip files
watcher.Filter = "*.zip";
//Add appropriate event handler
watcher.Created += new FileSystemEventHandler(OnCreated);
watcher.EnableRaisingEvents = true;
}
catch
{
throw;
}
}
private void OnCreated(object source, FileSystemEventArgs e)
{
FileStream oImg=null;
BinaryReader oBinaryReader=null;
Uploader uploader=new Uploader();
uploader.ComeFrom="88";
uploader.Comments="comment";
uploader.CreatedByUser="test";
uploader.FileName=e.Name;
try
{
oImg = new FileStream (e.FullPath,FileMode.Open,FileAccess.Read);
oBinaryReader = new BinaryReader(oImg);
byte[] oImgByteArray = oBinaryReader.ReadBytes((int) oImg.Length);
uploader.FileBody=oImgByteArray;
uploader.RecordType=Business.IFP.RecordType.IFP_UP LOAD;
uploader.Upload();
uploader=null;
}
catch(Exception ee)
{
*******************I don't know how to handle the exception
here??***********************
}
finally
{
if (oBinaryReader!=null) oBinaryReader.Close();
if (oImg!=null) oImg.Close();
}
}
"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message
news:ud**************@TK2MSFTNGP09.phx.gbl... Are you sure you are on the right track? A FileSystemWatcher object has to exist somewhere to be able to watch. An asp.net application doesn't exist anywhere but between a client http request and the server response. A very short time. Do you expect the watcher to catch something only when the server is busy serving client requests?
Eliyahu
"J-T" <Ra****@microsft.com> wrote in message news:%2****************@TK2MSFTNGP15.phx.gbl... We are working on an asp.net application which is a 3-tier application.I was aksed to create a component which monitors a folder and gets the file and pass them to a class library in our business logic layer(so far so good and easy).I initialize my class which is using a FileSystemWatcher in my Global.asax and everything works fine.I have found FileSystemWatcher class not very reliable and sometimes it behavies unexpectedly.I'm afriad that it brings down the whole application.Is there a better way of doing this **Inside Asp.Net application**.I can't use antother application like windows service or schedault taks.Everything needs to be done is ASP.NET application.
Thanks a lot
This could be a problem. In my experience I could not get FileSystemWatcher
to work for network directories. Either it is not able to or some tricky
setting is needed.
Eliyahu
"J-T" <Ra****@microsft.com> wrote in message
news:Ov**************@tk2msftngp13.phx.gbl... I forgot to say that directory which I monitor is a shared folder on the netwrok.
Thanks "Eliyahu Goldin" <re*************@monarchmed.com> wrote in message news:ud**************@TK2MSFTNGP09.phx.gbl... Are you sure you are on the right track? A FileSystemWatcher object has
to exist somewhere to be able to watch. An asp.net application doesn't
exist anywhere but between a client http request and the server response. A
very short time. Do you expect the watcher to catch something only when the server is busy serving client requests?
Eliyahu
"J-T" <Ra****@microsft.com> wrote in message news:%2****************@TK2MSFTNGP15.phx.gbl... We are working on an asp.net application which is a 3-tier
application.I was aksed to create a component which monitors a folder and gets the file
and pass them to a class library in our business logic layer(so far so good and easy).I initialize my class which is using a FileSystemWatcher in my Global.asax and everything works fine.I have found FileSystemWatcher class not very reliable and sometimes it behavies unexpectedly.I'm afriad
that it brings down the whole application.Is there a better way of doing this **Inside Asp.Net application**.I can't use antother application like windows service or schedault taks.Everything needs to be done is ASP.NET application.
Thanks a lot
Your FileSystemWatcher is scoped to the method in which you instantiate it.
That means that it goes out of scope and is not available as soon as the
method returns. It may hang around for a bit since you have no code to
dispose it at any point, but you need to bone up on scope and state. Here's
a primer:
Scope is the "area of influence" of a variable. In a class, you have global
and local variables. Global variables are declared outside of any methods,
and are therefore "visible" to all members of the class. Local variables are
declared inside a method, and are therefore "visible" only to other
variables within a single instance (call) of the method. Once the method
returns, it is removed from the stack, and everything in it becomes
available for garbage collection.
Note that this differs from public, private, etc. These are also definitions
of scope, but are more related to encapsulation. Public members of a class
are "visible" from other classes. Private members are not. Etc.
State refers to the persistence of an object (class instance or primitive)
in memory. Whenever an object is instantiated (created), it must reside in
memory somewhere. The lifetime of an object is limited to the lifetime of
the container in which it resides. A method is instantiated, just like a
variable, whenever you call it. And just like a variable, when it returns,
it is available for garbage collection. The only way to persist an instance
is to put it into something else that persists. Hence, ASP.Net, which
operates using HTTP, which is stateless, has mechanisms for maintaining
state, such as Session, Application, and ViewState.
The global.asax class, as I mentioned in my earlier reply, is a persistent
class, but it's methods are not. They are instantiated like any other
methods, and pass out of scope as soon as they return.
I hope you will study what I've said, and continue to study how OOP works as
you go. In the meantime, you need to understand how to persist your
FileSystemWatcher, as well as making sure that when you are finished with
it, you dispose it.
--
HTH,
Kevin Spencer
Microsoft MVP
..Net Developer
Sometimes you eat the elephant.
Sometimes the elephant eats you.
"J-T" <Ra****@microsft.com> wrote in message
news:e5**************@TK2MSFTNGP14.phx.gbl... Here is what I have done .I have created a class and I instanciate that class in Application_Start method of my Global.asax ere is the code:
********Gloabl.asax: //Sets up a wacher on an specific shared folder to pickup zip files
IFPWatcherComponent ifpWacher=new IFPWatcherComponent(ConfigurationSettings.AppSetti ngs[ "MonitorPath"]); ********MyClass:
/// </summary> public class IFPWatcherComponent:BusinessObject { string PathToMonitor=null;
public IFPWatcherComponent(string PathToMonitor) {
try { this.PathToMonitor = PathToMonitor; //Check to see if Web.config has appropriate settings if (this.PathToMonitor.Trim().Length==0 ) return; // Make a reference to a directory. DirectoryInfo di = new DirectoryInfo(this.PathToMonitor); // Create the directory only if it does not already exist. if (di.Exists == false) di.Create(); // create an instance of FileSystemWatcher object and assign path to monitor FileSystemWatcher watcher = new FileSystemWatcher(); // set necessary filters watcher.Path = this.PathToMonitor; //Watch for changes in FileName watcher.NotifyFilter = NotifyFilters.FileName; // watch zip files watcher.Filter = "*.zip"; //Add appropriate event handler watcher.Created += new FileSystemEventHandler(OnCreated); watcher.EnableRaisingEvents = true; } catch { throw; } } private void OnCreated(object source, FileSystemEventArgs e) { FileStream oImg=null; BinaryReader oBinaryReader=null;
Uploader uploader=new Uploader(); uploader.ComeFrom="88"; uploader.Comments="comment"; uploader.CreatedByUser="test"; uploader.FileName=e.Name;
try {
oImg = new FileStream (e.FullPath,FileMode.Open,FileAccess.Read); oBinaryReader = new BinaryReader(oImg); byte[] oImgByteArray = oBinaryReader.ReadBytes((int) oImg.Length); uploader.FileBody=oImgByteArray;
uploader.RecordType=Business.IFP.RecordType.IFP_UP LOAD; uploader.Upload(); uploader=null;
} catch(Exception ee) { *******************I don't know how to handle the exception here??*********************** } finally { if (oBinaryReader!=null) oBinaryReader.Close(); if (oImg!=null) oImg.Close();
} }
"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message news:ud**************@TK2MSFTNGP09.phx.gbl... Are you sure you are on the right track? A FileSystemWatcher object has to exist somewhere to be able to watch. An asp.net application doesn't exist anywhere but between a client http request and the server response. A very short time. Do you expect the watcher to catch something only when the server is busy serving client requests?
Eliyahu
"J-T" <Ra****@microsft.com> wrote in message news:%2****************@TK2MSFTNGP15.phx.gbl... We are working on an asp.net application which is a 3-tier application.I was aksed to create a component which monitors a folder and gets the file and pass them to a class library in our business logic layer(so far so good and easy).I initialize my class which is using a FileSystemWatcher in my Global.asax and everything works fine.I have found FileSystemWatcher class not very reliable and sometimes it behavies unexpectedly.I'm afriad that it brings down the whole application.Is there a better way of doing this **Inside Asp.Net application**.I can't use antother application like windows service or schedault taks.Everything needs to be done is ASP.NET application.
Thanks a lot
So how would you monitor a network path in your asp.net application?
Thanks for your reply.
"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message
news:e7**************@TK2MSFTNGP12.phx.gbl... This could be a problem. In my experience I could not get FileSystemWatcher to work for network directories. Either it is not able to or some tricky setting is needed.
Eliyahu
"J-T" <Ra****@microsft.com> wrote in message news:Ov**************@tk2msftngp13.phx.gbl... I forgot to say that directory which I monitor is a shared folder on the netwrok.
Thanks "Eliyahu Goldin" <re*************@monarchmed.com> wrote in message news:ud**************@TK2MSFTNGP09.phx.gbl... > Are you sure you are on the right track? A FileSystemWatcher object has to > exist somewhere to be able to watch. An asp.net application doesn't exist > anywhere but between a client http request and the server response. A very > short time. Do you expect the watcher to catch something only when the > server is busy serving client requests? > > Eliyahu > > > "J-T" <Ra****@microsft.com> wrote in message > news:%2****************@TK2MSFTNGP15.phx.gbl... >> We are working on an asp.net application which is a 3-tier application.I > was >> aksed to create a component which monitors a folder and gets the file and >> pass them to a class library in our business logic layer(so far so >> good > and >> easy).I initialize my class which is using a FileSystemWatcher in my >> Global.asax and everything works fine.I have found FileSystemWatcher > class >> not very reliable and sometimes it behavies unexpectedly.I'm afriad that > it >> brings down the whole application.Is there a better way of doing this >> **Inside Asp.Net application**.I can't use antother application like > windows >> service or schedault taks.Everything needs to be done is ASP.NET >> application. >> >> >> Thanks a lot >> >> > >
So you mean state wise and persistence wise,both,I will have problems by
using this code?
Do you have any idea how to persist my object.I want it to avaialable as
long as they application is up and working.So according to what you suggest
I will have to make the scope of my object some how global (not like the way
it is now) and also persist it in the memory for the time application is up.
Do I really have to dispose the object after the application gets stopped?
Thanks for super informative answer.
Ray
"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:u8****************@tk2msftngp13.phx.gbl... Your FileSystemWatcher is scoped to the method in which you instantiate it. That means that it goes out of scope and is not available as soon as the method returns. It may hang around for a bit since you have no code to dispose it at any point, but you need to bone up on scope and state. Here's a primer:
Scope is the "area of influence" of a variable. In a class, you have global and local variables. Global variables are declared outside of any methods, and are therefore "visible" to all members of the class. Local variables are declared inside a method, and are therefore "visible" only to other variables within a single instance (call) of the method. Once the method returns, it is removed from the stack, and everything in it becomes available for garbage collection.
Note that this differs from public, private, etc. These are also definitions of scope, but are more related to encapsulation. Public members of a class are "visible" from other classes. Private members are not. Etc.
State refers to the persistence of an object (class instance or primitive) in memory. Whenever an object is instantiated (created), it must reside in memory somewhere. The lifetime of an object is limited to the lifetime of the container in which it resides. A method is instantiated, just like a variable, whenever you call it. And just like a variable, when it returns, it is available for garbage collection. The only way to persist an instance is to put it into something else that persists. Hence, ASP.Net, which operates using HTTP, which is stateless, has mechanisms for maintaining state, such as Session, Application, and ViewState.
The global.asax class, as I mentioned in my earlier reply, is a persistent class, but it's methods are not. They are instantiated like any other methods, and pass out of scope as soon as they return.
I hope you will study what I've said, and continue to study how OOP works as you go. In the meantime, you need to understand how to persist your FileSystemWatcher, as well as making sure that when you are finished with it, you dispose it.
-- HTH,
Kevin Spencer Microsoft MVP .Net Developer Sometimes you eat the elephant. Sometimes the elephant eats you.
"J-T" <Ra****@microsft.com> wrote in message news:e5**************@TK2MSFTNGP14.phx.gbl... Here is what I have done .I have created a class and I instanciate that class in Application_Start method of my Global.asax ere is the code:
********Gloabl.asax: //Sets up a wacher on an specific shared folder to pickup zip files
IFPWatcherComponent ifpWacher=new IFPWatcherComponent(ConfigurationSettings.AppSetti ngs[ "MonitorPath"]); ********MyClass:
/// </summary> public class IFPWatcherComponent:BusinessObject { string PathToMonitor=null;
public IFPWatcherComponent(string PathToMonitor) {
try { this.PathToMonitor = PathToMonitor; //Check to see if Web.config has appropriate settings if (this.PathToMonitor.Trim().Length==0 ) return; // Make a reference to a directory. DirectoryInfo di = new DirectoryInfo(this.PathToMonitor); // Create the directory only if it does not already exist. if (di.Exists == false) di.Create(); // create an instance of FileSystemWatcher object and assign path to monitor FileSystemWatcher watcher = new FileSystemWatcher(); // set necessary filters watcher.Path = this.PathToMonitor; //Watch for changes in FileName watcher.NotifyFilter = NotifyFilters.FileName; // watch zip files watcher.Filter = "*.zip"; //Add appropriate event handler watcher.Created += new FileSystemEventHandler(OnCreated); watcher.EnableRaisingEvents = true; } catch { throw; } } private void OnCreated(object source, FileSystemEventArgs e) { FileStream oImg=null; BinaryReader oBinaryReader=null;
Uploader uploader=new Uploader(); uploader.ComeFrom="88"; uploader.Comments="comment"; uploader.CreatedByUser="test"; uploader.FileName=e.Name;
try {
oImg = new FileStream (e.FullPath,FileMode.Open,FileAccess.Read); oBinaryReader = new BinaryReader(oImg); byte[] oImgByteArray = oBinaryReader.ReadBytes((int) oImg.Length); uploader.FileBody=oImgByteArray;
uploader.RecordType=Business.IFP.RecordType.IFP_UP LOAD; uploader.Upload(); uploader=null;
} catch(Exception ee) { *******************I don't know how to handle the exception here??*********************** } finally { if (oBinaryReader!=null) oBinaryReader.Close(); if (oImg!=null) oImg.Close();
} }
"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message news:ud**************@TK2MSFTNGP09.phx.gbl... Are you sure you are on the right track? A FileSystemWatcher object has to exist somewhere to be able to watch. An asp.net application doesn't exist anywhere but between a client http request and the server response. A very short time. Do you expect the watcher to catch something only when the server is busy serving client requests?
Eliyahu
"J-T" <Ra****@microsft.com> wrote in message news:%2****************@TK2MSFTNGP15.phx.gbl... We are working on an asp.net application which is a 3-tier application.I was aksed to create a component which monitors a folder and gets the file and pass them to a class library in our business logic layer(so far so good and easy).I initialize my class which is using a FileSystemWatcher in my Global.asax and everything works fine.I have found FileSystemWatcher class not very reliable and sometimes it behavies unexpectedly.I'm afriad that it brings down the whole application.Is there a better way of doing this **Inside Asp.Net application**.I can't use antother application like windows service or schedault taks.Everything needs to be done is ASP.NET application.
Thanks a lot
I forgot to say that if I keep my object in an application variable,then the
persistence problem will be solved,right?
Thanks
"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:u8****************@tk2msftngp13.phx.gbl... Your FileSystemWatcher is scoped to the method in which you instantiate it. That means that it goes out of scope and is not available as soon as the method returns. It may hang around for a bit since you have no code to dispose it at any point, but you need to bone up on scope and state. Here's a primer:
Scope is the "area of influence" of a variable. In a class, you have global and local variables. Global variables are declared outside of any methods, and are therefore "visible" to all members of the class. Local variables are declared inside a method, and are therefore "visible" only to other variables within a single instance (call) of the method. Once the method returns, it is removed from the stack, and everything in it becomes available for garbage collection.
Note that this differs from public, private, etc. These are also definitions of scope, but are more related to encapsulation. Public members of a class are "visible" from other classes. Private members are not. Etc.
State refers to the persistence of an object (class instance or primitive) in memory. Whenever an object is instantiated (created), it must reside in memory somewhere. The lifetime of an object is limited to the lifetime of the container in which it resides. A method is instantiated, just like a variable, whenever you call it. And just like a variable, when it returns, it is available for garbage collection. The only way to persist an instance is to put it into something else that persists. Hence, ASP.Net, which operates using HTTP, which is stateless, has mechanisms for maintaining state, such as Session, Application, and ViewState.
The global.asax class, as I mentioned in my earlier reply, is a persistent class, but it's methods are not. They are instantiated like any other methods, and pass out of scope as soon as they return.
I hope you will study what I've said, and continue to study how OOP works as you go. In the meantime, you need to understand how to persist your FileSystemWatcher, as well as making sure that when you are finished with it, you dispose it.
-- HTH,
Kevin Spencer Microsoft MVP .Net Developer Sometimes you eat the elephant. Sometimes the elephant eats you.
"J-T" <Ra****@microsft.com> wrote in message news:e5**************@TK2MSFTNGP14.phx.gbl... Here is what I have done .I have created a class and I instanciate that class in Application_Start method of my Global.asax ere is the code:
********Gloabl.asax: //Sets up a wacher on an specific shared folder to pickup zip files
IFPWatcherComponent ifpWacher=new IFPWatcherComponent(ConfigurationSettings.AppSetti ngs[ "MonitorPath"]); ********MyClass:
/// </summary> public class IFPWatcherComponent:BusinessObject { string PathToMonitor=null;
public IFPWatcherComponent(string PathToMonitor) {
try { this.PathToMonitor = PathToMonitor; //Check to see if Web.config has appropriate settings if (this.PathToMonitor.Trim().Length==0 ) return; // Make a reference to a directory. DirectoryInfo di = new DirectoryInfo(this.PathToMonitor); // Create the directory only if it does not already exist. if (di.Exists == false) di.Create(); // create an instance of FileSystemWatcher object and assign path to monitor FileSystemWatcher watcher = new FileSystemWatcher(); // set necessary filters watcher.Path = this.PathToMonitor; //Watch for changes in FileName watcher.NotifyFilter = NotifyFilters.FileName; // watch zip files watcher.Filter = "*.zip"; //Add appropriate event handler watcher.Created += new FileSystemEventHandler(OnCreated); watcher.EnableRaisingEvents = true; } catch { throw; } } private void OnCreated(object source, FileSystemEventArgs e) { FileStream oImg=null; BinaryReader oBinaryReader=null;
Uploader uploader=new Uploader(); uploader.ComeFrom="88"; uploader.Comments="comment"; uploader.CreatedByUser="test"; uploader.FileName=e.Name;
try {
oImg = new FileStream (e.FullPath,FileMode.Open,FileAccess.Read); oBinaryReader = new BinaryReader(oImg); byte[] oImgByteArray = oBinaryReader.ReadBytes((int) oImg.Length); uploader.FileBody=oImgByteArray;
uploader.RecordType=Business.IFP.RecordType.IFP_UP LOAD; uploader.Upload(); uploader=null;
} catch(Exception ee) { *******************I don't know how to handle the exception here??*********************** } finally { if (oBinaryReader!=null) oBinaryReader.Close(); if (oImg!=null) oImg.Close();
} }
"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message news:ud**************@TK2MSFTNGP09.phx.gbl... Are you sure you are on the right track? A FileSystemWatcher object has to exist somewhere to be able to watch. An asp.net application doesn't exist anywhere but between a client http request and the server response. A very short time. Do you expect the watcher to catch something only when the server is busy serving client requests?
Eliyahu
"J-T" <Ra****@microsft.com> wrote in message news:%2****************@TK2MSFTNGP15.phx.gbl... We are working on an asp.net application which is a 3-tier application.I was aksed to create a component which monitors a folder and gets the file and pass them to a class library in our business logic layer(so far so good and easy).I initialize my class which is using a FileSystemWatcher in my Global.asax and everything works fine.I have found FileSystemWatcher class not very reliable and sometimes it behavies unexpectedly.I'm afriad that it brings down the whole application.Is there a better way of doing this **Inside Asp.Net application**.I can't use antother application like windows service or schedault taks.Everything needs to be done is ASP.NET application.
Thanks a lot
Even if you persist an object in the application, how can you make sure the
application is running? Did you consider windows service?
Eliyahu
"J-T" <Ra****@microsft.com> wrote in message
news:Oy**************@TK2MSFTNGP12.phx.gbl... So you mean state wise and persistence wise,both,I will have problems by using this code?
Do you have any idea how to persist my object.I want it to avaialable as long as they application is up and working.So according to what you
suggest I will have to make the scope of my object some how global (not like the
way it is now) and also persist it in the memory for the time application is
up. Do I really have to dispose the object after the application gets stopped?
Thanks for super informative answer.
Ray "Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message news:u8****************@tk2msftngp13.phx.gbl... Your FileSystemWatcher is scoped to the method in which you instantiate it. That means that it goes out of scope and is not available as soon as the method returns. It may hang around for a bit since you have no code
to dispose it at any point, but you need to bone up on scope and state. Here's a primer:
Scope is the "area of influence" of a variable. In a class, you have global and local variables. Global variables are declared outside of any methods, and are therefore "visible" to all members of the class. Local variables are declared inside a method, and are therefore "visible" only to other variables within a single instance (call) of the method. Once
the method returns, it is removed from the stack, and everything in it
becomes available for garbage collection.
Note that this differs from public, private, etc. These are also definitions of scope, but are more related to encapsulation. Public members of a class are "visible" from other classes. Private members are not. Etc.
State refers to the persistence of an object (class instance or
primitive) in memory. Whenever an object is instantiated (created), it must reside
in memory somewhere. The lifetime of an object is limited to the lifetime
of the container in which it resides. A method is instantiated, just like a variable, whenever you call it. And just like a variable, when it
returns, it is available for garbage collection. The only way to persist an instance is to put it into something else that persists. Hence, ASP.Net, which operates using HTTP, which is stateless, has mechanisms for maintaining state, such as Session, Application, and ViewState.
The global.asax class, as I mentioned in my earlier reply, is a
persistent class, but it's methods are not. They are instantiated like any other methods, and pass out of scope as soon as they return.
I hope you will study what I've said, and continue to study how OOP
works as you go. In the meantime, you need to understand how to persist your FileSystemWatcher, as well as making sure that when you are finished
with it, you dispose it.
-- HTH,
Kevin Spencer Microsoft MVP .Net Developer Sometimes you eat the elephant. Sometimes the elephant eats you.
"J-T" <Ra****@microsft.com> wrote in message news:e5**************@TK2MSFTNGP14.phx.gbl... Here is what I have done .I have created a class and I instanciate that class in Application_Start method of my Global.asax ere is the code:
********Gloabl.asax: //Sets up a wacher on an specific shared folder to pickup zip files
IFPWatcherComponent ifpWacher=new IFPWatcherComponent(ConfigurationSettings.AppSetti ngs[ "MonitorPath"]); ********MyClass:
/// </summary> public class IFPWatcherComponent:BusinessObject { string PathToMonitor=null;
public IFPWatcherComponent(string PathToMonitor) {
try { this.PathToMonitor = PathToMonitor; //Check to see if Web.config has appropriate settings if (this.PathToMonitor.Trim().Length==0 ) return; // Make a reference to a directory. DirectoryInfo di = new DirectoryInfo(this.PathToMonitor); // Create the directory only if it does not already exist. if (di.Exists == false) di.Create(); // create an instance of FileSystemWatcher object and assign path to monitor FileSystemWatcher watcher = new FileSystemWatcher(); // set necessary filters watcher.Path = this.PathToMonitor; //Watch for changes in FileName watcher.NotifyFilter = NotifyFilters.FileName; // watch zip files watcher.Filter = "*.zip"; //Add appropriate event handler watcher.Created += new FileSystemEventHandler(OnCreated); watcher.EnableRaisingEvents = true; } catch { throw; } } private void OnCreated(object source, FileSystemEventArgs e) { FileStream oImg=null; BinaryReader oBinaryReader=null;
Uploader uploader=new Uploader(); uploader.ComeFrom="88"; uploader.Comments="comment"; uploader.CreatedByUser="test"; uploader.FileName=e.Name;
try {
oImg = new FileStream (e.FullPath,FileMode.Open,FileAccess.Read); oBinaryReader = new BinaryReader(oImg); byte[] oImgByteArray = oBinaryReader.ReadBytes((int) oImg.Length); uploader.FileBody=oImgByteArray;
uploader.RecordType=Business.IFP.RecordType.IFP_UP LOAD; uploader.Upload(); uploader=null;
} catch(Exception ee) { *******************I don't know how to handle the exception here??*********************** } finally { if (oBinaryReader!=null) oBinaryReader.Close(); if (oImg!=null) oImg.Close();
} }
"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message news:ud**************@TK2MSFTNGP09.phx.gbl... Are you sure you are on the right track? A FileSystemWatcher object
has to exist somewhere to be able to watch. An asp.net application doesn't exist anywhere but between a client http request and the server response. A very short time. Do you expect the watcher to catch something only when the server is busy serving client requests?
Eliyahu
"J-T" <Ra****@microsft.com> wrote in message news:%2****************@TK2MSFTNGP15.phx.gbl... > We are working on an asp.net application which is a 3-tier > application.I was > aksed to create a component which monitors a folder and gets the file > and > pass them to a class library in our business logic layer(so far so
good and > easy).I initialize my class which is using a FileSystemWatcher in my > Global.asax and everything works fine.I have found FileSystemWatcher class > not very reliable and sometimes it behavies unexpectedly.I'm afriad > that it > brings down the whole application.Is there a better way of doing this > **Inside Asp.Net application**.I can't use antother application like windows > service or schedault taks.Everything needs to be done is ASP.NET > application. > > > Thanks a lot > >
You can set up a scheduled task that will copy files over to a local
directory periodically. Not very elegant, but works.
Eliyahu
"J-T" <Ra****@microsft.com> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl... So how would you monitor a network path in your asp.net application? Thanks for your reply.
"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message news:e7**************@TK2MSFTNGP12.phx.gbl... This could be a problem. In my experience I could not get FileSystemWatcher to work for network directories. Either it is not able to or some tricky setting is needed.
Eliyahu
"J-T" <Ra****@microsft.com> wrote in message news:Ov**************@tk2msftngp13.phx.gbl... I forgot to say that directory which I monitor is a shared folder on
the netwrok.
Thanks "Eliyahu Goldin" <re*************@monarchmed.com> wrote in message news:ud**************@TK2MSFTNGP09.phx.gbl... > Are you sure you are on the right track? A FileSystemWatcher object
has to > exist somewhere to be able to watch. An asp.net application doesn't exist > anywhere but between a client http request and the server response. A very > short time. Do you expect the watcher to catch something only when
the > server is busy serving client requests? > > Eliyahu > > > "J-T" <Ra****@microsft.com> wrote in message > news:%2****************@TK2MSFTNGP15.phx.gbl... >> We are working on an asp.net application which is a 3-tier application.I > was >> aksed to create a component which monitors a folder and gets the
file and >> pass them to a class library in our business logic layer(so far so >> good > and >> easy).I initialize my class which is using a FileSystemWatcher in my >> Global.asax and everything works fine.I have found
FileSystemWatcher > class >> not very reliable and sometimes it behavies unexpectedly.I'm afriad that > it >> brings down the whole application.Is there a better way of doing
this >> **Inside Asp.Net application**.I can't use antother application like > windows >> service or schedault taks.Everything needs to be done is ASP.NET >> application. >> >> >> Thanks a lot >> >> > >
>I forgot to say that if I keep my object in an application variable,then the persistence problem will be solved,right?
Yes. However, be aware that an ASP.Net application will stop running when it
is idle (no requests) for a period of more than (by default) 20 minutes.
When the application is stopped, the FileSystemWatcher will be stopped. When
the application restarts, the FileSystemWatcher will be restarted as well.
HOWEVER, any changes to the file system that the Watcher is supposed to
watch during the idle interval will go unnoticed. Now, as long as changes to
the files in the folder are also done by the ASP.Net app, there will BE no
changes while the app is stopped. So, while I can't answer your question
directly, this information, plus what you already know, should give you the
solution you need.
--
HTH,
Kevin Spencer
Microsoft MVP
..Net Developer
Sometimes you eat the elephant.
Sometimes the elephant eats you.
"J-T" <Ra****@microsft.com> wrote in message
news:eJ***************@TK2MSFTNGP12.phx.gbl...I forgot to say that if I keep my object in an application variable,then the persistence problem will be solved,right?
Thanks "Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message news:u8****************@tk2msftngp13.phx.gbl... Your FileSystemWatcher is scoped to the method in which you instantiate it. That means that it goes out of scope and is not available as soon as the method returns. It may hang around for a bit since you have no code to dispose it at any point, but you need to bone up on scope and state. Here's a primer:
Scope is the "area of influence" of a variable. In a class, you have global and local variables. Global variables are declared outside of any methods, and are therefore "visible" to all members of the class. Local variables are declared inside a method, and are therefore "visible" only to other variables within a single instance (call) of the method. Once the method returns, it is removed from the stack, and everything in it becomes available for garbage collection.
Note that this differs from public, private, etc. These are also definitions of scope, but are more related to encapsulation. Public members of a class are "visible" from other classes. Private members are not. Etc.
State refers to the persistence of an object (class instance or primitive) in memory. Whenever an object is instantiated (created), it must reside in memory somewhere. The lifetime of an object is limited to the lifetime of the container in which it resides. A method is instantiated, just like a variable, whenever you call it. And just like a variable, when it returns, it is available for garbage collection. The only way to persist an instance is to put it into something else that persists. Hence, ASP.Net, which operates using HTTP, which is stateless, has mechanisms for maintaining state, such as Session, Application, and ViewState.
The global.asax class, as I mentioned in my earlier reply, is a persistent class, but it's methods are not. They are instantiated like any other methods, and pass out of scope as soon as they return.
I hope you will study what I've said, and continue to study how OOP works as you go. In the meantime, you need to understand how to persist your FileSystemWatcher, as well as making sure that when you are finished with it, you dispose it.
-- HTH,
Kevin Spencer Microsoft MVP .Net Developer Sometimes you eat the elephant. Sometimes the elephant eats you.
"J-T" <Ra****@microsft.com> wrote in message news:e5**************@TK2MSFTNGP14.phx.gbl... Here is what I have done .I have created a class and I instanciate that class in Application_Start method of my Global.asax ere is the code:
********Gloabl.asax: //Sets up a wacher on an specific shared folder to pickup zip files
IFPWatcherComponent ifpWacher=new IFPWatcherComponent(ConfigurationSettings.AppSetti ngs[ "MonitorPath"]); ********MyClass:
/// </summary> public class IFPWatcherComponent:BusinessObject { string PathToMonitor=null;
public IFPWatcherComponent(string PathToMonitor) {
try { this.PathToMonitor = PathToMonitor; //Check to see if Web.config has appropriate settings if (this.PathToMonitor.Trim().Length==0 ) return; // Make a reference to a directory. DirectoryInfo di = new DirectoryInfo(this.PathToMonitor); // Create the directory only if it does not already exist. if (di.Exists == false) di.Create(); // create an instance of FileSystemWatcher object and assign path to monitor FileSystemWatcher watcher = new FileSystemWatcher(); // set necessary filters watcher.Path = this.PathToMonitor; //Watch for changes in FileName watcher.NotifyFilter = NotifyFilters.FileName; // watch zip files watcher.Filter = "*.zip"; //Add appropriate event handler watcher.Created += new FileSystemEventHandler(OnCreated); watcher.EnableRaisingEvents = true; } catch { throw; } } private void OnCreated(object source, FileSystemEventArgs e) { FileStream oImg=null; BinaryReader oBinaryReader=null;
Uploader uploader=new Uploader(); uploader.ComeFrom="88"; uploader.Comments="comment"; uploader.CreatedByUser="test"; uploader.FileName=e.Name;
try {
oImg = new FileStream (e.FullPath,FileMode.Open,FileAccess.Read); oBinaryReader = new BinaryReader(oImg); byte[] oImgByteArray = oBinaryReader.ReadBytes((int) oImg.Length); uploader.FileBody=oImgByteArray;
uploader.RecordType=Business.IFP.RecordType.IFP_UP LOAD; uploader.Upload(); uploader=null;
} catch(Exception ee) { *******************I don't know how to handle the exception here??*********************** } finally { if (oBinaryReader!=null) oBinaryReader.Close(); if (oImg!=null) oImg.Close();
} }
"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message news:ud**************@TK2MSFTNGP09.phx.gbl... Are you sure you are on the right track? A FileSystemWatcher object has to exist somewhere to be able to watch. An asp.net application doesn't exist anywhere but between a client http request and the server response. A very short time. Do you expect the watcher to catch something only when the server is busy serving client requests?
Eliyahu
"J-T" <Ra****@microsft.com> wrote in message news:%2****************@TK2MSFTNGP15.phx.gbl... > We are working on an asp.net application which is a 3-tier > application.I was > aksed to create a component which monitors a folder and gets the file > and > pass them to a class library in our business logic layer(so far so > good and > easy).I initialize my class which is using a FileSystemWatcher in my > Global.asax and everything works fine.I have found FileSystemWatcher class > not very reliable and sometimes it behavies unexpectedly.I'm afriad > that it > brings down the whole application.Is there a better way of doing this > **Inside Asp.Net application**.I can't use antother application like windows > service or schedault taks.Everything needs to be done is ASP.NET > application. > > > Thanks a lot > >
How about this :
Make my object remotly callable and register an sponser in the same
machine(Webserver) to call my object when it is going to be leased?
Makes sense?
Thanks for your help.
"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:u4**************@tk2msftngp13.phx.gbl... I forgot to say that if I keep my object in an application variable,then the persistence problem will be solved,right?
Yes. However, be aware that an ASP.Net application will stop running when it is idle (no requests) for a period of more than (by default) 20 minutes. When the application is stopped, the FileSystemWatcher will be stopped. When the application restarts, the FileSystemWatcher will be restarted as well. HOWEVER, any changes to the file system that the Watcher is supposed to watch during the idle interval will go unnoticed. Now, as long as changes to the files in the folder are also done by the ASP.Net app, there will BE no changes while the app is stopped. So, while I can't answer your question directly, this information, plus what you already know, should give you the solution you need.
-- HTH,
Kevin Spencer Microsoft MVP .Net Developer Sometimes you eat the elephant. Sometimes the elephant eats you.
"J-T" <Ra****@microsft.com> wrote in message news:eJ***************@TK2MSFTNGP12.phx.gbl...I forgot to say that if I keep my object in an application variable,then the persistence problem will be solved,right?
Thanks "Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message news:u8****************@tk2msftngp13.phx.gbl... Your FileSystemWatcher is scoped to the method in which you instantiate it. That means that it goes out of scope and is not available as soon as the method returns. It may hang around for a bit since you have no code to dispose it at any point, but you need to bone up on scope and state. Here's a primer:
Scope is the "area of influence" of a variable. In a class, you have global and local variables. Global variables are declared outside of any methods, and are therefore "visible" to all members of the class. Local variables are declared inside a method, and are therefore "visible" only to other variables within a single instance (call) of the method. Once the method returns, it is removed from the stack, and everything in it becomes available for garbage collection.
Note that this differs from public, private, etc. These are also definitions of scope, but are more related to encapsulation. Public members of a class are "visible" from other classes. Private members are not. Etc.
State refers to the persistence of an object (class instance or primitive) in memory. Whenever an object is instantiated (created), it must reside in memory somewhere. The lifetime of an object is limited to the lifetime of the container in which it resides. A method is instantiated, just like a variable, whenever you call it. And just like a variable, when it returns, it is available for garbage collection. The only way to persist an instance is to put it into something else that persists. Hence, ASP.Net, which operates using HTTP, which is stateless, has mechanisms for maintaining state, such as Session, Application, and ViewState.
The global.asax class, as I mentioned in my earlier reply, is a persistent class, but it's methods are not. They are instantiated like any other methods, and pass out of scope as soon as they return.
I hope you will study what I've said, and continue to study how OOP works as you go. In the meantime, you need to understand how to persist your FileSystemWatcher, as well as making sure that when you are finished with it, you dispose it.
-- HTH,
Kevin Spencer Microsoft MVP .Net Developer Sometimes you eat the elephant. Sometimes the elephant eats you.
"J-T" <Ra****@microsft.com> wrote in message news:e5**************@TK2MSFTNGP14.phx.gbl... Here is what I have done .I have created a class and I instanciate that class in Application_Start method of my Global.asax ere is the code:
********Gloabl.asax: //Sets up a wacher on an specific shared folder to pickup zip files
IFPWatcherComponent ifpWacher=new IFPWatcherComponent(ConfigurationSettings.AppSetti ngs[ "MonitorPath"]); ********MyClass:
/// </summary> public class IFPWatcherComponent:BusinessObject { string PathToMonitor=null;
public IFPWatcherComponent(string PathToMonitor) {
try { this.PathToMonitor = PathToMonitor; //Check to see if Web.config has appropriate settings if (this.PathToMonitor.Trim().Length==0 ) return; // Make a reference to a directory. DirectoryInfo di = new DirectoryInfo(this.PathToMonitor); // Create the directory only if it does not already exist. if (di.Exists == false) di.Create(); // create an instance of FileSystemWatcher object and assign path to monitor FileSystemWatcher watcher = new FileSystemWatcher(); // set necessary filters watcher.Path = this.PathToMonitor; //Watch for changes in FileName watcher.NotifyFilter = NotifyFilters.FileName; // watch zip files watcher.Filter = "*.zip"; //Add appropriate event handler watcher.Created += new FileSystemEventHandler(OnCreated); watcher.EnableRaisingEvents = true; } catch { throw; } } private void OnCreated(object source, FileSystemEventArgs e) { FileStream oImg=null; BinaryReader oBinaryReader=null;
Uploader uploader=new Uploader(); uploader.ComeFrom="88"; uploader.Comments="comment"; uploader.CreatedByUser="test"; uploader.FileName=e.Name;
try {
oImg = new FileStream (e.FullPath,FileMode.Open,FileAccess.Read); oBinaryReader = new BinaryReader(oImg); byte[] oImgByteArray = oBinaryReader.ReadBytes((int) oImg.Length); uploader.FileBody=oImgByteArray;
uploader.RecordType=Business.IFP.RecordType.IFP_UP LOAD; uploader.Upload(); uploader=null;
} catch(Exception ee) { *******************I don't know how to handle the exception here??*********************** } finally { if (oBinaryReader!=null) oBinaryReader.Close(); if (oImg!=null) oImg.Close();
} }
"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message news:ud**************@TK2MSFTNGP09.phx.gbl... > Are you sure you are on the right track? A FileSystemWatcher object > has to > exist somewhere to be able to watch. An asp.net application doesn't > exist > anywhere but between a client http request and the server response. A > very > short time. Do you expect the watcher to catch something only when the > server is busy serving client requests? > > Eliyahu > > > "J-T" <Ra****@microsft.com> wrote in message > news:%2****************@TK2MSFTNGP15.phx.gbl... >> We are working on an asp.net application which is a 3-tier >> application.I > was >> aksed to create a component which monitors a folder and gets the file >> and >> pass them to a class library in our business logic layer(so far so >> good > and >> easy).I initialize my class which is using a FileSystemWatcher in my >> Global.asax and everything works fine.I have found FileSystemWatcher > class >> not very reliable and sometimes it behavies unexpectedly.I'm afriad >> that > it >> brings down the whole application.Is there a better way of doing this >> **Inside Asp.Net application**.I can't use antother application like > windows >> service or schedault taks.Everything needs to be done is ASP.NET >> application. >> >> >> Thanks a lot >> >> > >
The reason I'm bearing with all these problems is that I don;t want to use a
seperate component outside of our asp.net application.What do you mean by
using a windows service? you mean using windows service to keep the object
alive ? kind of sponser thing in remoting?
Thanks
"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message
news:Oy**************@TK2MSFTNGP14.phx.gbl... Even if you persist an object in the application, how can you make sure the application is running? Did you consider windows service?
Eliyahu
"J-T" <Ra****@microsft.com> wrote in message news:Oy**************@TK2MSFTNGP12.phx.gbl... So you mean state wise and persistence wise,both,I will have problems by using this code?
Do you have any idea how to persist my object.I want it to avaialable as long as they application is up and working.So according to what you suggest I will have to make the scope of my object some how global (not like the way it is now) and also persist it in the memory for the time application is up. Do I really have to dispose the object after the application gets stopped?
Thanks for super informative answer.
Ray "Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message news:u8****************@tk2msftngp13.phx.gbl... > Your FileSystemWatcher is scoped to the method in which you instantiate > it. That means that it goes out of scope and is not available as soon > as > the method returns. It may hang around for a bit since you have no code to > dispose it at any point, but you need to bone up on scope and state. > Here's a primer: > > Scope is the "area of influence" of a variable. In a class, you have > global and local variables. Global variables are declared outside of > any > methods, and are therefore "visible" to all members of the class. Local > variables are declared inside a method, and are therefore "visible" > only > to other variables within a single instance (call) of the method. Once the > method returns, it is removed from the stack, and everything in it becomes > available for garbage collection. > > Note that this differs from public, private, etc. These are also > definitions of scope, but are more related to encapsulation. Public > members of a class are "visible" from other classes. Private members > are > not. Etc. > > State refers to the persistence of an object (class instance or primitive) > in memory. Whenever an object is instantiated (created), it must reside in > memory somewhere. The lifetime of an object is limited to the lifetime of > the container in which it resides. A method is instantiated, just like > a > variable, whenever you call it. And just like a variable, when it returns, > it is available for garbage collection. The only way to persist an > instance is to put it into something else that persists. Hence, > ASP.Net, > which operates using HTTP, which is stateless, has mechanisms for > maintaining state, such as Session, Application, and ViewState. > > The global.asax class, as I mentioned in my earlier reply, is a persistent > class, but it's methods are not. They are instantiated like any other > methods, and pass out of scope as soon as they return. > > I hope you will study what I've said, and continue to study how OOP works > as you go. In the meantime, you need to understand how to persist your > FileSystemWatcher, as well as making sure that when you are finished with > it, you dispose it. > > -- > HTH, > > Kevin Spencer > Microsoft MVP > .Net Developer > Sometimes you eat the elephant. > Sometimes the elephant eats you. > > "J-T" <Ra****@microsft.com> wrote in message > news:e5**************@TK2MSFTNGP14.phx.gbl... >> Here is what I have done .I have created a class and I instanciate >> that >> class in Application_Start method of my Global.asax ere is the code: >> >> ********Gloabl.asax: >> //Sets up a wacher on an specific shared folder to pickup zip files >> >> IFPWatcherComponent ifpWacher=new >> IFPWatcherComponent(ConfigurationSettings.AppSetti ngs[ >> "MonitorPath"]); >> >> >> >> ********MyClass: >> >> /// </summary> >> public class IFPWatcherComponent:BusinessObject >> { >> string PathToMonitor=null; >> >> public IFPWatcherComponent(string PathToMonitor) >> { >> >> try >> { >> this.PathToMonitor = PathToMonitor; >> //Check to see if Web.config has appropriate settings >> if (this.PathToMonitor.Trim().Length==0 ) return; >> // Make a reference to a directory. >> DirectoryInfo di = new DirectoryInfo(this.PathToMonitor); >> // Create the directory only if it does not already exist. >> if (di.Exists == false) >> di.Create(); >> // create an instance of FileSystemWatcher object and assign path >> to >> monitor >> FileSystemWatcher watcher = new FileSystemWatcher(); >> // set necessary filters >> watcher.Path = this.PathToMonitor; >> //Watch for changes in FileName >> watcher.NotifyFilter = NotifyFilters.FileName; >> // watch zip files >> watcher.Filter = "*.zip"; >> //Add appropriate event handler >> watcher.Created += new FileSystemEventHandler(OnCreated); >> watcher.EnableRaisingEvents = true; >> } >> catch >> { >> throw; >> } >> } >> private void OnCreated(object source, FileSystemEventArgs e) >> { >> FileStream oImg=null; >> BinaryReader oBinaryReader=null; >> >> Uploader uploader=new Uploader(); >> uploader.ComeFrom="88"; >> uploader.Comments="comment"; >> uploader.CreatedByUser="test"; >> uploader.FileName=e.Name; >> >> try >> { >> >> oImg = new FileStream (e.FullPath,FileMode.Open,FileAccess.Read); >> oBinaryReader = new BinaryReader(oImg); >> byte[] oImgByteArray = oBinaryReader.ReadBytes((int) oImg.Length); >> uploader.FileBody=oImgByteArray; >> >> uploader.RecordType=Business.IFP.RecordType.IFP_UP LOAD; >> uploader.Upload(); >> uploader=null; >> >> } >> catch(Exception ee) >> { >> *******************I don't know how to handle the exception >> here??*********************** >> } >> finally >> { >> if (oBinaryReader!=null) oBinaryReader.Close(); >> if (oImg!=null) oImg.Close(); >> >> } >> } >> >> "Eliyahu Goldin" <re*************@monarchmed.com> wrote in message >> news:ud**************@TK2MSFTNGP09.phx.gbl... >>> Are you sure you are on the right track? A FileSystemWatcher object has >>> to >>> exist somewhere to be able to watch. An asp.net application doesn't >>> exist >>> anywhere but between a client http request and the server response. A >>> very >>> short time. Do you expect the watcher to catch something only when >>> the >>> server is busy serving client requests? >>> >>> Eliyahu >>> >>> >>> "J-T" <Ra****@microsft.com> wrote in message >>> news:%2****************@TK2MSFTNGP15.phx.gbl... >>>> We are working on an asp.net application which is a 3-tier >>>> application.I >>> was >>>> aksed to create a component which monitors a folder and gets the >>>> file >>>> and >>>> pass them to a class library in our business logic layer(so far so good >>> and >>>> easy).I initialize my class which is using a FileSystemWatcher in my >>>> Global.asax and everything works fine.I have found >>>> FileSystemWatcher >>> class >>>> not very reliable and sometimes it behavies unexpectedly.I'm afriad >>>> that >>> it >>>> brings down the whole application.Is there a better way of doing >>>> this >>>> **Inside Asp.Net application**.I can't use antother application like >>> windows >>>> service or schedault taks.Everything needs to be done is ASP.NET >>>> application. >>>> >>>> >>>> Thanks a lot >>>> >>>> >>> >>> >> >> > >
You can't remotely call an object. An object is a type, just like an
Integer, or a Function. It exists only as an instance inside an application.
So, you have to write a service if you want it to run all the time. You
COULD keep your ASP.Net Application alive by making Requests to the app
shortly before the Application Timeout, but that would require a client
application to be running all the time.
--
HTH,
Kevin Spencer
Microsoft MVP
..Net Developer
Sometimes you eat the elephant.
Sometimes the elephant eats you.
"Ray5531" <Ra****@microsft.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl... How about this :
Make my object remotly callable and register an sponser in the same machine(Webserver) to call my object when it is going to be leased?
Makes sense?
Thanks for your help.
"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message news:u4**************@tk2msftngp13.phx.gbl... >I forgot to say that if I keep my object in an application variable,then >the persistence problem will be solved,right?
Yes. However, be aware that an ASP.Net application will stop running when it is idle (no requests) for a period of more than (by default) 20 minutes. When the application is stopped, the FileSystemWatcher will be stopped. When the application restarts, the FileSystemWatcher will be restarted as well. HOWEVER, any changes to the file system that the Watcher is supposed to watch during the idle interval will go unnoticed. Now, as long as changes to the files in the folder are also done by the ASP.Net app, there will BE no changes while the app is stopped. So, while I can't answer your question directly, this information, plus what you already know, should give you the solution you need.
-- HTH,
Kevin Spencer Microsoft MVP .Net Developer Sometimes you eat the elephant. Sometimes the elephant eats you.
"J-T" <Ra****@microsft.com> wrote in message news:eJ***************@TK2MSFTNGP12.phx.gbl...I forgot to say that if I keep my object in an application variable,then the persistence problem will be solved,right?
Thanks "Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message news:u8****************@tk2msftngp13.phx.gbl... Your FileSystemWatcher is scoped to the method in which you instantiate it. That means that it goes out of scope and is not available as soon as the method returns. It may hang around for a bit since you have no code to dispose it at any point, but you need to bone up on scope and state. Here's a primer:
Scope is the "area of influence" of a variable. In a class, you have global and local variables. Global variables are declared outside of any methods, and are therefore "visible" to all members of the class. Local variables are declared inside a method, and are therefore "visible" only to other variables within a single instance (call) of the method. Once the method returns, it is removed from the stack, and everything in it becomes available for garbage collection.
Note that this differs from public, private, etc. These are also definitions of scope, but are more related to encapsulation. Public members of a class are "visible" from other classes. Private members are not. Etc.
State refers to the persistence of an object (class instance or primitive) in memory. Whenever an object is instantiated (created), it must reside in memory somewhere. The lifetime of an object is limited to the lifetime of the container in which it resides. A method is instantiated, just like a variable, whenever you call it. And just like a variable, when it returns, it is available for garbage collection. The only way to persist an instance is to put it into something else that persists. Hence, ASP.Net, which operates using HTTP, which is stateless, has mechanisms for maintaining state, such as Session, Application, and ViewState.
The global.asax class, as I mentioned in my earlier reply, is a persistent class, but it's methods are not. They are instantiated like any other methods, and pass out of scope as soon as they return.
I hope you will study what I've said, and continue to study how OOP works as you go. In the meantime, you need to understand how to persist your FileSystemWatcher, as well as making sure that when you are finished with it, you dispose it.
-- HTH,
Kevin Spencer Microsoft MVP .Net Developer Sometimes you eat the elephant. Sometimes the elephant eats you.
"J-T" <Ra****@microsft.com> wrote in message news:e5**************@TK2MSFTNGP14.phx.gbl... > Here is what I have done .I have created a class and I instanciate > that class in Application_Start method of my Global.asax ere is the > code: > > ********Gloabl.asax: > //Sets up a wacher on an specific shared folder to pickup zip files > > IFPWatcherComponent ifpWacher=new > IFPWatcherComponent(ConfigurationSettings.AppSetti ngs[ > "MonitorPath"]); > > > > ********MyClass: > > /// </summary> > public class IFPWatcherComponent:BusinessObject > { > string PathToMonitor=null; > > public IFPWatcherComponent(string PathToMonitor) > { > > try > { > this.PathToMonitor = PathToMonitor; > //Check to see if Web.config has appropriate settings > if (this.PathToMonitor.Trim().Length==0 ) return; > // Make a reference to a directory. > DirectoryInfo di = new DirectoryInfo(this.PathToMonitor); > // Create the directory only if it does not already exist. > if (di.Exists == false) > di.Create(); > // create an instance of FileSystemWatcher object and assign path > to monitor > FileSystemWatcher watcher = new FileSystemWatcher(); > // set necessary filters > watcher.Path = this.PathToMonitor; > //Watch for changes in FileName > watcher.NotifyFilter = NotifyFilters.FileName; > // watch zip files > watcher.Filter = "*.zip"; > //Add appropriate event handler > watcher.Created += new FileSystemEventHandler(OnCreated); > watcher.EnableRaisingEvents = true; > } > catch > { > throw; > } > } > private void OnCreated(object source, FileSystemEventArgs e) > { > FileStream oImg=null; > BinaryReader oBinaryReader=null; > > Uploader uploader=new Uploader(); > uploader.ComeFrom="88"; > uploader.Comments="comment"; > uploader.CreatedByUser="test"; > uploader.FileName=e.Name; > > try > { > > oImg = new FileStream (e.FullPath,FileMode.Open,FileAccess.Read); > oBinaryReader = new BinaryReader(oImg); > byte[] oImgByteArray = oBinaryReader.ReadBytes((int) oImg.Length); > uploader.FileBody=oImgByteArray; > > uploader.RecordType=Business.IFP.RecordType.IFP_UP LOAD; > uploader.Upload(); > uploader=null; > > } > catch(Exception ee) > { > *******************I don't know how to handle the exception > here??*********************** > } > finally > { > if (oBinaryReader!=null) oBinaryReader.Close(); > if (oImg!=null) oImg.Close(); > > } > } > > "Eliyahu Goldin" <re*************@monarchmed.com> wrote in message > news:ud**************@TK2MSFTNGP09.phx.gbl... >> Are you sure you are on the right track? A FileSystemWatcher object >> has to >> exist somewhere to be able to watch. An asp.net application doesn't >> exist >> anywhere but between a client http request and the server response. A >> very >> short time. Do you expect the watcher to catch something only when >> the >> server is busy serving client requests? >> >> Eliyahu >> >> >> "J-T" <Ra****@microsft.com> wrote in message >> news:%2****************@TK2MSFTNGP15.phx.gbl... >>> We are working on an asp.net application which is a 3-tier >>> application.I >> was >>> aksed to create a component which monitors a folder and gets the >>> file and >>> pass them to a class library in our business logic layer(so far so >>> good >> and >>> easy).I initialize my class which is using a FileSystemWatcher in my >>> Global.asax and everything works fine.I have found >>> FileSystemWatcher >> class >>> not very reliable and sometimes it behavies unexpectedly.I'm afriad >>> that >> it >>> brings down the whole application.Is there a better way of doing >>> this >>> **Inside Asp.Net application**.I can't use antother application like >> windows >>> service or schedault taks.Everything needs to be done is ASP.NET >>> application. >>> >>> >>> Thanks a lot >>> >>> >> >> > >
Or how about this:
OnCreate event of the file ,I query the directory for all the files of the
type of I want and loop through them and enforce what I want.In the way even
if the application is in idle mode and filesystemWatcher is down as well,the
next file which triggers the fileSystemWatcher causes all the other files to
be processed as well.
Thanks
"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:u4**************@tk2msftngp13.phx.gbl... I forgot to say that if I keep my object in an application variable,then the persistence problem will be solved,right?
Yes. However, be aware that an ASP.Net application will stop running when it is idle (no requests) for a period of more than (by default) 20 minutes. When the application is stopped, the FileSystemWatcher will be stopped. When the application restarts, the FileSystemWatcher will be restarted as well. HOWEVER, any changes to the file system that the Watcher is supposed to watch during the idle interval will go unnoticed. Now, as long as changes to the files in the folder are also done by the ASP.Net app, there will BE no changes while the app is stopped. So, while I can't answer your question directly, this information, plus what you already know, should give you the solution you need.
-- HTH,
Kevin Spencer Microsoft MVP .Net Developer Sometimes you eat the elephant. Sometimes the elephant eats you.
"J-T" <Ra****@microsft.com> wrote in message news:eJ***************@TK2MSFTNGP12.phx.gbl...I forgot to say that if I keep my object in an application variable,then the persistence problem will be solved,right?
Thanks "Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message news:u8****************@tk2msftngp13.phx.gbl... Your FileSystemWatcher is scoped to the method in which you instantiate it. That means that it goes out of scope and is not available as soon as the method returns. It may hang around for a bit since you have no code to dispose it at any point, but you need to bone up on scope and state. Here's a primer:
Scope is the "area of influence" of a variable. In a class, you have global and local variables. Global variables are declared outside of any methods, and are therefore "visible" to all members of the class. Local variables are declared inside a method, and are therefore "visible" only to other variables within a single instance (call) of the method. Once the method returns, it is removed from the stack, and everything in it becomes available for garbage collection.
Note that this differs from public, private, etc. These are also definitions of scope, but are more related to encapsulation. Public members of a class are "visible" from other classes. Private members are not. Etc.
State refers to the persistence of an object (class instance or primitive) in memory. Whenever an object is instantiated (created), it must reside in memory somewhere. The lifetime of an object is limited to the lifetime of the container in which it resides. A method is instantiated, just like a variable, whenever you call it. And just like a variable, when it returns, it is available for garbage collection. The only way to persist an instance is to put it into something else that persists. Hence, ASP.Net, which operates using HTTP, which is stateless, has mechanisms for maintaining state, such as Session, Application, and ViewState.
The global.asax class, as I mentioned in my earlier reply, is a persistent class, but it's methods are not. They are instantiated like any other methods, and pass out of scope as soon as they return.
I hope you will study what I've said, and continue to study how OOP works as you go. In the meantime, you need to understand how to persist your FileSystemWatcher, as well as making sure that when you are finished with it, you dispose it.
-- HTH,
Kevin Spencer Microsoft MVP .Net Developer Sometimes you eat the elephant. Sometimes the elephant eats you.
"J-T" <Ra****@microsft.com> wrote in message news:e5**************@TK2MSFTNGP14.phx.gbl... Here is what I have done .I have created a class and I instanciate that class in Application_Start method of my Global.asax ere is the code:
********Gloabl.asax: //Sets up a wacher on an specific shared folder to pickup zip files
IFPWatcherComponent ifpWacher=new IFPWatcherComponent(ConfigurationSettings.AppSetti ngs[ "MonitorPath"]); ********MyClass:
/// </summary> public class IFPWatcherComponent:BusinessObject { string PathToMonitor=null;
public IFPWatcherComponent(string PathToMonitor) {
try { this.PathToMonitor = PathToMonitor; //Check to see if Web.config has appropriate settings if (this.PathToMonitor.Trim().Length==0 ) return; // Make a reference to a directory. DirectoryInfo di = new DirectoryInfo(this.PathToMonitor); // Create the directory only if it does not already exist. if (di.Exists == false) di.Create(); // create an instance of FileSystemWatcher object and assign path to monitor FileSystemWatcher watcher = new FileSystemWatcher(); // set necessary filters watcher.Path = this.PathToMonitor; //Watch for changes in FileName watcher.NotifyFilter = NotifyFilters.FileName; // watch zip files watcher.Filter = "*.zip"; //Add appropriate event handler watcher.Created += new FileSystemEventHandler(OnCreated); watcher.EnableRaisingEvents = true; } catch { throw; } } private void OnCreated(object source, FileSystemEventArgs e) { FileStream oImg=null; BinaryReader oBinaryReader=null;
Uploader uploader=new Uploader(); uploader.ComeFrom="88"; uploader.Comments="comment"; uploader.CreatedByUser="test"; uploader.FileName=e.Name;
try {
oImg = new FileStream (e.FullPath,FileMode.Open,FileAccess.Read); oBinaryReader = new BinaryReader(oImg); byte[] oImgByteArray = oBinaryReader.ReadBytes((int) oImg.Length); uploader.FileBody=oImgByteArray;
uploader.RecordType=Business.IFP.RecordType.IFP_UP LOAD; uploader.Upload(); uploader=null;
} catch(Exception ee) { *******************I don't know how to handle the exception here??*********************** } finally { if (oBinaryReader!=null) oBinaryReader.Close(); if (oImg!=null) oImg.Close();
} }
"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message news:ud**************@TK2MSFTNGP09.phx.gbl... > Are you sure you are on the right track? A FileSystemWatcher object > has to > exist somewhere to be able to watch. An asp.net application doesn't > exist > anywhere but between a client http request and the server response. A > very > short time. Do you expect the watcher to catch something only when the > server is busy serving client requests? > > Eliyahu > > > "J-T" <Ra****@microsft.com> wrote in message > news:%2****************@TK2MSFTNGP15.phx.gbl... >> We are working on an asp.net application which is a 3-tier >> application.I > was >> aksed to create a component which monitors a folder and gets the file >> and >> pass them to a class library in our business logic layer(so far so >> good > and >> easy).I initialize my class which is using a FileSystemWatcher in my >> Global.asax and everything works fine.I have found FileSystemWatcher > class >> not very reliable and sometimes it behavies unexpectedly.I'm afriad >> that > it >> brings down the whole application.Is there a better way of doing this >> **Inside Asp.Net application**.I can't use antother application like > windows >> service or schedault taks.Everything needs to be done is ASP.NET >> application. >> >> >> Thanks a lot >> >> > >
> Or how about this: OnCreate event of the file ,I query the directory for all the files of the type of I want and loop through them and enforce what I want.In the way even if the application is in idle mode and filesystemWatcher is down as well,the next file which triggers the fileSystemWatcher causes all the other files to be processed as well.
If you're going to do that, you don't need anything right away, right? In
that case, you don't need a FileSystemWatcher at all, just a timer and
System.IO to check and keep track of the contents of the folder. Problem
solved, at least if you don't need the data immediately.
--
HTH,
Kevin Spencer
Microsoft MVP
..Net Developer
Sometimes you eat the elephant.
Sometimes the elephant eats you.
"Ray5531" <Ra****@microsft.com> wrote in message
news:OV**************@TK2MSFTNGP14.phx.gbl... Or how about this:
OnCreate event of the file ,I query the directory for all the files of the type of I want and loop through them and enforce what I want.In the way even if the application is in idle mode and filesystemWatcher is down as well,the next file which triggers the fileSystemWatcher causes all the other files to be processed as well.
Thanks "Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message news:u4**************@tk2msftngp13.phx.gbl... >I forgot to say that if I keep my object in an application variable,then >the persistence problem will be solved,right?
Yes. However, be aware that an ASP.Net application will stop running when it is idle (no requests) for a period of more than (by default) 20 minutes. When the application is stopped, the FileSystemWatcher will be stopped. When the application restarts, the FileSystemWatcher will be restarted as well. HOWEVER, any changes to the file system that the Watcher is supposed to watch during the idle interval will go unnoticed. Now, as long as changes to the files in the folder are also done by the ASP.Net app, there will BE no changes while the app is stopped. So, while I can't answer your question directly, this information, plus what you already know, should give you the solution you need.
-- HTH,
Kevin Spencer Microsoft MVP .Net Developer Sometimes you eat the elephant. Sometimes the elephant eats you.
"J-T" <Ra****@microsft.com> wrote in message news:eJ***************@TK2MSFTNGP12.phx.gbl...I forgot to say that if I keep my object in an application variable,then the persistence problem will be solved,right?
Thanks "Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message news:u8****************@tk2msftngp13.phx.gbl... Your FileSystemWatcher is scoped to the method in which you instantiate it. That means that it goes out of scope and is not available as soon as the method returns. It may hang around for a bit since you have no code to dispose it at any point, but you need to bone up on scope and state. Here's a primer:
Scope is the "area of influence" of a variable. In a class, you have global and local variables. Global variables are declared outside of any methods, and are therefore "visible" to all members of the class. Local variables are declared inside a method, and are therefore "visible" only to other variables within a single instance (call) of the method. Once the method returns, it is removed from the stack, and everything in it becomes available for garbage collection.
Note that this differs from public, private, etc. These are also definitions of scope, but are more related to encapsulation. Public members of a class are "visible" from other classes. Private members are not. Etc.
State refers to the persistence of an object (class instance or primitive) in memory. Whenever an object is instantiated (created), it must reside in memory somewhere. The lifetime of an object is limited to the lifetime of the container in which it resides. A method is instantiated, just like a variable, whenever you call it. And just like a variable, when it returns, it is available for garbage collection. The only way to persist an instance is to put it into something else that persists. Hence, ASP.Net, which operates using HTTP, which is stateless, has mechanisms for maintaining state, such as Session, Application, and ViewState.
The global.asax class, as I mentioned in my earlier reply, is a persistent class, but it's methods are not. They are instantiated like any other methods, and pass out of scope as soon as they return.
I hope you will study what I've said, and continue to study how OOP works as you go. In the meantime, you need to understand how to persist your FileSystemWatcher, as well as making sure that when you are finished with it, you dispose it.
-- HTH,
Kevin Spencer Microsoft MVP .Net Developer Sometimes you eat the elephant. Sometimes the elephant eats you.
"J-T" <Ra****@microsft.com> wrote in message news:e5**************@TK2MSFTNGP14.phx.gbl... > Here is what I have done .I have created a class and I instanciate > that class in Application_Start method of my Global.asax ere is the > code: > > ********Gloabl.asax: > //Sets up a wacher on an specific shared folder to pickup zip files > > IFPWatcherComponent ifpWacher=new > IFPWatcherComponent(ConfigurationSettings.AppSetti ngs[ > "MonitorPath"]); > > > > ********MyClass: > > /// </summary> > public class IFPWatcherComponent:BusinessObject > { > string PathToMonitor=null; > > public IFPWatcherComponent(string PathToMonitor) > { > > try > { > this.PathToMonitor = PathToMonitor; > //Check to see if Web.config has appropriate settings > if (this.PathToMonitor.Trim().Length==0 ) return; > // Make a reference to a directory. > DirectoryInfo di = new DirectoryInfo(this.PathToMonitor); > // Create the directory only if it does not already exist. > if (di.Exists == false) > di.Create(); > // create an instance of FileSystemWatcher object and assign path > to monitor > FileSystemWatcher watcher = new FileSystemWatcher(); > // set necessary filters > watcher.Path = this.PathToMonitor; > //Watch for changes in FileName > watcher.NotifyFilter = NotifyFilters.FileName; > // watch zip files > watcher.Filter = "*.zip"; > //Add appropriate event handler > watcher.Created += new FileSystemEventHandler(OnCreated); > watcher.EnableRaisingEvents = true; > } > catch > { > throw; > } > } > private void OnCreated(object source, FileSystemEventArgs e) > { > FileStream oImg=null; > BinaryReader oBinaryReader=null; > > Uploader uploader=new Uploader(); > uploader.ComeFrom="88"; > uploader.Comments="comment"; > uploader.CreatedByUser="test"; > uploader.FileName=e.Name; > > try > { > > oImg = new FileStream (e.FullPath,FileMode.Open,FileAccess.Read); > oBinaryReader = new BinaryReader(oImg); > byte[] oImgByteArray = oBinaryReader.ReadBytes((int) oImg.Length); > uploader.FileBody=oImgByteArray; > > uploader.RecordType=Business.IFP.RecordType.IFP_UP LOAD; > uploader.Upload(); > uploader=null; > > } > catch(Exception ee) > { > *******************I don't know how to handle the exception > here??*********************** > } > finally > { > if (oBinaryReader!=null) oBinaryReader.Close(); > if (oImg!=null) oImg.Close(); > > } > } > > "Eliyahu Goldin" <re*************@monarchmed.com> wrote in message > news:ud**************@TK2MSFTNGP09.phx.gbl... >> Are you sure you are on the right track? A FileSystemWatcher object >> has to >> exist somewhere to be able to watch. An asp.net application doesn't >> exist >> anywhere but between a client http request and the server response. A >> very >> short time. Do you expect the watcher to catch something only when >> the >> server is busy serving client requests? >> >> Eliyahu >> >> >> "J-T" <Ra****@microsft.com> wrote in message >> news:%2****************@TK2MSFTNGP15.phx.gbl... >>> We are working on an asp.net application which is a 3-tier >>> application.I >> was >>> aksed to create a component which monitors a folder and gets the >>> file and >>> pass them to a class library in our business logic layer(so far so >>> good >> and >>> easy).I initialize my class which is using a FileSystemWatcher in my >>> Global.asax and everything works fine.I have found >>> FileSystemWatcher >> class >>> not very reliable and sometimes it behavies unexpectedly.I'm afriad >>> that >> it >>> brings down the whole application.Is there a better way of doing >>> this >>> **Inside Asp.Net application**.I can't use antother application like >> windows >>> service or schedault taks.Everything needs to be done is ASP.NET >>> application. >>> >>> >>> Thanks a lot >>> >>> >> >> > >
I don't need the data immidiately at all.I need to get them sometimes
anyway. just a timer and System.IO to check and keep track of the contents of the folder
you mean I should implement a timer in my class which makes my object to be
executed at a time interval for changes in an specific folder?
Thanks
"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl... Or how about this:
OnCreate event of the file ,I query the directory for all the files of the type of I want and loop through them and enforce what I want.In the way even if the application is in idle mode and filesystemWatcher is down as well,the next file which triggers the fileSystemWatcher causes all the other files to be processed as well.
If you're going to do that, you don't need anything right away, right? In that case, you don't need a FileSystemWatcher at all, just a timer and System.IO to check and keep track of the contents of the folder. Problem solved, at least if you don't need the data immediately.
-- HTH,
Kevin Spencer Microsoft MVP .Net Developer Sometimes you eat the elephant. Sometimes the elephant eats you.
"Ray5531" <Ra****@microsft.com> wrote in message news:OV**************@TK2MSFTNGP14.phx.gbl... Or how about this:
OnCreate event of the file ,I query the directory for all the files of the type of I want and loop through them and enforce what I want.In the way even if the application is in idle mode and filesystemWatcher is down as well,the next file which triggers the fileSystemWatcher causes all the other files to be processed as well.
Thanks "Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message news:u4**************@tk2msftngp13.phx.gbl... >I forgot to say that if I keep my object in an application >variable,then the persistence problem will be solved,right?
Yes. However, be aware that an ASP.Net application will stop running when it is idle (no requests) for a period of more than (by default) 20 minutes. When the application is stopped, the FileSystemWatcher will be stopped. When the application restarts, the FileSystemWatcher will be restarted as well. HOWEVER, any changes to the file system that the Watcher is supposed to watch during the idle interval will go unnoticed. Now, as long as changes to the files in the folder are also done by the ASP.Net app, there will BE no changes while the app is stopped. So, while I can't answer your question directly, this information, plus what you already know, should give you the solution you need.
-- HTH,
Kevin Spencer Microsoft MVP .Net Developer Sometimes you eat the elephant. Sometimes the elephant eats you.
"J-T" <Ra****@microsft.com> wrote in message news:eJ***************@TK2MSFTNGP12.phx.gbl... I forgot to say that if I keep my object in an application variable,then the persistence problem will be solved,right?
Thanks "Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message news:u8****************@tk2msftngp13.phx.gbl... > Your FileSystemWatcher is scoped to the method in which you > instantiate it. That means that it goes out of scope and is not > available as soon as the method returns. It may hang around for a bit > since you have no code to dispose it at any point, but you need to > bone up on scope and state. Here's a primer: > > Scope is the "area of influence" of a variable. In a class, you have > global and local variables. Global variables are declared outside of > any methods, and are therefore "visible" to all members of the class. > Local variables are declared inside a method, and are therefore > "visible" only to other variables within a single instance (call) of > the method. Once the method returns, it is removed from the stack, and > everything in it becomes available for garbage collection. > > Note that this differs from public, private, etc. These are also > definitions of scope, but are more related to encapsulation. Public > members of a class are "visible" from other classes. Private members > are not. Etc. > > State refers to the persistence of an object (class instance or > primitive) in memory. Whenever an object is instantiated (created), it > must reside in memory somewhere. The lifetime of an object is limited > to the lifetime of the container in which it resides. A method is > instantiated, just like a variable, whenever you call it. And just > like a variable, when it returns, it is available for garbage > collection. The only way to persist an instance is to put it into > something else that persists. Hence, ASP.Net, which operates using > HTTP, which is stateless, has mechanisms for maintaining state, such > as Session, Application, and ViewState. > > The global.asax class, as I mentioned in my earlier reply, is a > persistent class, but it's methods are not. They are instantiated like > any other methods, and pass out of scope as soon as they return. > > I hope you will study what I've said, and continue to study how OOP > works as you go. In the meantime, you need to understand how to > persist your FileSystemWatcher, as well as making sure that when you > are finished with it, you dispose it. > > -- > HTH, > > Kevin Spencer > Microsoft MVP > .Net Developer > Sometimes you eat the elephant. > Sometimes the elephant eats you. > > "J-T" <Ra****@microsft.com> wrote in message > news:e5**************@TK2MSFTNGP14.phx.gbl... >> Here is what I have done .I have created a class and I instanciate >> that class in Application_Start method of my Global.asax ere is the >> code: >> >> ********Gloabl.asax: >> //Sets up a wacher on an specific shared folder to pickup zip files >> >> IFPWatcherComponent ifpWacher=new >> IFPWatcherComponent(ConfigurationSettings.AppSetti ngs[ >> "MonitorPath"]); >> >> >> >> ********MyClass: >> >> /// </summary> >> public class IFPWatcherComponent:BusinessObject >> { >> string PathToMonitor=null; >> >> public IFPWatcherComponent(string PathToMonitor) >> { >> >> try >> { >> this.PathToMonitor = PathToMonitor; >> //Check to see if Web.config has appropriate settings >> if (this.PathToMonitor.Trim().Length==0 ) return; >> // Make a reference to a directory. >> DirectoryInfo di = new DirectoryInfo(this.PathToMonitor); >> // Create the directory only if it does not already exist. >> if (di.Exists == false) >> di.Create(); >> // create an instance of FileSystemWatcher object and assign path >> to monitor >> FileSystemWatcher watcher = new FileSystemWatcher(); >> // set necessary filters >> watcher.Path = this.PathToMonitor; >> //Watch for changes in FileName >> watcher.NotifyFilter = NotifyFilters.FileName; >> // watch zip files >> watcher.Filter = "*.zip"; >> //Add appropriate event handler >> watcher.Created += new FileSystemEventHandler(OnCreated); >> watcher.EnableRaisingEvents = true; >> } >> catch >> { >> throw; >> } >> } >> private void OnCreated(object source, FileSystemEventArgs e) >> { >> FileStream oImg=null; >> BinaryReader oBinaryReader=null; >> >> Uploader uploader=new Uploader(); >> uploader.ComeFrom="88"; >> uploader.Comments="comment"; >> uploader.CreatedByUser="test"; >> uploader.FileName=e.Name; >> >> try >> { >> >> oImg = new FileStream (e.FullPath,FileMode.Open,FileAccess.Read); >> oBinaryReader = new BinaryReader(oImg); >> byte[] oImgByteArray = oBinaryReader.ReadBytes((int) oImg.Length); >> uploader.FileBody=oImgByteArray; >> >> uploader.RecordType=Business.IFP.RecordType.IFP_UP LOAD; >> uploader.Upload(); >> uploader=null; >> >> } >> catch(Exception ee) >> { >> *******************I don't know how to handle the exception >> here??*********************** >> } >> finally >> { >> if (oBinaryReader!=null) oBinaryReader.Close(); >> if (oImg!=null) oImg.Close(); >> >> } >> } >> >> "Eliyahu Goldin" <re*************@monarchmed.com> wrote in message >> news:ud**************@TK2MSFTNGP09.phx.gbl... >>> Are you sure you are on the right track? A FileSystemWatcher object >>> has to >>> exist somewhere to be able to watch. An asp.net application doesn't >>> exist >>> anywhere but between a client http request and the server response. >>> A very >>> short time. Do you expect the watcher to catch something only when >>> the >>> server is busy serving client requests? >>> >>> Eliyahu >>> >>> >>> "J-T" <Ra****@microsft.com> wrote in message >>> news:%2****************@TK2MSFTNGP15.phx.gbl... >>>> We are working on an asp.net application which is a 3-tier >>>> application.I >>> was >>>> aksed to create a component which monitors a folder and gets the >>>> file and >>>> pass them to a class library in our business logic layer(so far so >>>> good >>> and >>>> easy).I initialize my class which is using a FileSystemWatcher in >>>> my >>>> Global.asax and everything works fine.I have found >>>> FileSystemWatcher >>> class >>>> not very reliable and sometimes it behavies unexpectedly.I'm afriad >>>> that >>> it >>>> brings down the whole application.Is there a better way of doing >>>> this >>>> **Inside Asp.Net application**.I can't use antother application >>>> like >>> windows >>>> service or schedault taks.Everything needs to be done is ASP.NET >>>> application. >>>> >>>> >>>> Thanks a lot >>>> >>>> >>> >>> >> >> > >
I mean that you should create a timer in your Application_OnStart event
handler, store it in the Application Cache, and let its Elapsed event
handler do the work. If your Application stops, the timer stops. But when
the Application starts, the timer restarts.
--
HTH,
Kevin Spencer
Microsoft MVP
..Net Developer
Sometimes you eat the elephant.
Sometimes the elephant eats you.
"J-T" <J-*@microsft.com> wrote in message
news:un*************@TK2MSFTNGP12.phx.gbl... I don't need the data immidiately at all.I need to get them sometimes anyway. just a timer and System.IO to check and keep track of the contents of the folder you mean I should implement a timer in my class which makes my object to be executed at a time interval for changes in an specific folder?
Thanks "Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message news:%2****************@TK2MSFTNGP10.phx.gbl... Or how about this:
OnCreate event of the file ,I query the directory for all the files of the type of I want and loop through them and enforce what I want.In the way even if the application is in idle mode and filesystemWatcher is down as well,the next file which triggers the fileSystemWatcher causes all the other files to be processed as well.
If you're going to do that, you don't need anything right away, right? In that case, you don't need a FileSystemWatcher at all, just a timer and System.IO to check and keep track of the contents of the folder. Problem solved, at least if you don't need the data immediately.
-- HTH,
Kevin Spencer Microsoft MVP .Net Developer Sometimes you eat the elephant. Sometimes the elephant eats you.
"Ray5531" <Ra****@microsft.com> wrote in message news:OV**************@TK2MSFTNGP14.phx.gbl... Or how about this:
OnCreate event of the file ,I query the directory for all the files of the type of I want and loop through them and enforce what I want.In the way even if the application is in idle mode and filesystemWatcher is down as well,the next file which triggers the fileSystemWatcher causes all the other files to be processed as well.
Thanks "Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message news:u4**************@tk2msftngp13.phx.gbl... >I forgot to say that if I keep my object in an application >variable,then the persistence problem will be solved,right?
Yes. However, be aware that an ASP.Net application will stop running when it is idle (no requests) for a period of more than (by default) 20 minutes. When the application is stopped, the FileSystemWatcher will be stopped. When the application restarts, the FileSystemWatcher will be restarted as well. HOWEVER, any changes to the file system that the Watcher is supposed to watch during the idle interval will go unnoticed. Now, as long as changes to the files in the folder are also done by the ASP.Net app, there will BE no changes while the app is stopped. So, while I can't answer your question directly, this information, plus what you already know, should give you the solution you need.
-- HTH,
Kevin Spencer Microsoft MVP .Net Developer Sometimes you eat the elephant. Sometimes the elephant eats you.
"J-T" <Ra****@microsft.com> wrote in message news:eJ***************@TK2MSFTNGP12.phx.gbl... >I forgot to say that if I keep my object in an application >variable,then the persistence problem will be solved,right? > > Thanks > "Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message > news:u8****************@tk2msftngp13.phx.gbl... >> Your FileSystemWatcher is scoped to the method in which you >> instantiate it. That means that it goes out of scope and is not >> available as soon as the method returns. It may hang around for a bit >> since you have no code to dispose it at any point, but you need to >> bone up on scope and state. Here's a primer: >> >> Scope is the "area of influence" of a variable. In a class, you have >> global and local variables. Global variables are declared outside of >> any methods, and are therefore "visible" to all members of the class. >> Local variables are declared inside a method, and are therefore >> "visible" only to other variables within a single instance (call) of >> the method. Once the method returns, it is removed from the stack, >> and everything in it becomes available for garbage collection. >> >> Note that this differs from public, private, etc. These are also >> definitions of scope, but are more related to encapsulation. Public >> members of a class are "visible" from other classes. Private members >> are not. Etc. >> >> State refers to the persistence of an object (class instance or >> primitive) in memory. Whenever an object is instantiated (created), >> it must reside in memory somewhere. The lifetime of an object is >> limited to the lifetime of the container in which it resides. A >> method is instantiated, just like a variable, whenever you call it. >> And just like a variable, when it returns, it is available for >> garbage collection. The only way to persist an instance is to put it >> into something else that persists. Hence, ASP.Net, which operates >> using HTTP, which is stateless, has mechanisms for maintaining state, >> such as Session, Application, and ViewState. >> >> The global.asax class, as I mentioned in my earlier reply, is a >> persistent class, but it's methods are not. They are instantiated >> like any other methods, and pass out of scope as soon as they return. >> >> I hope you will study what I've said, and continue to study how OOP >> works as you go. In the meantime, you need to understand how to >> persist your FileSystemWatcher, as well as making sure that when you >> are finished with it, you dispose it. >> >> -- >> HTH, >> >> Kevin Spencer >> Microsoft MVP >> .Net Developer >> Sometimes you eat the elephant. >> Sometimes the elephant eats you. >> >> "J-T" <Ra****@microsft.com> wrote in message >> news:e5**************@TK2MSFTNGP14.phx.gbl... >>> Here is what I have done .I have created a class and I instanciate >>> that class in Application_Start method of my Global.asax ere is the >>> code: >>> >>> ********Gloabl.asax: >>> //Sets up a wacher on an specific shared folder to pickup zip files >>> >>> IFPWatcherComponent ifpWacher=new >>> IFPWatcherComponent(ConfigurationSettings.AppSetti ngs[ >>> "MonitorPath"]); >>> >>> >>> >>> ********MyClass: >>> >>> /// </summary> >>> public class IFPWatcherComponent:BusinessObject >>> { >>> string PathToMonitor=null; >>> >>> public IFPWatcherComponent(string PathToMonitor) >>> { >>> >>> try >>> { >>> this.PathToMonitor = PathToMonitor; >>> //Check to see if Web.config has appropriate settings >>> if (this.PathToMonitor.Trim().Length==0 ) return; >>> // Make a reference to a directory. >>> DirectoryInfo di = new DirectoryInfo(this.PathToMonitor); >>> // Create the directory only if it does not already exist. >>> if (di.Exists == false) >>> di.Create(); >>> // create an instance of FileSystemWatcher object and assign path >>> to monitor >>> FileSystemWatcher watcher = new FileSystemWatcher(); >>> // set necessary filters >>> watcher.Path = this.PathToMonitor; >>> //Watch for changes in FileName >>> watcher.NotifyFilter = NotifyFilters.FileName; >>> // watch zip files >>> watcher.Filter = "*.zip"; >>> //Add appropriate event handler >>> watcher.Created += new FileSystemEventHandler(OnCreated); >>> watcher.EnableRaisingEvents = true; >>> } >>> catch >>> { >>> throw; >>> } >>> } >>> private void OnCreated(object source, FileSystemEventArgs e) >>> { >>> FileStream oImg=null; >>> BinaryReader oBinaryReader=null; >>> >>> Uploader uploader=new Uploader(); >>> uploader.ComeFrom="88"; >>> uploader.Comments="comment"; >>> uploader.CreatedByUser="test"; >>> uploader.FileName=e.Name; >>> >>> try >>> { >>> >>> oImg = new FileStream (e.FullPath,FileMode.Open,FileAccess.Read); >>> oBinaryReader = new BinaryReader(oImg); >>> byte[] oImgByteArray = oBinaryReader.ReadBytes((int) >>> oImg.Length); >>> uploader.FileBody=oImgByteArray; >>> >>> uploader.RecordType=Business.IFP.RecordType.IFP_UP LOAD; >>> uploader.Upload(); >>> uploader=null; >>> >>> } >>> catch(Exception ee) >>> { >>> *******************I don't know how to handle the exception >>> here??*********************** >>> } >>> finally >>> { >>> if (oBinaryReader!=null) oBinaryReader.Close(); >>> if (oImg!=null) oImg.Close(); >>> >>> } >>> } >>> >>> "Eliyahu Goldin" <re*************@monarchmed.com> wrote in message >>> news:ud**************@TK2MSFTNGP09.phx.gbl... >>>> Are you sure you are on the right track? A FileSystemWatcher object >>>> has to >>>> exist somewhere to be able to watch. An asp.net application doesn't >>>> exist >>>> anywhere but between a client http request and the server response. >>>> A very >>>> short time. Do you expect the watcher to catch something only when >>>> the >>>> server is busy serving client requests? >>>> >>>> Eliyahu >>>> >>>> >>>> "J-T" <Ra****@microsft.com> wrote in message >>>> news:%2****************@TK2MSFTNGP15.phx.gbl... >>>>> We are working on an asp.net application which is a 3-tier >>>>> application.I >>>> was >>>>> aksed to create a component which monitors a folder and gets the >>>>> file and >>>>> pass them to a class library in our business logic layer(so far so >>>>> good >>>> and >>>>> easy).I initialize my class which is using a FileSystemWatcher in >>>>> my >>>>> Global.asax and everything works fine.I have found >>>>> FileSystemWatcher >>>> class >>>>> not very reliable and sometimes it behavies unexpectedly.I'm >>>>> afriad that >>>> it >>>>> brings down the whole application.Is there a better way of doing >>>>> this >>>>> **Inside Asp.Net application**.I can't use antother application >>>>> like >>>> windows >>>>> service or schedault taks.Everything needs to be done is ASP.NET >>>>> application. >>>>> >>>>> >>>>> Thanks a lot >>>>> >>>>> >>>> >>>> >>> >>> >> >> > >
Thanks Kevin for all the helps and follow ups.I appreciate it.
I got it finally up and working.Here is the code
**********My Global.asax :
IFPWatcherComponent ifpWacher=new IFPWatcherComponent(
ConfigurationSettings.AppSettings["MonitorPath"],
int.Parse(ConfigurationSettings.AppSettings["SimultaniousNumberOfFiles"]));
Authorization.Current.Initialize();
try
{
Application.Lock();
Application.Add("IFPWatcherComponent",ifpWacher);
}
finally
{
Application.UnLock();
**********Class:
public class IFPWatcherComponent: Timer
{
private string mDir="";
private string mirrorDir="";
private int iCount=1;
// Monitor directory
public string monitorDirectory
{
get { return this.mDir; }
set { this.mDir= value; }
}
// interval
public double timerInterval
{
get { return this.Interval; }
set { this.Interval= value; }
}
public IFPWatcherComponent(string MonitorPath,int FileCounts)
{
this.monitorDirectory=MonitorPath;
this.fileCounts=FileCounts;
try
{
//Check to see if Web.config has appropriate settings
if (this.monitorDirectory.Trim().Length==0 ) return;
// Make a reference to a Montitor directory.
DirectoryInfo diMonitor = new DirectoryInfo(this.monitorDirectory);
// Create the directory only if it does not already exist.
if (diMonitor.Exists == false)
diMonitor.Create();
this.Elapsed+=new ElapsedEventHandler(OnTimedEvent);
this.Interval=this.timerInterval;
this.Enabled=true;
}
catch
{
throw;
}
}
private void OnTimedEvent(object source, ElapsedEventArgs e)
{
//do whatever you want here
}
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Stampede |
last post by:
Hi,
I want to use the FileSystemWatcher in a Windows Service. I read an article,
where the author created the FileSystemWatcher object in a seperate thread
and when the event is fired, he started...
|
by: Sacha Korell |
last post by:
I need to set up a FileSystemWatcher in my web application (to automatically
process uploaded files) and I'm trying to decide where to set it up. I would
like to keep it within the web app, but it...
|
by: Greg Collins [MVP] |
last post by:
Hi, I couldn't find what I was looking for by searching the newsgroup, but perhaps these have already been discussed somewhere. This is a bit long with a lot of interrelated questions.
What I've...
|
by: Mika M |
last post by:
Hi!
I've made FileSystemWatcher application to copy files from one UNC-path
(\\server\directory) into another when files appears into source path. I
made it using link...
|
by: rgparkins |
last post by:
HI
I have an ASp.NET web application that I am developing which uses a
menu control that will not change "very frequently" BUT can do!. The
control will load its menu items from an xml file that...
| |
by: ljh |
last post by:
Has anyone else noticed that the FileSystemWatcher raises the changed event
twice when a file is changed?
Do you have any idea why this is the case?
|
by: =?Utf-8?B?Sm9obiBT?= |
last post by:
I am trying to find out if there is a way to tell if there is already a
filesystemwatcher (created by a webservice) monitoring a folder.
I have a webservice that creates a filesystemwatcher,...
|
by: IUnknown |
last post by:
Ok, we are all aware of the situation where modifying the folder
structure (adding files, folders, deleting files, etc) will result in
ASP.NET triggering a recompilation/restart of the application....
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |