ASP.NET Core MVC on Windows IoT (Raspbian Pi) - asp.net-mvc

I am new to Windows IoT and am trying to get my first dot net core app running on a Raspberry Pi.
It is not because I think the Raspbian Pi is the perfect place to host web sites, but my ambition is to implement a measurement and control system on the Pi, and make the whole thing accessible through a REST API.
First things first, I wanted to create a standard dot net core app from the VS2017 template and get it running on the Pi.
The template built an App that was available on http://localhost:62100.
As I knew from previous experiments, the app was only listening on localhost, so I modified the Program class as follows:
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args)
{
var configuration = new ConfigurationBuilder()
.AddCommandLine(args)
.Build();
var hostUrl = configuration["hosturl"];
if (string.IsNullOrEmpty(hostUrl))
hostUrl = "http://0.0.0.0:62100";
return new WebHostBuilder()
.UseKestrel()
.UseUrls(hostUrl)
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseConfiguration(configuration)
.Build();
}
}
And now the server was also usable from my phone using the PC’s IP address.
In order to prepare the project for the Pi, I opened a PowerShell in the folder, where the project file resides (in my case C:\Users\\Documents\Visual Studio 2017\Projects\AspNetCore\AspNetCore) and ran the command:
dotnet publish -r win10arm
And then I copied all of the files placed under bin\Debug\netcoreapp2.0\win10-arm\publish to the Pi and start the application from the PowerShell connected to the Pi:
.\AspNetCore.exe
The Pi (after some deep thought) answers as when it was run on the PC:
[192.168.200.106]: PS C:\CoreApplications\AspNetCore> .\AspNetCore.exe
Hosting environment: Production
Content root path: C:\CoreApplications\AspNetCore
Now listening on: http://0.0.0.0:62100
Application started. Press Ctrl+C to shut down.
But trying to access the server from my browser (http://192.168.200.106:62100) times out with ERR_CONNECTION_TIMED_OUT.
What am I missing?

You need to add the port to firewall with following cmdlet in powershell,by default ASP.Net Core binds to http://localhost:5000.
netsh advfirewall firewall add rule name="ASPNet Core 2 Server Port" dir=in action=allow protocol=TCP localport=62100

Related

HOST NOT RESOLVABLE seen ONLY for FIRST request with RemoteWebDriver

HOST NOT RESOLVABLE seen ONLY for FIRST request with selenium-server-standalone 2.41(Machine A) RemoteWebDriver, Firefox 28 on Machine B alone with hub and node on the same MACHINE B.
The debugging session is going on from two days with no concrete outcome. Can anyone please point us in the right direction?
Are we missing anything as part of setup here? What is the correct way to make use of selenium-server-standalone 2.41 with Firefox 28 for RemoteWebDriver usecase?
Maven Dependency
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server-standalone</artifactId>
<version>2.41.0</version>
</dependency>
SETUP AND EXECUTION DETAILS
We have two machines Machine A (ARM64) , Machine B(Linux X86).
The way we are making use of it now is as follows,
Machine A(Linux ARM64) is where RemoteWebDriver invocation occurs, selenium-server-standalone-2.41.0.jar is used.
Machine B(Linux x86), we have a running docker container acts as both hub and node, Expose 4444 port from CONTAINER to HOST MACHINE B
java -jar /u01/selenium/selenium-server-standalone-2.44.0.jar -role hub
java -jar /u01/selenium/selenium-server-standalone-2.44.0.jar -role node -hub http://localhost:4444/grid/register
Access the HOST:port from ARM based machine
OUTPUT SEEN
First connection results in WebDriver Exception, HOST NOT RESOLVABLE, however, subsequent connetions results in no expections, everything just works after first request failure. Here, Geckodriver is not used as we are making use of selenium 2.41, as per MOZ documention
https://firefox-source-docs.mozilla.org/testing/geckodriver/Support.html
CODE USED
The below code is executed from MACHINE A.
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
public class Main {
public static void main(String[] args) throws Exception {
DesiredCapabilities capability = DesiredCapabilities.firefox();
WebDriver driver = new RemoteWebDriver(new URL("http://<<MACHINEB>>:4444/wd/hub"), capability);
driver.manage().window().maximize();
driver.get("http://localhost:4444");
Thread.sleep(10000);
driver.close();
}
}
I guess the issue here is not with your Selenium code rather with the server you are calling. Probably it is initially "sleeping" and "wakes up" after it detects some request coming to it.

How can I expose a .NET Framework console app port in docker?

I have a .NET Framework app that is self-hosting a kestrel (.NET Core) server. When I run this from the command line, the web-app is accessible through any browser at "http://localhost:5000/" (I can change the port if required). However, when I put this in a Docker container, I am not able to make the web app visible to the host machine.
I am using windows images in Docker. I have been able to create ASP.NET apps (for testing) that are accessible in the host machine, but I am not able to make this .NET Framework + Kestrel combination work. The code that starts the server is this:
webHostBuilder.UseKestrel()
.UseIISIntegration()
.CaptureStartupErrors(true)
.ConfigureKestrel((context, options) => { options.ListenLocalhost(port); })
.UseStartup<Startup>();

How to fix "An error occurred while processing your request" while calling an action method from a container (VS2017 / Docker)?

I created an image from .net core project in vs2017 and create and run a container from it ( via Docker).
The project works fine when I debug it from VS2017 and request for this url :
Url (debugged project) : https://localhost:44363/api/Movies/getM
which returns proper response Which I expected.
but when I call the url from started / running container it returns this error :
Url (Container) : http://localhost:8080/api/Movies/getM
How to solve it? any help would be appreciated .
this is the action method of getM:
[HttpGet]
[Route("api/Movies/getM")] // stdid,sourseid
public dynamic getdataJoin3Table(/*int id*/)
{
ResponseModel _objResponseModel = new ResponseModel();
_objResponseModel.Info = "res";
_objResponseModel.ResponseStatus = true;
_objResponseModel.ResponseMessage = "Data Received successfully";
return _objResponseModel;
}
docker container logs:
According to the information you have provided it seems like you are trying to connect to the local database which is located on the host system. When you run it from Visual Studio it can find the local database and connect to it. But when you run your app in the docker container the localhost becomes the docker instance so it cannot connect to database anymore because it doesn't run in the same docker container.
There are numbers of approaches how to resolve this issue. First - you can run your database in another docker container and link both app and db containers together using Docker Compose. Another approach is to set the connection string to your database by setting the environment variable for your docker container. This connection string should point to host's database. Another option would be to use some external database which can be accessed from both host and docker instances.

Dockerized Asp.net core 2.1 - Hangfire not starting

Situation:
Use Hangfire with Asp.net core 2.1 and Docker.
App crashes on docker run. When I comment all codes related to
Hangfire, app works perfectly.
If I run the app via IIS express on Visual Studio, app works fine
with Hangfire.
Issue:
Application startup exception: System.Exception: Cannot connect to SQL Server Browser. Ensure SQL Server Browser has been started. ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException: No such device or address
\\\\\\\\\\\\\\\\\\\\\\
\\\\\\\\\\\\\\\\\\\\\\\
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider)
at Hangfire.HangfireApplicationBuilderExtensions.ThrowIfNotConfigured(IApplicationBuilder app)
at Hangfire.HangfireApplicationBuilderExtensions.UseHangfireServer(IApplicationBuilder app, BackgroundJobServerOptions options, IEnumerable`1 additionalProcesses, JobStorage storage)
error on docker run
Connection strings on appsettings.json:
"ConnectionStrings": {
"TestDB": "Server=[server_instance]\\LOCALHOST;database=[db1];uid=[user];pwd=[pwd]",
"HangfireDB": "Server=[server_instance]\\LOCALHOST;database=Hangfire;uid=[user];pwd=[pwd]"
},
Any idea guys why this is happening?
Thanks
Change the server values in your connection strings from LOCALHOST to be your ip

Xamarin VS2015 ios app connect to web app api

I have a iOS app that I am building in Visual Studio 2015 with Xamarin. I also have a Web Application with an Api, no authorisation. But when I try to connect to the api I get an error saying "Connection Refused".
I call the api through the following code:
var client = new HttpClient();
var data = await client.GetAsync("http://localhost:7074/api/values/get");
Config your Web API with an IP Address, say local 192.168.x.x
And replace localhost by that IP
var data = await client.GetAsync("http://191.268.x.x:7074/api/values/get");
localhost will point to your iOS device/emulator, not the server. You need to specify the IP or FQDN in the url. Also be be sure the firewall is open and that IIS is setup to accept remote connections.
localhost is your iPhone / iPad or simulator (the device where you run your app). You have to change it to the IP/Domain of your server.
If the server is your dev machine, ensure, that your phone is connected to the same network (e.g. via WiFi), else you might have the same problem, because your dev machine might not be reachable.
You can find the IP of your dev machine by typing ipconfig | find "IPv4" into the console (on Windows).

Resources