Transfering files between executables

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

What is the best way to transfer files between two .NET executables?
I have a service, and a client program. Client asks service for data. The
service creates one, and passes it back to client. Both programs run in .NET
framework.
Given this requirement, how best to approach the issue? Does .NET framework
provides any class that can help?
Thanks in advance,
 
Do they have a common drive they can access? If so, you could just write
the file to disk and then let the service pick it up at scheduled intervals.
 
Ravi

The other option is to stream the file from app to app over using sockets.
The file share is quicker id the app is for local use only. The socket
streaming might be good if the application is located on a separate server
to which the service do not have access too.

Henk
 
That's such an old school COBOL-esque way of doing things.

If you're transferring files between client/server via remothing you'd just
convert the filestream to a bytearray (code below) and return the contents.
If you're worried about first "loading the file into memory" or whatever...
consider this: this is pretty much exactly what the operating system does
even if you go the "polling" route suggested by others.

Dim fs As New System.IO.FileStream(theFile, IO.FileMode.Open,
IO.FileAccess.Read)
Dim bytes(CInt(fs.Length) - 1) As Byte '.length property is Long so it needs
to be cast
fs.Read(bytes, 0, CInt(fs.Length))
fs.Close()
Return bytes

then just reverse the process on the client side.
 
AFAIK the only way to communicate between EXE's-- even on the same machine--
is via Remoting. It's not that hard.... just declare your classes as
ServicedComponent and register them on a channel. Check out this article for
some simple examples (that don't require IIS and stuff)
http://www.codeguru.com/Csharp/Csharp/cs_syntax/remoting/article.php/c5871.

In the COM world exe's could do this easily via SingleUse vs. MultiUse
classes. Even plain Win32 DLL's can do it (share variables) via #pragma
blocks. But in .NET this seems a little hard.
 
Thanks Henk, But sockets is ruled out for the time being. Both the client
and server needs to be on the same machine without touching sockets.
 
Thanks Peter and CMM.
Peter, I thought about using a shared drive, but then I don'treally need
..NEt framework for this. I am wondering if some sort of queue can be
established between client and the server where in the server keeps writing
in and the clients keep reading.
CMM, Thx for the code snippet, but I am a little confused. How does the
client establish a connection to the file stream that the server (or windows
service) is writing to?

Back in the COM days, you could invoke a COM component running inside a
service and still be able to communicate as if it were a simple local
object. Can something like that be done using .NET? One possibility I see is
somehow add a reference inside the client for the service and use the class.
Would that be possible? And also, how to impliment it if both client and the
service are running in seperate user account?

Again, thanks for your replies (past/advance)
Ravi
 
You'd need to study up on .NET Remoting... even if both exe's are on the same
machine. I think- if I understood your post correctly- that this is what
you're looking for. It's not difficult and it allows your exe to behave
similarly to a good ol' COM ActiveX EXE Server...

In the server EXE's Sub Main you'd do something like:
Dim TCPChannel As TcpChannel = New TcpChannel(49175) 'can be any port you want
ChannelServices.RegisterChannel(TCPChannel)
RemotingConfiguration.RegisterWellKnownServiceType(GetType(<YOURCLASS>),
"<SOME_NAME_TO_IDENTIFY_THIS_CONNECTION>", WellKnownObjectMode.SingleCall)

In the client you'd do something like this to call the exe:
Dim o As YOURCLASS = CType(Activator.GetObject(GetType(YOURCLASS),
"tcp://localhost:49175/<SOME_NAME_TO_IDENTIFY_THE_CONNECTION"), YOURCLASS)
'now use it like any ol' object
x = o.DoSomething()

Your class needs to inherit from MarshalByRef... and any types it returns
need to be serializable (most types are... but some aren't).

Also, there's the "gotcha" that the client needs to "know" what the class
is... so it needs to have a local reference to the server class. This defeats
the purpose of remoting because then you need a COPY of the server class on
the client even though it's actually executing somewhere else.... sooooo, the
easy solution is for YOURCLASS to imlement an Interface defined in a "shim"
dll that both client and server can share. The lightweight dll (containing
Interface definitions only) would then accomplish the same exact thing as a
Type Library (.tlb) in ol' COM or WSDL file in Web Services.

More info: check out:
http://msdn.microsoft.com/architect...l=/library/en-us/dnnetsec/html/SecNetHT15.asp
 
Back
Top