ASP.NET 5 Kestrel connect within LAN - c#

I would like to connect to my Kestrel server with ASP.NET 5 application hosted on it from another PC in the same network. Is it possible? I can ping my computer from cmd, but I get 'Connection timed out' when I try to connect from a web browser (I type this: "http://{my_kestrel_ip}:5000/").

The hosting.ini was not working for us. I have to add this to the project.json file. I believe that the hosting.ini file is being deprecated after Beta8.
--server.urls http://0.0.0.0:5000
or I prefer the following which I believe is less confusing.
--server.urls http://*:5000
So you would end up with something like this in your project.json.
"commands": {
"web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.Kestrel --server.urls http://0.0.0.0:5000",
"ef": "EntityFramework.Commands"
},

In your project folder you should have a file called hosting.ini. In that file by default you should have something like this:
server=Kestrel
server.urls=http://localhost:5000
You need to make the HTTP server listen on your public IP address as well as localhost. To do that, you can add an additional address by separating them with a semi colon:
server.urls=http://localhost:5000;http://{my_kestrel_ip}:5000

Just did a quick test that seems to work.
Create a hosting.json file beside your project.json file.
hosting.json:
{
"server.urls": "http://localhost:5000;http://192.168.1.4:5000"
}
project.json:
"commands": {
"web": "Microsoft.AspNet.Server.Kestrel --config hosting.json"
},
In a command prompt simply run dnx web, output:
Hosting environment: Production
Now listening on: http://localhost:5000
Now listening on: http://192.168.1.4:5000
Application started. Press Ctrl+C to shut down.
You'll get a firwall prompt, accept it, and tadaaa!! You can access the site from the LAN and localhost.

Related

vscode c# debugger was not attached within the expected time limit

I'm running vscode version 1.73.1
I have the following extensions installed on a new VM that sits behind a corporate proxy:
Microsoft C# v1.25.2
Azurite v3.20.1
Azure Functions v1.9.0
I have a C# Azure Functions project that I'd like to debug.
My tasks.json file contains the following:
{
"type": "func",
"dependsOn": "build (functions)",
"options": {
"cwd": "${workspaceFolder}/bin/Debug/net6.0"
},
"command": "host start --dotnet-isolated-debug",
"isBackground": true,
"problemMatcher": "$func-dotnet-watch"
}
The launch.json is as follows:
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach to .NET Functions",
"type": "coreclr",
"request": "attach",
"processId": "${command:azureFunctions.pickProcess}"
}
]
}
I click the debug icon from the sidebar menu and choose "Run and Debug - Attach to .Net Functions"
The function app is then build and starts. The VM is slow so it takes more than 60 seconds for the function app to start. During that time VS code pops up a message to say is it slow. However, the function app does start and the http endpoints are presented in green text.
I then see the following presented in the terminal window:
Azure Functions .NET Worker (PID: 12116) initialized in debug mode.
Waiting for debugger to attach... Host lock lease acquired by instance
ID '00000000000000000000000083B41702'. A debugger was not attached
within the expected time limit. The process will continue without a
debugger. Worker process started and initialized.
I don't see any text in the "Debug Console"
I am able to call the http endpoint and a successful response is returned, but the breakpoint does not break!
If I run the same solution in vs code directly on my laptop, then the debugger is attached. Problem is, I need to debug on the corporate VM because the code selects from an on-prem sql db.
So, I guess there must be some difference between the two configurations of vs code / extensions or perhaps the corporate proxy is causing a problem?
Not sure which text file this gets saved to but was able to resolve by extending the timeout in the workspace preferences section:
I increased from the default of 60 (seconds) to 180

Why won't my windows service create and write to log file?

