Reading Xml from an absolute path - wcf

I need to access a remote Xml document from a WCF service. Right now I have:
XmlReader reader = XmlReader.Create("path");
But since the Xml doc is elsewhere on our network I need to give the XmlReader an absolute path, as opposed to having it look deeper in the project folder. How do I do this? I've found surprisingly little information about this. It seems like this should be a simple thing to do. Any help is appreciated!
Thanks

You can use overload that accepts Stream parameters as follows:
using (FileStream fileStream = new FileStream(#"\\computername\shared path"))
using (XmlReader reader = XmlReader.Create(fileStream))
{
// perform your custom code with XmlReader
}
Please note that you need appropriate permission to open remote stream. In WCF service context you may need to use impersonation.

Related

C++/CLI using XmlWriter with TcpClient not writing

I hope you'll forgive me if this is obvious; just starting out with c++/cli. I'm using some sample C# code to generate this but am using cli mode in a legacy c++ application so I'm having trouble in translation. Not easy finding samples on the web.
This is a snippet of code. An XDocument is passed into this code. I've checked it by writing to a file and the resulting xml file looks as it should (identical to the sample C# app). The tcpclient call seems to work, if I change the address, it throws an exception. I also see the connection established in a TCP Sniffer.
The request->WriteTo doesn't work - the sniffer shows no data written and the device doesn't respond.
Further, the XmlReader::Create call locks everything up.
Clearly, I've got something wrong in how I'm setting up the writer or the stream.
Any ideas?
Thanks.
TcpClient ^ socket = gcnew TcpClient(address,port); // connect to device
NetworkStream ^ stream;
stream = socket->GetStream(); // get stream.
XmlWriter ^writer = XmlWriter::Create(stream,xmlWrite); // Create writer
request->WriteTo(writer); // Write XDocument to stream.
XmlReader ^reader = XmlReader::Create(stream,xmlRead);
I don't know if this is good form or not but I found the answer so I'll post here in case anyone else has a similar problem:
After
request->WriteTo(writer)
I added
writer->Flush().
Odd that the C# example I was working from didn't require this so if anyone has any insight into why the difference, I'd love to hear it.

Sending a file from a java client to a server using wcf method?

I want to build a wcf web service so that the client and the server would be able to transfer files between each other. Do you know how I can achieve this? I think I should turn it into a byte array but I have no idea how to do that. The file is also quite big so I must turn on streamed response.
It sounds like you're on the right track. A quick search of the interwebz yielded this link: http://www.codeproject.com/Articles/166763/WCF-Streaming-Upload-Download-Files-Over-HTTP
Your question indicates that you want to send a file from a java client to a WCFd endpoint, but the contents of your question indicate that this should be a bidirectional capability. If this is the case, then you'll need to implement a service endpoint on your client as well. As far as that is concerned, I cannot be of much help, but there are resources out there like this SO question: In-process SOAP service server for Java
As far as practical implementation, I would think that using these two links you should be able to produce some code for your server and client.
As far as reading all bytes of a file, in C# you can use: File.ReadAllBytes It should work as in the following code:
//Read The contents of the file indicated
string fileName = "/path/to/some/file";
//store the binary in a byte array
byte[] buffer = File.ReadAllBytes(fileName);
//do something with those bytes!
Be sure to use the search function in the future:

Access .exe file in resources?

I have an encrypted program in my resources in a project I'm working on and I need to access that file like this.
Dim fs As New FileStream(filepath, FileMode.Open)
Any help?
try GetManifestResourceStream - it gives you a stream to read the resource by name. This stream can be used for example to decrypt and/or write to a real file and/or load as an Assembly (if the embedded resource is a .NET exe/DLL)...
For sample code see http://www.eggheadcafe.com/microsoft/VB-NET/33899321/how-to-extract-a-resource-to-a-file.aspx .

WCF - reading an xml file in a service using XElement.Load

i have a simple xml file in a wcf service that i am trying to load using Xelement.Load("sample.xml") which is not reading the file. What's the right way of doing this?
The service is supposed to return an xml to an asp.net application.
TIA
I got it to work by providing the ABSOLUTE path as the parameter to the XElement.Load() method, RELATIVE path would be better though.
You should try something like this then.
var appPath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
bodyFile = Path.Combine(appPath, #"<File Name Path");
This will work relative to your application's physical path.

getting a responseLength for a HttpWebRequest upload from another webfile to stream into the upload when the source doesn't implement ContentLength?

Background - I'm trying to stream an existing webpage to a separate web application, using HttpWebRequest/HttpWebResponse in C#. One issue I'm striking is that I'm trying to set the file upload request content-length using the file download's content-length, HOWEVER the issue seems to be when the source webpage is on a webserver for which the HttpWebResponse doesn't provide a content length.
HttpWebRequest downloadRequest = WebRequest.Create(new Uri("downloaduri")) as HttpWebRequest;
using (HttpWebResponse downloadResponse = downloadRequest.GetResponse() as HttpWebResponse)
{
var uploadRequest = (HttpWebRequest) WebRequest.Create(new Uri("uripath"));
uploadRequest.Method = "POST";
uploadRequest.ContentLength = downloadResponse.ContentLength; // ####
QUESTION: How could I update this approach to cater for this case (when the download response doesn't have a content-length set). Would it be to somehow use a MemoryStream perhaps? Any sample code would be appreciated.
If you're happy to download the response from the other web server completely, that would indeed make life easier. Just repeatedly write into a MemoryStream as you fetch from the first web server, then you know the length to set for the second request and you can write the data in easily (especially as MemoryStream has a WriteTo method to write its contents to another stream).
The downside of this is that you'll take a lot of memory if it's a large file. Is that likely to be a problem in your situation? Alternatives include:
Writing to a file instead of using a MemoryStream. You'll need to clean up the file afterwards, of course - you're basically using the file system as bigger memory :)
Using a chunked transfer encoding to "read a chunk, write a chunk"; this may be fiddly to get right - it's certainly not something I've done before.