Why is my Interface not being found? - c#

I'm getting, "The type or namespace name 'INRBQDeliveryRepository' could not be found (are you missing a using directive or an assembly reference?)" here:
public class DeliveryController : ApiController
{
private readonly INRBQDeliveryRepository _deliveryRepository;
The interface type I'm trying to reference there is:
namespace SeaStore.Data.Legacy.Interfaces
{
public interface INRBQDeliveryRepository
. . .
These are different projects in a solution (NRBQ.Web is trying to access its sibling, SeaStore.Data.Legacy); adding "using SeaStore.Data.Legacy.Interfaces;" to the class that prevents compilation with the "not found" error doesn't help - in fact, I go from the error noted above to getting an addition error, specifically:
"The type or namespace name 'Data' does not exist in the namespace 'SeaStore' (are you missing an assembly reference?)" in the Controller class here:
using SeaStore.Data.Legacy.Interfaces;
Why is my other-project interface not visible?

The error message you received is kind of selfexplanatory: are you missing an assembly reference.
It indicates that you are actually missing a reference to an assembly.
Add --> Reference --> Locate the library where your interface is defined, and add the reference.
The using should resolve itself then, because it can now find the namespace. Which means you can use your interface!

Related

Getting error on AuthenticationParameters and AcquireToken

I am getting following errors:
Code:
string redirectUrl = ConfigurationManager.AppSettings["RedirectUrl"];
AuthenticationParameters ap = AuthenticationParameters.CreateFromResourceUrlAsync(new Uri(redirectUrl)).Result;
Error:
An object reference is required for the non-static field, method, or property 'AuthenticationParameters.CreateFromResourceUrlAsync(Uri)' Pending email for Authorizers C:\Users\handemv\source\Workspaces\Dynamics 365\Trunk-UCI\Tools\Automation_CapGSupport\Pending Email\Pending email for Authorizers\PendingEmailCheck.cs
Code:
AuthenticationContext authContext = new AuthenticationContext(_authority, false);
AuthenticationResult result = authContext.AcquireToken(_serviceUri, clientCred);
Error:
AuthenticationContext' does not contain a definition for 'AcquireToken' and no accessible extension method 'AcquireToken' accepting a first argument of type 'AuthenticationContext' could be found (are you missing a using directive or an assembly reference?)
Can anyone help me for same?
Error : AuthenticationContext' does not contain a definition for 'AcquireToken' and no accessible extension method 'AcquireToken' accepting a first argument of type 'AuthenticationContext' could be found (are you missing a using directive or an assembly reference?)
Solution 1:
This error due to AcquireToken was removed in v3 of the Microsoft.IdentityModel.Clients.ActiveDirectory library.
To fix this downgrade the version in nuget to 2.22.302.111727 to solve this error.
Solution 2:
In V3 of ADAL.NET which appears to no longer have AcquireToken().
But it still has AcquireTokenAsync(). Note however that the parameters have slightly changed in the methods for v2 and v3.
For more details refer this thread
Error: An object reference is required for the non-static field, method, or property 'AuthenticationParameters.CreateFromResourceUrlAsync(Uri)' Pending email for Authorizers
Follow this link :https://github.com/AzureAD/azure-activedirectory-library-for-dotnet/issues/1410

CompilationFailedException during runtime compilation of Razor from AspNetCore TestHost

I have ASP.Net MVC application, one part of it is compiling razor views to string. The code is very similar to this example:
https://long2know.com/2017/08/rendering-and-emailing-embedded-razor-views-with-net-core/
I registered Razor engine in Startup.cs in this way:
var viewAssembly = typeof(HtmlGeneratorService).GetTypeInfo().Assembly;
var fileProvider = new EmbeddedFileProvider(
viewAssembly,
"ApplicationServices.Widgets.Html.Templates");
services.Configure<MvcRazorRuntimeCompilationOptions>(options => {
options.FileProviders.Clear();
options.FileProviders.Add(fileProvider);
});
services.AddRazorPages().AddRazorRuntimeCompilation();
In test project i have this setup:
var builder = new HostBuilder()
.ConfigureWebHost(webHost =>
{
webHost.UseTestServer();
webHost.UseStartup<Startup>();
});
var host = await builder.StartAsync();
HttpClient = host.GetTestClient();
But when i call my endpoint using this HttpClient, IRazorViewEngine.GetView starts to throw strange exceptions:
Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.CompilationFailedException: 'One or more compilation failures occurred:
rnouw0xu.21w(4,41): error CS0234: The type or namespace name 'Razor' does not exist in the namespace 'Microsoft.AspNetCore' (are you missing an assembly reference?)
rnouw0xu.21w(4,82): error CS0518: Predefined type 'System.Type' is not defined or imported
rnouw0xu.21w(4,110): error CS0518: Predefined type 'System.String' is not defined or imported
rnouw0xu.21w(4,127): error CS0518: Predefined type 'System.String' is not defined or imported
rnouw0xu.21w(8,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
rnouw0xu.21w(9,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
rnouw0xu.21w(10,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
rnouw0xu.21w(11,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
rnouw0xu.21w(13,36): error CS0234: The type or namespace name 'Rendering' does not exist in the namespace 'Microsoft.AspNetCore.Mvc' (are you missing an assembly reference?)
rnouw0xu.21w(14,36): error CS0234: The type or namespace name 'ViewFeatures' does not exist in the namespace 'Microsoft.AspNetCore.Mvc' (are you missing an assembly reference?)
rnouw0xu.21w(29,35): error CS0234: The type or namespace name 'Razor' does not exist in the namespace 'Microsoft.AspNetCore' (are you missing an assembly reference?)
rnouw0xu.21w(29,78): error CS0518: Predefined type 'System.String' is not defined or imported
rnouw0xu.21w(29,87): error CS0518: Predefined type 'System.String' is not defined or imported
I trided to fix this erros in many different ways bul it looks like I got stuck here.
Looks like i found answer in this article:
https://github.com/aspnet/Razor/issues/1212
I just added this code to my test.csproj file:
<Target Name="CopyDepsFiles" AfterTargets="Build" Condition="'$(TargetFramework)'!=''">
<ItemGroup>
<DepsFilePaths Include="$([System.IO.Path]::ChangeExtension('%(_ResolvedProjectReferencePaths.FullPath)', '.deps.json'))" />
</ItemGroup>
<Copy SourceFiles="%(DepsFilePaths.FullPath)" DestinationFolder="$(OutputPath)" Condition="Exists('%(DepsFilePaths.FullPath)')" />
</Target>
Just extending the approved answer a bit since the suggested fix didn't work for me when upgrading to .NET 6, runtime was still unable to locate the assemblies until I explicitly added a AddApplicationPart(assembly) call to the IMvcBuilder.
Also a minor simplification is to pass the options lambda to the AddRazordRuntimeCompilation call, in which case services.Configure<MvcRazorRuntimeCompilationOptions> can be removed.
It might have failed due to several reasons (this was a Windows service project, views were in a separate dll), but anyway this is a compilation of various fixes I gathered from the web:
// this part is needed if you don't have a valid IWebHostEnvironment
// service registered, in which case you need to create your own dummy
// implementation because Razor needs IWebHostEnvironment.ApplicationName
// (it should return Assembly.GetEntryAssembly().GetName().Name, and you
// can leave other properties null)
services.AddSingleton<IWebHostEnvironment, DummyHostingEnvironment>();
services.AddSingleton<IHostEnvironment, DummyHostingEnvironment>();
// this also needs to be registered as two separate dependencies
// if you're getting "Unable to resolve service for type
// 'System.Diagnostics.DiagnosticListener'"
// (see https://github.com/dotnet/aspnetcore/issues/14544)
var diagnosticSource = new DiagnosticListener("Microsoft.AspNetCore");
services.AddSingleton<DiagnosticSource>(diagnosticSource);
services.AddSingleton<DiagnosticListener>(diagnosticSource);
// make sure you specify correct assemblies, in case the views
// are in a separate dll
var assemblyWithTemplates = ...;
var assemblyReferencingTheRazorPackage = ...;
services
.AddRazorPages()
.AddApplicationPart(assemblyReferencingTheRazorPackage ) // <-- added this
.AddRazorRuntimeCompilation(options =>
{
// important to clear because some of them are null
options.FileProviders.Clear();
// resolve views as embedded resources
options.FileProviders.Add(new EmbeddedFileProvider(assemblyWithTemplates));
});
And then also add the code from the accepted answer to the .csproj file so that .deps files are properly copied.

accessing files from a difference namespace c#

I have created a project structure like below
-Project.sln
--Provider.csproj
---Middleware
----Helper.cs
---Tests.csproj
----test1.cs
I am trying to access Helper.cs from test1.cs like using Provider.Middleware, but build is failing with message:
The type or namespace name 'Middleware' does not exist in the namespace 'Provider'.
Am I missing something?
Are you sure that you have Middleware as a folder in your project and something like this in your Helper class:
namespace Provider.Middleware{
…
}

ASP.NET vNext DataAnnotations DatabaseGenerated not working

I'm trying define a model ID variable like this in ASP.NET 5:
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
To support data annotations I've added the package System.ComponentModel.DataAnnotations in my project.json file like this:
"System.ComponentModel.Annotations": "4.0.10-beta-22811"
And in the model cs file I've added using System.ComponentModel.DataAnnotations.Schema;
Though I get the following error:
Error CS0433 The type 'DatabaseGeneratedAttribute' exists in both 'System.ComponentModel.Annotations, Version=4.0.10.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' and 'System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
And I don't know how I should fix this really. I've tried to include the namespace System.ComponentModel.Annotations instead of System.ComponentModel.DataAnnotations but it seems like it doesn't exist as I get this error then:
Error CS0234 The type or namespace name 'Annotations' does not exist in the namespace 'System.ComponentModel' (are you missing an assembly reference?)
And if that namespace does not exist I don't understand how I can get the previous error which tells me that DatabaseGeneratedAttribute exists in two places.
I would really appreciate all help I can get with this.
You can just use the KeyAttribute. That should do the auto generation for you.
[Key]
public int Id { get; set; }
This attribute is available in the System.ComponentModel.DataAnnotations namespace
However, if you want to continue using the DatabaseGeneratedAttribute. The error is pretty self explanatory. It tells you that it is available in both namespaces
System.ComponentModel.DataAnnotations
System.ComponentModel.DataAnnotations.Schema
You will need to explicity state the namespace you need to use E.g.
[System.ComponentModel.DataAnnotations.Schema.DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
You can always use an alias to keep the namespace short and sweet.
Please check that your project does not have reference to both version of System.ComponentModel.DataAnnotations.dll assembly.
Old version (4.0.0.0) can be included to your project by default, and do not be removed after you install package with new version (4.0.10.0).

How can I access a Masterpage property from a base class or other functions file

I'm accessing Masterpage properties from a regular ASP.NET C# page by doing the following:
((SecondMasterPage)(Page.Master)).speciallink = true;
((SecondMasterPage)(Page.Master)).specialspan = false;
That works fine in a page's code-behind, but when I try to move it to a function in my base class file like this:
public class MyBaseClass : System.Web.UI.Page
{
public void portalFuncs()
{
((SecondMasterPage)(Page.Master)).speciallink = true;
((SecondMasterPage)(Page.Master)).specialspan = false;
}
}
... I get the following error message:
Compiler Error Message: CS0246: The type or namespace name 'SecondMasterPage' could not be found (are you missing a using directive or an assembly reference?)
My base class file is in my App_Code directory and other functions in there work without errors. Why won't this function work? Also if a function like this particular one won't work in my base class file, where could I put it so it would be available to be called from any page?
Unless I'm missing something, you should just be able to go into the codebehind for your SecondMasterPage file, and check the namespace of it. Perhaps the namespace is incorrect or something different than you want.
In your base class, use a using my.masterpage.namespace; statement to get it to work.

Categories