Check if call came from API (.Net Core) or MVC

Asked

Viewed 108 times

2

I have some applications that need to share the same Repository (Class Library).

The Repository project is created and working, with MVC applications. The problem is that it searches for the Connection string using Configurationmanager, directly in the repository.

I need to create an API in . Net Core and the API configuration is not accessed by Configurationmanager. Then, when the call is made, it gives error when accessing the Configurationmanager.

There is a way to search for the Connection string via Iconfiguration or Configurationmanager, depending on the origin of the call, and without having to pass the controller’s Iconfiguration pro repository?

  • Both are Aspnet Core? and the two applications are in the same Project?

  • Have you considered the idea of passing the Connection string per parameter when instantiating the repository? So you leave the code less coupled :)

  • Some applications are MVC (.Net Framework) and the new API is Aspnet Core. I already have many calls to these repositories and would not like to change all. If there is a way to get through it without having to change the signature of the methods, it would be better.

  • There is a lot of nonsense in the market dividing the whole project mainly those who are imposing DDD, but, the database layer and its connection need to be independent of the project and in the project you pass the connectivity. NetFramework and NetCore has different peculiarities but, this may be solved with a class where the same seeks the connection depending on the project. You need to exemplify how are today the two projects to get a real idea of the problem

  • 1

    @Virgilionovic then, I have classes like Usuariorepository, for example, that need to be accessed by projects of both types. But in the repository, I simply use: public Static string Connectionstring => Configurationmanager.Connectionstrings["CS"]. Connectionstring;

  • Netframework is which version?

  • 1

    @Virgilionovic Framework: 4.6.1 and Core: 2.1

Show 2 more comments

2 answers

1


Propose for your code, a package that has in its code particularities being that the version of the project makes reference to its current model of its two projects ASPNETFULL and ASPNETCORE as follows:

Create a Library Class and then edit your file .csproj and in the content change to:

<Project Sdk="Microsoft.NET.Sdk">  
  <PropertyGroup>
      <RuntimeFrameworkVersion>2.1.9</RuntimeFrameworkVersion>
    <TargetFrameworks>net46;net461;net47;netstandard2.0;netcoreapp2.1;netcoreapp2.2</TargetFrameworks>
  </PropertyGroup>
  <ItemGroup Condition="'$(TargetFramework)'=='net46'">    
    <Reference Include="System.Configuration" />
  </ItemGroup>
  <ItemGroup Condition="'$(TargetFramework)'=='net461'">
    <PackageReference Include="System.Configuration.ConfigurationManager" Version="4.5.0" />    
  </ItemGroup>
  <ItemGroup Condition="'$(TargetFramework)'=='net47'">
    <PackageReference Include="System.Configuration.ConfigurationManager" Version="4.5.0" />
  </ItemGroup>
  <ItemGroup Condition="'$(TargetFramework)'=='netstandard2.0'">
    <PackageReference Include="System.Configuration.ConfigurationManager" Version="4.5.0" />
  </ItemGroup>
  <ItemGroup Condition="'$(TargetFramework)'=='netcoreapp2.1'">
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="2.1.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.1.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
  </ItemGroup>
  <ItemGroup Condition="'$(TargetFramework)'=='netcoreapp2.2'">
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="2.1.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.1.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
  </ItemGroup>
  <ItemGroup Condition="'$(TargetFramework)'=='netstandard2.0'">
    <PackageReference Include="System.Configuration.ConfigurationManager" Version="4.5.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.2.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
  </ItemGroup>  
</Project>

that will make this Library is compatible with both projects and the main code where you find the connection settings is:

namespace Library
{
    public class FindConnection
    {
#if NET46 || NET461 || NET47 || NETSTANDARD2_0
        public System.Guid FullId { get; } = System.Guid.NewGuid();
        public static string StringConnection
        {
            get
            {
                return System.Configuration.ConfigurationManager.ConnectionStrings["minhaconnection"].ToString();
            }
        }
#endif
#if NETCOREAPP2_1 || NETCOREAPP2_2
        public System.Guid CoreId { get; } = System.Guid.NewGuid();
        public FindConnection (Microsoft.Extensions.Configuration.IConfiguration configuration)
        {
            StringConnection = configuration.GetSection("ConnectionStrings:minhaconnection").Value;
        }
        public string StringConnection { get; }
#endif
    }
}

in this code will happen a conditional compilation and so the code will fit in the two projects and of course each with its particularity.

To use this Library add reference to your projects and on ASPNETFULL use as follows:

string stringConnection = FindConnection.StringConnection;

and in the project ASPNETCORE use with dependency injection configuring:

public void ConfigureServices(IServiceCollection services)
{
    services.AddScoped<FindConnection>(); // aqui
    services.AddMvc()
        .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}

and in your controller for example:

public class HomeController : Controller
{
    public FindConnection FindConnection { get; }

    public HomeController(FindConnection findConnection)
    {
        FindConnection = findConnection;
    }

I would do even more in your Repository layer create a constructor with this class and do everything for dependency injection facility found in the ASPNETCORE.

0

First you need to install Configurationmanager through Nuget on ASP.NET Core (considering that you are using 2.0, previous to this has no support):

Install-Package System.Configuration.ConfigurationManager -Version 4.5.0

Now create the app.config with the settings:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="minhaconnection" connectionString="server=localhost;database=legacy" />
  </connectionStrings>
</configuration>

Finally, access as normal:

System.Configuration.ConfigurationManager.ConnectionStrings["minhaconnection"].ConnectionString

Browser other questions tagged

You are not signed in. Login or sign up in order to post.