Comments (15)
Take a look at this sample code I have created for you. It uses a background worker to host the web server in your winforms app.
https://www.dropbox.com/s/yzbo365m8hlqrzf/WindowsFormsApplication1.zip?dl=0
from embedio.
Let me try to reproduce it.
from embedio.
I found some minor issues in the basic sample that I'll fix. I created a console app with the Nuget and the next code and it's working fine:
namespace Company.Project
{
using System;
using Unosquare.Labs.EmbedIO;
using Unosquare.Labs.EmbedIO.Log;
using Unosquare.Labs.EmbedIO.Modules;
class Program
{
/// <summary>
/// Defines the entry point of the application.
/// </summary>
/// <param name="args">The arguments.</param>
static void Main(string[] args)
{
var url = "http://localhost:9696/";
if (args.Length > 0)
url = args[0];
// Our web server is disposable. Note that if you don't want to use logging,
// there are alternate constructors that allow you to skip specifying an ILog object.
using (var server = new WebServer(url, new SimpleConsoleLog()))
{
// First, we will configure our web server by adding Modules.
// Please note that order DOES matter.
// ================================================================================================
// If we want to enable sessions, we simply register the LocalSessionModule
// Beware that this is an in-memory session storage mechanism so, avoid storing very large objects.
// You can use the server.GetSession() method to get the SessionInfo object and manupulate it.
// You could potentially implement a distributed session module using something like Redis
server.RegisterModule(new LocalSessionModule());
// Here we setup serving of static files
server.RegisterModule(new StaticFilesModule("c:/web"));
// The static files module will cache small files in ram until it detects they have been modified.
server.Module<StaticFilesModule>().UseRamCache = true;
server.Module<StaticFilesModule>().DefaultExtension = ".html";
// We don't need to add the line below. The default document is always index.html.
//server.Module<Modules.StaticFilesWebModule>().DefaultDocument = "index.html";
// Once we've registered our modules and configured them, we call the RunAsync() method.
// This is a non-blocking method (it return immediately) so in this case we avoid
// disposing of the object until a key is pressed.
//server.Run();
server.RunAsync();
// Fire up the browser to show the content if we are debugging!
#if DEBUG
var browser = new System.Diagnostics.Process()
{
StartInfo = new System.Diagnostics.ProcessStartInfo(url) { UseShellExecute = true }
};
browser.Start();
#endif
// Wait for any key to be pressed before disposing of our web server.
// In a service we'd manage the lifecycle of of our web server using
// something like a BackgroundWorker or a ManualResetEvent.
Console.ReadKey(true);
}
}
}
}
from embedio.
Thanks for the quick reply, I still get an exception which I thought was related to the access rights level to the file system, but raising the level to admin didn't made it go away. Any idea what this is?
My VS is in German, but the trace should be readable:
Web server prefix 'http://localhost:9696/' added.
Finished Loading Web Server.
"App.vshost.exe" (CLR v4.0.30319: App.vshost.exe): "C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\mscorlib.resources\v4.0_4.0.0.0_de_b77a5c561934e089\mscorlib.resources.dll" geladen. Das Modul wurde ohne Symbole erstellt.
Started HTTP Listener
System.Net.HttpListenerPrefixCollection
Listener Closed.
System.ObjectDisposedException: Auf das verworfene Objekt kann nicht zugegriffen werden
(object can't be accessed).
Objektname: "System.Net.HttpListener".
bei System.Net.HttpListener.CheckDisposed()
bei System.Net.HttpListener.EndGetContext(IAsyncResult asyncResult)
bei System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- Ende der Stapelüberwachung vom vorhergehenden Ort, an dem die Ausnahme ausgelöst wurde ---
bei System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
bei System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
bei System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
bei Unosquare.Labs.EmbedIO.WebServer.<<RunAsync>b__4>d__7.MoveNext()
from embedio.
It's seem like the tcp port can not be open. What Windows version are you
using?
On May 10, 2015 3:44 PM, "Saman" [email protected] wrote:
Thanks for the quick reply, I still get an exception which I thought was
related to the access rights level to the file system, but raising the
level to admin didn't made it go away. Any idea what this is?My VS is in German, but the trace should be readable:
Web server prefix 'http://localhost:9696/' added.
Finished Loading Web Server.
"App .vshost.exe" (CLR v4.0.30319: LernBar Studio.vshost.exe): "C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\mscorlib.resources\v4.0_4.0.0.0_de_b77a5c561934e089\mscorlib.resources.dll" geladen. Das Modul wurde ohne Symbole erstellt.
Started HTTP Listener
System.Net.HttpListenerPrefixCollection
Listener Closed.
System.ObjectDisposedException: Auf das verworfene Objekt kann nicht zugegriffen werden
(object can't be accessed).
Objektname: "System.Net.HttpListener".
bei System.Net.HttpListener.CheckDisposed()
bei System.Net.HttpListener.EndGetContext(IAsyncResult asyncResult)
bei System.Threading.Tasks.TaskFactory1.FromAsyncCoreLogic(IAsyncResult iar, Func
2 endFunction, Action1 endAction, Task
1 promise, Boolean requiresSynchronization)
--- Ende der Stapelüberwachung vom vorhergehenden Ort, an dem die Ausnahme ausgelöst wurde ---
bei System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
bei System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
bei System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
bei Unosquare.Labs.EmbedIO.WebServer.<b__4>d__7.MoveNext()—
Reply to this email directly or view it on GitHub
#10 (comment).
from embedio.
I would also suggest changing the port. Go for a url prefix such as:
From: Geovanni Perez [mailto:[email protected]]
Sent: Sunday, May 10, 2015 3:48 PM
To: unosquare/embedio
Subject: Re: [embedio] Basic example doesn't serve static files (#10)
It's seem like the tcp port can not be open. What Windows version are you
using?
On May 10, 2015 3:44 PM, "Saman" <[email protected]mailto:[email protected]> wrote:
Thanks for the quick reply, I still get an exception which I thought was
related to the access rights level to the file system, but raising the
level to admin didn't made it go away. Any idea what this is?My VS is in German, but the trace should be readable:
Web server prefix 'http://localhost:9696/' added.
Finished Loading Web Server.
"App .vshost.exe" (CLR v4.0.30319: LernBar Studio.vshost.exe): "C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\mscorlib.resources\v4.0_4.0.0.0_de_b77a5c561934e089\mscorlib.resources.dll" geladen. Das Modul wurde ohne Symbole erstellt.
Started HTTP Listener
System.Net.HttpListenerPrefixCollection
Listener Closed.
System.ObjectDisposedException: Auf das verworfene Objekt kann nicht zugegriffen werden
(object can't be accessed).
Objektname: "System.Net.HttpListener".
bei System.Net.HttpListener.CheckDisposed()
bei System.Net.HttpListener.EndGetContext(IAsyncResult asyncResult)
bei System.Threading.Tasks.TaskFactory1.FromAsyncCoreLogic(IAsyncResult iar, Func
2 endFunction, Action1 endAction, Task
1 promise, Boolean requiresSynchronization)
--- Ende der Stapelüberwachung vom vorhergehenden Ort, an dem die Ausnahme ausgelöst wurde ---
bei System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
bei System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
bei System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
bei Unosquare.Labs.EmbedIO.WebServer.<b__4>d__7.MoveNext()—
Reply to this email directly or view it on GitHub
#10 (comment).
—
Reply to this email directly or view it on GitHubhttps://github.com//issues/10#issuecomment-100699325.
from embedio.
I'm on Windows 8.1, VS 2013, .net 4.5
@mariodivece Changing the port didn't changed it, still the same exception. I tried a few ports.
from embedio.
Quick question: Are you trying from the nuget package or from source? If from source, can you do a step-through and let us know which line breaks it? If from nuget can you send us the version number pls?
Please forgive the brevity and typos. Message sent from a mobile device.
From: Samanmailto:[email protected]
Sent: 5/10/2015 3:53 PM
To: unosquare/embediomailto:[email protected]
Cc: Mario Di Vecemailto:[email protected]
Subject: Re: [embedio] Basic example doesn't serve static files (#10)
I'm on Windows 8.1, VS 2013, .net 4.5
@mariodivecehttps://github.com/mariodivece Changing the port didn't changed it, still the same exception. I tried a few ports.
—
Reply to this email directly or view it on GitHubhttps://github.com//issues/10#issuecomment-100700106.
from embedio.
I used the nuget package, version: 1.0.3
I could also try using the source code, but I'm not a full c# programmer, I'll try to figure it out.
from embedio.
Hold on, I didn't realized that the example was only supposed to be run from a console application. But still the exception irritated me.
However I created a console only application and it's seems to be OK. I ran it within an forms project.
Is there any guideline how I should/can embed this inside such an existing application, or can you give me some tips? Do the I have to keep the webserver running in a loop in a background or how do I handle this.
from embedio.
I created a new project from embedio package 1.0.3, added index.html to c:\web and it worked just fine... I'm attaching the zip file with the VS 2013 project. Can you let us know if it works?
https://www.dropbox.com/s/0zkg1vwn5qrsiy3/ConsoleApplication1.zip?dl=0
from embedio.
What kind of application are you writing? You don't have to run it in a console app. In fact we use it in Windows services and linux daemons. If you'd like to run it in say, a WPF app or a WinForms app, you'd just spawn a new thread and leave it there.
from embedio.
It's a desktop application which hosts a local web application and I want to implement a REST API to the web server, so I can move the application later online and retain the web application implementation, once the online service is done.
In the ideal case I would keep the current web server implementation I'm working on (and run on mono) and only change the existent legacy backend.
from embedio.
Got it. Guys you are just awesome and really helpful thank you! I might be annoying you in the near future again, since I'm playing with you project. Thanks again :)
from embedio.
You bet, and good luck with your project! Closing this one down.
from embedio.
Related Issues (20)
- When requesting a mp4 embedio is responding with identity in the content encoding header.
- Cannot control charset in ContentType header HOT 3
- How to send statuscode 202 HOT 1
- EmbedIO 3.5.0 NuGet package references wrong/old version of Unosquare.Swan.Lite HOT 22
- System.ObjectDisposedException when processing multiple requests at once HOT 2
- WithZipFile should open a read only handle(or have the option to) HOT 2
- FileModule or StaticFolderModule get stuck returning medium files HOT 4
- Add XML as response type
- EmbedIO Dual-Stack Localhost Listener Causes Empty Replies HOT 1
- How can I custom the Server header HOT 2
- Disable request logging only
- Cookies issue
- EmbedIO.Samples console app does not work when PublishAot is set to true HOT 2
- Web Server does not work in Xamarin.iOS (IOS-17) HOT 3
- How to report progess via api - event-stream
- HttpContext.OnClose not firing HOT 1
- Getting rid of double quotes around strings in a Web API response (turning off JSON serialization) HOT 1
- Issue Serving Static Folder Xamarin ( 3.5.2) Forms on Android
- Request.Url mixing up http and https
- server stop responding after some time HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from embedio.