bits , sharpBits.net - asp.net-mvc-3

I using in my project BITS - Background Intelligent Transfer Service for send file with larg size. Using SharpBITS.NET in C# code.
I want to upload file from server to client. I now note the sides.
-------------client side---------------
static void Main(string[] args)
{
string local = #"I:\a.mp3";
string destination = "http://192.168.56.128/BitsTest/Home/FileUpload";
string remoteFile = #destination;
string localFile = local;
if (!string.IsNullOrEmpty(localFile) && System.IO.File.Exists(localFile))
{
var bitsManager = new BitsManager();
var job = bitsManager.CreateJob("uploading file", JobType.Upload);
job.NotificationFlags = NotificationFlags.JobErrorOccured | NotificationFlags.JobModified |
NotificationFlags.JobTransferred;
job.AddFile(remoteFile, localFile);
job.Resume();
job.OnJobError += new EventHandler<JobErrorNotificationEventArgs>(job_OnJobError);
}
}
This is a simple console application. the local -- path the file that I want to send, destination -- the path is receiver it is remote server.
When I run program the job.Error take mi follow --- "The server's response was not valid. The server was not following the defined protocol. Resume the job, and then Background Intelligent Transfer Service (BITS) will try again. -- BG_E_HTTP_ERROR_200 .-2145845048, 0x801900C8"
For Client (receiver) i have the follow code: It is Mvs 3 small project and I View only action
where to go by our destination path.
public ActionResult FileUpload()
{
try
{
HttpPostedFileBase file = Request.Files[0];
file.SaveAs(System.IO.Path.Combine(Server.MapPath("/BitsTest/"), file.FileName));
}
catch
{ }
/*System.IO.File.Move(Server.MapPath("/BitsTest/bin/aa.png"), Server.MapPath("/BitsTest/Content/aa.png"));*/
}
But FileUpload action thas not recevie file. I don't know how I can receive file in client Side.
As you can see, I used HttpPostedFileBase for recive file but that is not working.
My host server is Windows server 2008 r2 and I done needed steps for BITS. For more information you can visit the follow site http://technet.microsoft.com/en-us/library/cc431377.aspx ---- How to Configure Windows Server 2008 for Configuration Manager 2007 Site Systems.
So I don't know what doing that I can receive file in host server.You can tell me what you can do.

With a stateless methdology like the one web applications use, there is no connection to the server once the response is completed. You can poll the server from the client side, but the client is not listening for the server to send additional bits.
In "the past" you could set up ActiveX controls, Java applets, etc (Silverlight today?) to continue to listen, but this is not straight web style development.
HTML5 expands your options, if you are willing to use the websocketAPI. As with all parts of HTML5, you have some risk using these bits for implementation as not all browsers have adopted the "standard" yet (adoption expected to be complete in the next 10-12 years:->).

Related

How can I create a WCF Service Application in Visual Studio that does NOT use a Web Server

