ASP.NET PhysicalFileProvider not available in Startup.cs - c#

I am trying to serve static files in a ASP.NET 4.6 web application, MVC 5.2.3 using the Entity Framework. I'm following this tutorial.
I'm getting a compiler error:
The type or namespace name 'PhysicalFileProvider' could not be found
(are you missing a using directive or an assembly reference?)
It seems that the using Microsoft.Extensions.FileProviders is not actually being used (it is grayed out in Visual Studio.
I have tried importing various assemblies without any luck. I'm not a .NET developer and any help would be greatly appreciated.
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Primitives;
using Microsoft.Owin;
using Microsoft.Owin.StaticFiles;
using Microsoft.Extensions.Configuration;
using Owin;
[assembly: OwinStartup(typeof(Monica.Startup))]
namespace Monica
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseStaticFiles(); // For the wwwroot folder
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), #"MyStaticFiles")),
RequestPath = new PathString("/StaticFiles")
});
}
}
}

You should install the nugget package:
Microsoft.Extensions.FileProviders.Physical
Right click the project -> Manage Nugget Packages -> Browse -> Microsoft.Extensions.FileProviders.Physical -> Install.
The using statement should be available to you afterwards.

Related

running C# libraries inside another project

I'm trying to get this library to work inside my project. I've downloaded it to my documents folder and in the same documents folder I've created a new C# console app with just a Program.cs to try and run it. I can compile the library as far as I know.
Now, I've imported the project into my project using:
dotnet add C:/blabla/documents/tachograph-reader-test-code/tachograph-reader-test-code.csproj reference C:/blabla/documents/tachograph-reader/src/tachograph-reader-lib.csproj
and this worked just fine. As the following piece of code is added to my project file:
<ItemGroup>
<ProjectReference Include="..\tachograph-reader\src\tachograph-reader-lib.csproj" />
</ItemGroup>
Now for the life of me, I can't figure out how to use the library?
Intellisense does not see the library in any way. the using keyword doenst work either. De namespace should be DataFileReader but I cannot use it, and it won't compile.
By the way, I'm using VS CODE not vs studio.
So, how do I use such a library inside my project?
The problem is not in your project reference. I guess you have to add these three namespaces as follow:
using System.IO;
using System.Text;
using System.Xml;
Your Program.cs should look like:
using System;
using System.IO;
using System.Text;
using System.Xml;
using DataFileReader;
namespace tachograph-reader-test-code
{
class Program
{
static void Main(string[] args)
{
...
}
...
}
}

Why doesn’t using a missing package cause Visual Studio to complain about my Azure Function?

I’m new to C#. In my project, I have a file which contains this code:
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
namespace MyNamespace {
public static class MyClass {
// ...
static MyClass () {
// ...
}
[FunctionName("...")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequest req,
ILogger log) {
log.LogInformation("...");
// ...
}
}
}
For all those using statements, I have a corresponding package listed under Solution explorer → Dependencies → Packages, except for Microsoft.Extensions.Logging. Now, Visual Studio doesn’t complain about this fact and the build process works just fine. Running the application, however, this error appears when it reaches the log.LogInformation("...") statement:
So, I install it from NuGet:
but doing that, I obtain a different error from the console of the Azure Function, that is:
What should I do?
Have you installed dependent packages like Microsoft.Extensions.Logging.Abstractions. Its strange by default dependent packages are installed as part of creating new Azure function. I would suggest try once again using Visual studio template of Azure function.

How to add an App.Config in Visual Studio Code?