I have a windows service (.net 6), I am using Serilog.Extensions.Logging.File to add logging file, when I run exe file it creates a file and works fine, but when I install it as windows service it is not working. In Event log all looks good, if there is any error for service I can see it in event viewer log. (locally and in the server I have the same condition)
I am running under LOCAL SERVICE if that helps. It has Full access.
I have created the logs directory under my application folder.
appsetting.json file content:
"Logging": {
"File": {
"PathFormat": "Logs/log-{Date}.txt",
"fileSizeLimitBytes": 10485760,
"retainedFileCountLimit": 10
}
I checked the similar questions like this: Similar question not helpful for me
There was an issue in server which it was showing current working directory set to application folder but in fact it was saving in the windows folder, after restarting windows, it is solved.
A windows service, when registered and started, is not running from the folder from which you originally registered the service.
Instead, an instance of the service is held and the execution context is under the OS folder where services are held once registered.
Think of it as the "hosting directory" that you know from web-driven services proobably.
You can look up the directory of a service from the cmd with the command below:
sc qc <service name>

Testing with emulator - Can't get response from bot when running from publish directory

When i build and run the project everything works fine.
When i publish the project and run it from within publish directory, it doesn't respond to any message.
In the emulator i get the message:
Cannot post activity. Unauthorized.
POST403 directline.postActivity
dotnet myBot.dll //from within publish directory, bot runs, doesnt repond to any message
dotnet run //from project directory, everything works fine
So, the problem was that i was tying to test production configuration trough localhost endpoint and in my .bot file i had this:
{
"type": "endpoint",
"appId": "myId",
"appPassword": "myPass",
"endpoint": "https://my.azurewebsites.net/api/messages",
"name": "production",
"id": "4"
},
The production endpoint is the azure service. So, you either test locally on localhost or set your service on Azure to point to your localhost(ngrok provided link - In that case you need to configure ngrok to expose your localhost to the outside).
https://blog.botframework.com/2017/10/19/debug-channel-locally-using-ngrok/

.NET Core publishing to IIS problems - 403.14

I am trying to publish a Core 1.0 app to a 2012R2 box that runs fine locally in IIS Express.
Installed DotNetCore.1.0.0-WindowsHosting.exe on server
Installed httpPlatformHandler_amd64.msi
Set app pool to have 'no managed code'
published from VS using 'Web Deploy'
Latest VS Core Tools as of this writing
All I get is:
HTTP Error 403.14 - Forbidden
The Web server is configured to not list the contents of this
directory.
In this article from Microsoft it only mentions that a 403.14 Forbidden error is created by picking the wrong directory for the site... which is not the case.
VS does NOT pusblish the web.config, however. And there is no choice for 'copy to server always' or 'content' (in DDL), both choices are missing.
Make sure that the service account / user credentials set on the application pool has access to run / access the app. That was my issue when deploying to IIS a few times.
As for including the web.config, make sure to include the following code in your project.json file. Just specify what you want to include in the publish.
"publishOptions": {
"include": [
"wwwroot",
"Views",
"appsettings.json",
"web.config"
]
}
Check if there is web.config in your application's folder. If there is no web.config in the folder IIS don't know it's an ASP.NET Core application, it thinks you are trying listing the file in that folder.
So if you set IsTransformWebConfigDisabled property in the project configuration to true, which caused Web.config file isn't generated in the publish folder when build it.
So Change the IsTransformWebConfigDisabled property to false in YourProjectName.csproj file like this:-
<IsTransformWebConfigDisabled>false</IsTransformWebConfigDisabled>

Running ASP.NET 5 Cross-Platform

I'm interested in ASP.NET 5 on my Windows and Mac OS machines. To get started, I installed Visual Studio 2015 RC on my Windows machine. I created a new, empty web site for ASP.NET 5 (aka vNext). I updated the template with a Views directory and included the MVC and Static Files nuget packages. I can successfully run this "Hello World" app. I also was successful in checking it into GitHub and automatically deploying it to Azure as a Website.
Then, I cloned the repository on my Mac OS machine. I successfully ran dnu restore to get the packages. I then ran dnx . run. When I do this, I get an error though. The error is:
'Website' does not contain a static 'Main' method suitable for an entry point
What am I doing wrong? I have a Startup.cs file. I know it works based on the fact that it runs on Windows and in Azure. Yet, I can't figure out what I'm missing. My Startup.cs file looks like this:
Startup.cs
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.Framework.ConfigurationModel;
using Microsoft.Framework.DependencyInjection;
namespace Test.Site.Web
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
public void Configure(IApplicationBuilder app)
{
app.UseErrorPage();
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute("default",
"{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index" });
});
app.UseMvc();
app.UseWelcomePage();
}
}
}
What did I do wrong?
In your project.json file, there should be a set of commands to run the project. By default, one of those is web and one is kestrel. Kestrel is the server for OS X and Linux, it's based on libuv, the same library that powers Node.
"commands": {
"gen": "Microsoft.Framework.CodeGeneration",
"kestrel": "Microsoft.AspNet.Hosting --server Kestrel --server.urls http://localhost:5004",
"web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5002"
},
(I don't have VS 2015 in front of me at the moment, so I'm not 100% sure of the default commands in an "empty" project, so you may need to add the kestrel command).
So run this command to start your server on OS X or Linux:
dnx . kestrel
If you were starting it from the command prompt on Windows, you'd use:
dnx . web
Note you can customize the commands however you like. For example, one command might generate the database. Another might analyze the server for system requirements. One might even uninstall the application!

Categories