I have a simple task: A program (executable) is supposed to call a function of another program (also executable) with some parameters. Program A is supposed to be started, call the function and then terminate. Program B is legacy program that has a GUI and runs continuously. Both programs run on the same Windows PC and use the .NET Framework. I have no experience in web development and Program B is not supposed to run as a web service! Named pipes seem like a good option.
I researched what the best method would be and wanted to try WCF. The documentation claims that "A service endpoint can be part of a continuously available service hosted by IIS, or it can be a service hosted in an application". From that I understand that I can run Program B as a service without hosting a web server.
However everything I see in Visual Studio seems to presume I want to run a server. Wenn I want to create a new WCF project in Visual Studio the only options are a library or "A project for creating WCF service application that is hosted in IIS/WAS". Once I've created said project the debugger wants me to choose a browser for hosting the service.
In another StackOverflow topic a popular suggestion was using this website as a guide and simply removing the http references since the guide is for both named pipes and http. Another indication that it should be possible.
So can someone point me in the right direction? What am I missing? How can I use WCF with nothing related to Web Development involved?
You have already been on the way, it is enough to host the web service in Program B, without specifying a web server. this is called a self-hosted WCF. As the link you provided mentioned, the Service host class is used to host the WCF service, which means that we can host the service in the Console/Winform, and so on.
Here is an example of hosting the service in a Winform application.
public partial class Form1 : Form
{
ServiceHost serviceHost = null;
public Form1()
{
InitializeComponent();
Uri uri = new Uri("http://localhost:9009");
BasicHttpBinding binding = new BasicHttpBinding();
serviceHost = new ServiceHost(typeof(MyService), uri);
serviceHost.AddServiceEndpoint(typeof(IService), binding, "");
ServiceMetadataBehavior smb = new ServiceMetadataBehavior()
{
HttpGetEnabled = true
};
serviceHost.Description.Behaviors.Add(smb);
System.ServiceModel.Channels.Binding mexbinding = MetadataExchangeBindings.CreateMexHttpBinding();
serviceHost.AddServiceEndpoint(typeof(IMetadataExchange), mexbinding, "mex");
serviceHost.Open();
}
private void Form1_Load(object sender, EventArgs e)
{
if (serviceHost.State==CommunicationState.Opened)
{
this.label1.Text = "Service is running";
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (serviceHost.State==CommunicationState.Opened&&serviceHost.State!=CommunicationState.Closed)
{
serviceHost.Close();
}
}
}
[ServiceContract]
public interface IService
{
[OperationContract]
string Test();
}
public class MyService:IService
{
public string Test()
{
return DateTime.Now.ToLongTimeString();
}
}
After that, we could consume it by using a client proxy.
https://learn.microsoft.com/en-us/dotnet/framework/wcf/accessing-services-using-a-wcf-client
Feel free to let me know if there is anything I can help with.

Firefox web extension - read local file (last downloaded file)

Im creating a web extension and porting from XUL. I used to be able to easily read files with
var dJsm = Components.utils.import("resource://gre/modules/Downloads.jsm").Downloads;
var tJsm = Components.utils.import("resource://gre/modules/Task.jsm").Task;
var fuJsm = Components.utils.import("resource://gre/modules/FileUtils.jsm").FileUtils;
var nsiPromptService = Components.classes["#mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService);
....
NetUtil.asyncFetch(file, function(inputStream, status) {
if (!Components.isSuccessCode(status)) {
return;
}
var data = NetUtil.readInputStreamToString(inputStream, inputStream.available());
var data = window.btoa(data);
var encoded_data_to_send_via_xmlhttp = encodeURIComponent(data);
...
});
This above will be deprecated.
I can use the downloads.download() to know what was the last download but I can NOT read the file and then get the equivalent for encoded_data_to_send_via_xmlhttp
Also in Firefox 57 onwards, means that I have to try to fake a user action by a button click or something, or upload a file.
Access to file:// URLs or reading files without any explicit user input
isnt there an easy way to read the last downloaded file?
The WebExtension API won't allow extensions to read local files anymore. You could let the extension get CORS privilege and read the content directly from the URL via fetch() or XMLHttpRequest() as blob and store directly to IndexedDB or memory, then encode and send to server. This comes with many restrictions and limitations such as to which origin you can read from and so forth.
Also, this would add potentially many unneeded steps. If the purpose is, as it seem to be in the question at the moment, to share the downloaded file with a server, I would instead suggest that you obtain the last DownloadItem object, extract the URL (.url) from that object and send the URL back to server.
This way the server can load directly from that URL (and encode it on server if needed). The network load will be about the same (a little less actually since there is no Base64 encoding involved which adds 33% to the size), and much less load on the client. The server would read the data as a binary/byte data stream; about the same as if the data was sent directly from the extension.
To obtain the last downloaded file you would do the following from a privileged script:
browser.downloads.search({
limit: 1,
orderBy: ["-startTime"]
})
.then(getLastDownload);
function getLastDownload(downloads) {
if (downloads.length) {
var url = downloads[0].url;
// ... send url to the server and let server fetch the data from it directly
}
}
According to this support mozilla question.
(2) Local file security
Firefox limits access from pages on web servers to pages on local disk or UNC paths. [...]).
Which solution ?
Use local-filesystem-links firefox addon (not tested)
and/or
run a small local webserver on client side, supposing server was run with sufficient privileges, you may finally access any local content via http:// (but still cannot with file:///)

When self-hosting what exactly causes AddressAccessDeniedException : HTTP could not register URL

I am writing a bdd test for a component that will startup phantomjs and hit a specific route on my site and do processing on that. Because the component is fundamentally about automating a phantom instance there is no way to easily stub out the http requests.
So I want to stub out a self-hosted endpoint that will stub out the data I'm after. Because this is a unit test I think its really important for it to run in isolation so I do something like this:
async Task can_render_html_for_slide_async() {
var config = new HttpSelfHostConfiguration("http://localhost:54331");
config.Routes.MapHttpRoute("Controller", "{controller}", new {});
using (var server = new HttpSelfHostServer(config)) {
server.OpenAsync().Wait();
var client = new HttpClient();
var resp = await client.GetStringAsync(config.BaseAddress+"/Stub");
Console.WriteLine(resp);
}
}
public class StubController : ApiController
{
public string Get() {
return "Booyah";
}
}
Which gets me
AddressAccessDeniedException : HTTP could not register URL http://+:54331/
I understand that netsh or Admin mode is required for this but I don't understand why. Nodejs for example runs perfectly fine on windows but has no such requirement.
Also using OWIN directly needs no netsh-ing. So....what's going on?
I wrote an article about it on codeproject, it was done to make it possible for multiple application to share the same port.
You can have both, IIS and Apache (or OWIN in your case) listenening port 80. The routing to the right application is done thanks to the path of the url.
IIS and Apache both would use this driver (http.sys). But you need permission to "reserve" a path.
Administrators are always authorized. For other users, use netsh or my GUI tool HttpSysManager to set ACL.
Any method that requires giving permission via netsh uses a Windows kernel driver to provide http access.
If a library opens a socket itself and handles the http communication that way, no netsh use is needed.
So to answer your question, some methods are using the kernel driver and some are handling the protocol themselves.

Communication between C# running on Windows and C running on linux with protobuf

I have a server application running on Linux. This application was
developed using protobuf c and protobuf.rpc.c files for RPC communication.
I have a client application which was running on windows.It was developed in c# using protobuf-net.dll and ProtobufRemote.dll for RPC communication.Both application using the same proto file having same service methods.
I can able to create a proxy from C# client application with the below code.
using System.Configuration;
using System.Net.Sockets;
using ProtoBufRemote; // rpc reference
using StarCall; // proto file
#region Create client connection
Int32 port = Convert.ToInt32(ConfigurationManager.AppSettings["PORT"]);
TcpClient tcpClient = new TcpClient(ConfigurationManager.AppSettings["SERVERIP"].ToString(), port);
var controller = new RpcController();
var client = new RpcClient(controller);
var channel = new NetworkStreamRpcChannel(controller, tcpClient.GetStream());
channel.Start();
var service = client.GetProxy<Istarcall_services>();
if (service == null)
Console.WriteLine("error creating client..");
//now calls can be made, they will block until a result is received
Console.WriteLine("Client connected to Server....\n");
#endregion
But whenever I am trying to invoke a service method from C# client application as shown below, the application is hanging and not getting any response from Linux c server application.
try
{
Room_Config room = new Room_Config();
room.Room_Dial_Num = 1;
Room_Config roomRet = service.read_room(room); // service method
}
catch (Exception)
{
throw;
}
The application is hanging in the below code.
protected RpcMessage.Parameter EndAsyncCallHelper(string methodName, IAsyncResult asyncResult)
{
PendingCall pendingCall = (PendingCall)asyncResult;
pendingCall.AsyncWaitHandle.WaitOne(); // application hanging here
pendingCall.AsyncWaitHandle.Close();
if (pendingCall.IsFailed)
throw new InvalidRpcCallException(serviceName, methodName,
String.Format("Server failed to process call, returned error message: \"{0}\".",
pendingCall.ServerErrorMessage));
return pendingCall.Result;
}
According to above mentioned scenarios, I have the following queries.
Whether this protobuf remote c# dll can help to create a communicatgion from the linux c code. If not please help me how to create a communication channel with the linux c code.
Please provide if any alternative rpc dll for c# client application to communicate to linux protobuf c and protobuf rpc.c file.
Please tell me if my above approach is wrong and rectify with the suitable solution.
Please help me out. If not clear please send to mail mentioned below.
Have you implemented your server in Linux with ProtoBufRemote cpp available at https://code.google.com/p/protobuf-remote/ ??
If yes, then you must have replaced or modified SocketRpcChannel.cpp class as it is using WinSock2 that is not applicable on Linux.
Have you done so? If yes, please share modified SocketRpcChannel class that you have used in your server.
Thank you.

.NET IE BHO Remoting

I have a IE Browser Helper Object, which is a Toolbar addin for IE 8.
I have another .NET .EXE application (Remoting Client) that connects to this BHO (Remoting Server) using remoting via common Interface.
When I test the communication between the .EXE application and a TEMP Console application with the same code used in the Server component, it communicates fine, and runs the remote Method.
However, when i try and communicate with the BHO server with security on the TCP cahannel ON ChannelServices.RegisterChannel(tcpClientChannel, true); , I get a "FileNotFoundException" Could not load file or assembly "xxxx" which "xxxx" is the common Interface assembly that contains the Server Methods.
When i try and communicate with the BHO server with security on the TCP cahannel OFF ChannelServices.RegisterChannel(tcpClientChannel, false); , I get error "connection to the remote object was forcably closed".
If I re-test it with the simple test console app it work.
Im starting to believe the problem is with the way remoting works inside a BHO instance... Has anyone used Remoting in a BHO .NET instance, Im using the SPICIE library to create the BHO using .NET.
COMMON Interface assembly for Remoting Interface Object
namespace WWie.CommonClasses
{
class WWieRemote : MarshalByRefObject, WWieClassLibrary.WWieCommonClass.IGetHtmlElement
{
public string GetElementClicked()
{
return ("Returned from WWieRemote ");
}
public void SetElementClicked(string str)
{
MessageBox.Show("SetElement " + str);
}
}
}
CLIENT APP
static TcpChannel tcpClientChannel = new TcpChannel();
public static WWieClassLibrary.WWieCommonClass.IGetHtmlElement remoteObject;
ChannelServices.RegisterChannel(tcpClientChannel, false);
remoteObject = (WWieClassLibrary.WWieCommonClass.IGetHtmlElement)Activator.GetObject(typeof(WWieClassLibrary.WWieCommonClass.IGetHtmlElement), "tcp://localhost:9002/TestWWie");
testing with remote method call
remoteObject.SetElementClicked("from Client");
SERVER BHO
TcpChannel tcpServerChannel = new TcpChannel(9002);
ChannelServices.RegisterChannel(tcpServerChannel, true);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(WWieClassLibrary.WWieCommonClass.IGetHtmlElement), "TestWWie", WellKnownObjectMode.Singleton);
Since IE is running in protected mode by default, it usually doesn't have access to communicate with higher integrity processes. If your url is in the intranet zone, you can push a policy that disable protected mode for the intranet zone.
Otherwise you may want to look for other options, like shared memory, named pipe, hidden worker windows & registered messages & customized message filter for Vista's UIPI, etc.

Resources