How to configure a C# program with a connection string in Visual Studio Code?
This is for .Net Core 2.2 using .Net Core SDK version 2.2.203 in Visual Studio Code 1.34.0
I have tried to add App.Config file to the project, but I'm not able to resolve this issue. Do let me know any solution to configure connection string.
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Collections.Specialized;
using Dapper;
namespace Sample_Dapper
{
class Program
{
static void Main(string[] args)
{
IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
var SqlString = "SELECT TOP 100 [CustomerID],[CustomerFirstName],
[CustomerLastName],[IsActive] FROM [Customer]";
var ourCustomers = (List<Customer>)db.Query<Customer>(SqlString);
}
}
}
Install Microsoft.Extensions.Configuration.Json
Then add appsettings.json file to project and right click on file -properties and select copy to output as copy if newer
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
IConfigurationRoot configuration = builder.Build();
Console.WriteLine(configuration.GetConnectionString("Test"));
Console.WriteLine(configuration.GetSection("SampleObj:AnyPropName").Value);
sample appsetting.json file
{
"SampleObj": {
"AnyPropName" : "testProp"
} ,
"ConnectionStrings": {
"Test": "CONNECTION-STRING"
}
}
Your code has ConfigurationManager.ConnectionStrings to get the connection string. This code will not work in .NET Core 2.2 and later, because ConfigurationManager.ConnectionStrings is used to get connection strings in web.config for ASP.NET project and app.config for console and Winforms/WPF projects if your app is using .NET Framework.
In .NET Core 2.0 and later, most configuration is stored as JSON file, and by default the equivalent of web.config/app.config is appsettings.json file, and you can also have your own configuration file as well, because it is highly extensible.
The official .NET Core 2.0 (or later) documentation on how to manage configuration files is available at: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.2

How can i use an SQLiteAsyncConnection for Android using Xamarin?

I am trying to implement SQLite in a Xamarin solution using Visual Studio. However, i can not find the proper way to implement an SQLiteAsyncConnection.
I am following along a tutorial, which just passes a string to the constructor of the SQLiteAsyncConnection, however this is (no longer?) accepted. I googled to find a newer solution and almost everywhere found the solution to add a platform to the constructor, however that results in an error for me.
I think i might not have added the correct NUGet packages to the project(s). I have added the sqlite-net-pcl package to both the shared project and the android project.
I have also updated all NUGet packages today, rebuilt the solution and even deleted the /bin and /obj folder and it still is not working.
using System.IO;
using SQLite.Net;
using SQLite.Net.Async;
using Xamarin.Forms;
using SQLiteTest.Droid;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
[assembly: Dependency(typeof(SQLiteDB))]
namespace SQLiteTest
{
public class SQLiteDB : ISQLiteDB
{
public SQLiteAsyncConnection GetAsyncConnection()
{
const string fileName = "test.db3";
var documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var path = Path.Combine(documentsPath, fileName);
var platform = new SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid();
var cwLock = new SQLiteConnectionWithLock(platform, new SQLiteConnectionString(path, true));
var connection = new SQLiteAsyncConnection(() => cwLock);
return connection;
}
}
}
The error i get is:
Error CS0234 The type or namespace name 'Platform' does not exist in the namespace 'SQLite.Net' (are you missing an assembly reference?) SQLiteTest.Android D:\Mobile Apps\Mosh\SQLiteTest\SQLiteTest\SQLiteTest.Android\SQLiteDB.cs
Well, I am pretty sure you have not added the SQLite.Net.Platform.XamarinAndroid.dll package to your native Android project which is causing this issue when you use this dependency service.
If you are using SQLite-net and or SQLite.Net-PCL look for the Xamarin.Android platform dll int he path below:
packages\SQLite.Net-PCL.3.1.1\lib\MonoAndroid\`SQLite.Net.Platform.XamarinAndroid.dll`

Not getting Startup.Auth file

I am trying to install steam login, for my website, BUT after i have done the installation in nuget package manager console, with the following install code: Install-Package Owin.Security.Providers.Steam I am missing my Startup.Auth.cs. What am i doing wrong?
The project is started without "Individual User Accounts".
The Startup.Auth.cs file should be in your App_Start Folder. Just create a default MVC web application with Individual User Accounts selected as the Authentication type under Change Authentication option to see how its implemented.
For reference:
using Microsoft.Owin;
using Microsoft.Owin.Security.Cookies;
using Owin;
using Owin.Security.Providers.Steam;
namespace WebApplication
{
public partial class Startup
{
public void ConfigureAuth(IAppBuilder app)
{
//other Config
app.UseSteamAuthentication("your API key");
}
}
}
See here for reference on how to add the Steam login button.
Edit:
Also see here for Adding MVC 5 Identity to our Existing Project.
You may need to add startup.cs to root of your project:
using Microsoft.Owin;
using Owin;
[assembly: OwinStartupAttribute(typeof(WebApplication1.Startup))]
namespace WebApplication1
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
}
}
}

Categories