XUnit 'Startup' does not exist - asp.net-core

In my xunit project i am getting bellow error
The type or namespace name 'Startup' does not exist in the namespace 'TestService' (are you missing an assembly reference?) [Testervice.Tests]
I added the TestService reference to the Testervice.Tests by using
dotnet add reference ..\TestService\TestService.csproj
The reference was added successfully the the test project and its also available in TestService.csproj file. But still i am getting the error. Why i am getting the error.Any one try help me.

I am not sure this will solve your problem but what you need to is create a TestStartUp class which inherits the original Startup.cs to properly build your .net core app. Sample code is shown below.
public class TestStartup : Startup
{
public TestStartup(IConfiguration configuration, IHostingEnvironment hostingEnvironment) : base(configuration, hostingEnvironment)
{
}
}
Hope this solves your problem.

Related

Maui App not working with Android Simulator namespace missing

I installed Visual Studio 2022 Preview Getting error when running Maui App using Android or Machine Simulator. Is anyone having the same issue?
Already installed
Single-project MSIX Packaging
Android device Manger
Namespace missing if add the namespace still not working
error "Severity Code Description Project File Line Suppression State
Error CS0246 The type or namespace name 'MauiApp' could not be found (are you missing a using directive or an assembly reference?) MauiApp37 (net6.0-android)"
Missing namespace if I include it still not working "using Microsoft.Maui.Hosting;"
Second Error
Severity Code Description Project File Line Suppression State
Error NU1605 Detected package downgrade: Microsoft.WindowsAppSDK from 1.0.0-preview3 to 1.0.0-experimental1. Reference the package directly from the project to select a different version.
MauiApp37 -> Microsoft.AspNetCore.Components.WebView.Maui 6.0.101-preview.10.2068 -> Microsoft.WindowsAppSDK (>= 1.0.0-preview3)
MauiApp37 -> Microsoft.WindowsAppSDK (>= 1.0.0-experimental1) MauiApp37
Your main class is MainApplication of type MauiApplication
That means that the template that you used to create that project is old (I think it was preview 6 or 7)
If you create a new .NET MAUI App with a newer template, you will see that the main class is something like this. And it's the only main file
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
});
return builder.Build();
}
}
So I would recommend creating a new project using a new template, and then migrate the rest of the classes/views (use the same name for your project to make it easier)

Can not compile xUnit test classes (via mono) to exe file, using mcs command

I am using MSYS2 and mono in order to run C# project (in .NET Framework) in Linux environment on Windows machine.
My projects are placed in the path: D:\MyRootPath and there I have two separate solutions:
D:\MyRootPath\MainProject - this is my main project;
D:\MyRootPath\xUnitProject- this is a separate solution that contains project that I use for unit testing with xUnit.net framework (for .NET Framework);
Building main project (working sample)
Inside of D:\MyRootPath\MainProject I have a sample class D:\MyRootPath\MainProject\HelloWorld.cs:
public class HelloWorld
{
public static void Main(string[] args)
{
Console.WriteLine ("Hello Mono World");
}
}
When I want to build this via mono, to create an .exe file and run it, I am using the following commands inside of the MSYS2 console (this approach is working well):
mcs /d/MyRootPath/MainProject/HelloWorld.cs
mono /d/MyRootPath/MainProject/HelloWorld.exe
Building xunit project (need help here)
Inside of the xUnitProject, I have many classes, where each class represent a test case (and each test case have reference to the main project - MainProject.dll). One example of test case is D:\MyRootPath\xUnitProject\SampleRectangle.cs:
using MainProject;
using System;
using Xunit;
namespace xUnitProject
{
public class SimpleRectangle
{
[Fact]
public void simpleRectangle()
{
//this content is irrelevant to the question, I have a few Assert checks
Assert.Equal("someVariable", someVariable);
Assert.Equal("someOtherVarialble", someOtherVarialble);
}
}
}
Here I need to build this via mono, to create an .exe file and run it. Since I have many classes like SimpleRectangle, I need to run them all and to have separate .exe files for each.
So I am trying to build the test classes with the following (similar as above working example):
mcs /d/MyRootPath/xUnitProject/SampleRectangle.cs -r:/d/MyRootPath/xUnitProject/bin/Debug/MainProject.dll:/d/MyRootPath/xUnitProject/bin/Debug/xunit.core.dll
where I had to add -r flag, since it points to the .dll files that are used.
The output that I got is:
D:/MyRootPath/xUnitProject/SimpleRectangle.cs: error CS0012: The type `System.Attribute' is defined in an assembly that is not referenced. Consider adding a reference to assembly `System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
D:\MyRootPath\xUnitProject\bin\Debug\xunit.core.dll (Location of the symbol related to previous error)
D:/MyRootPath/xUnitProject/SimpleRectangle.cs: error CS0103: The name `Assert' does not exist in the current context
D:/MyRootPath/xUnitProject/SimpleRectangle.cs: error CS0103: The name `Assert' does not exist in the current context
error CS5001: Program `SimpleRectangle.exe' does not contain a static `Main' method suitable for an entry point
Compilation failed: 4 error(s), 0 warnings

Unity WCF config

Hello i am trying to implement the Unity dependency pattern into my wcf project. I have installed the required packages and am trying to setup the registeredtypes into the container. I am strictly following the examples on the codeplex website but i keep getting "WcfServiceFactory does not implement inherited abstract member ConfigureContainer." Which is kinda strange since im overriding it in the servicefactory. I had a look in the example project but i cant seem to find what im doing wrong. Please help!
namespace WCFService
{
public class WcfServiceFactory : UnityServiceHostFactory
{
protected override void ConfigureContainer(IUnityContainer container)
{
// register all your components with the container here
container
.RegisterType<IPersonRepository, PersonRepository>();
}
}
}
Well in the end it seems it was an issue with the packages installed. After removing all packages and reinstalling them there was no longer an issue... :S

Unity.MVC4 Doesn't Dispose()

I've installed the NuGet Unity.MVC4 package http://nuget.org/packages/Unity.Mvc4/ and have hooked it up the same as I would if I were using the Unity.MVC3 package.
My dependencies are getting resolved correctly, but when using the HierarchicalLifetimeManager, my object's Dispose() method is not called at the end of the request.
I've actually set up a simple project using Unity.MVC3 and Unity.MVC4, with the exact same Bootstrapper.cs code in both projects. On the MVC3 version, Dispose() is called; on the MVC4 version, it is not.
I'm registering my type like so
container.RegisterType<ITest, Test>(new HierarchicalLifetimeManager());
with a very simple Test class
public interface ITest
{
void Foo();
}
public class Test : IDisposable, ITest
{
public Test()
{
}
public void Foo()
{
}
public void Dispose()
{
}
}
I don't think I'm doing anything incorrectly, but there appears to be some bug in the Unity.MVC4 package.
If anyone is successfully using that package, I'm curious how you got it to work.
The best solution to this issue is to upgrade to Unity3 and install the Unity Bootstrapper for ASP.NET MVC package. The Bootstrapper package includes PerRequestLifetimeManager which should be used in place of HierarchicalLifetimeManager. Once the integration is complete, Dispose() is called at the end of every HTTP request just as expected.
Note that the newest Unity documentation suggests that PerRequestLifetimeManager should not be used at all unless absolutely necessary. (I believe, however, that it is still needed in some cases - for example, where you want to share a DbContext across all objects used during a single request.)
With this solution, Unity.MVC4 is no longer needed and can be uninstalled.
I have had a look at the code. We simply ported the Unity.MVC3 code from codeplex (http://unitymvc3.codeplex.com/) and re-bundled it up for MVC4.
We have not made any alterations to the code.
Unity.MVC4 makes use of the Unity package managed by Microsoft (http://nuget.org/packages/Unity/). The majority of the code is in Unity.
Are you running different Dot.Net versions? i.e 4.0 vs 4.5? There seems to be a pre-release Unity for DotNet 4.5.

Getting Fluent NHibernate to work with SQLite

I'm sure there is something simple I've not done but I'm trying to get Fluent NHibernate to work with Sqlite on my machine.
I used NuGet to download fluent nhibernate and added the following entity and mapping:
public class Customer
{
public virtual string CustomerCode { get; set; }
public virtual string Name { get; set; }
}
public class CustomerMap : ClassMap<Customer>
{
public CustomerMap ()
{
Id(x => x.CustomerCode);
Map(x => x.Name);
Table("tblCustomer");
}
}
Then following the getting started with fluent guide I added the following code to a Windows Command project:
class Program
{
static void Main(string[] args)
{
var sessionFactory = CreateSessionFactory();
using (var session = sessionFactory.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
var customer = new Customer { CustomerCode = "123", Name = "Bob" };
session.SaveOrUpdate(customer);
transaction.Commit();
}
}
}
private static ISessionFactory CreateSessionFactory()
{
return Fluently.Configure()
.Database(
SQLiteConfiguration.Standard
.UsingFile("firstProject.db")
)
.Mappings(m =>
m.FluentMappings.AddFromAssemblyOf<Program>())
.ExposeConfiguration(BuildSchema)
.BuildSessionFactory();
}
private static void BuildSchema(Configuration config)
{
// delete the existing db on each run
if (File.Exists("firstProject.db"))
File.Delete("firstProject.db");
// this NHibernate tool takes a configuration (with mapping info in)
// and exports a database schema from it
new SchemaExport(config)
.Create(false, true);
}
}
Finally I added the Sqlite dll using NuGet.. however I'm getting the following error when trying to run the program:
Top Exception:
An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.
Next Exception:
Could not create the driver from NHibernate.Driver.SQLite20Driver, NHibernate, Version=3.1.0.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4.
Inner most exception:
Unable to find the requested .Net Framework Data Provider. It may not be installed.
This is when it's trying to create the session factory.
Can anyone help with this? I'm running a 32 bit machine?
Thanks
Dave
You need two things:
Reference System.Data.SQLite in your project.
Include sqlite3.dll, but you also can’t add reference to sqlite3.dll, because it’s an unmanaged dll. Simply add it as element to solution and set it to copy to output directory.
You need the .NET provider for Sqlite. Download it here http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki
After initial investigation I thought it was because my System.Data.SQLite assembly wasn't loaded into memory at the time, so I included code to preload the system.Data.SQLite assembly. However when running the application the real error arose:
Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.
to fix this I changed my app.config to look like the following:
<?xml version="1.0"?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
with the important bit being useLegacyV2RuntimeActivationPolicy="true"
I was having the same problem today, and spend an hour searching for a solution. Anyway, I found one here:
http://thelongwayback.wordpress.com/2009/11/02/fluent-nhibernate-sqlite-problem-the-idbcommand-and-idbconnection-implementation-in-the-assembly-system-data-sqlite-could-not-be-found/
Basically, you'll have to use an older version of SQLite (v1.0.60) from here:
http://sourceforge.net/projects/sqlite-dotnet2/files/
On another note, I got the same code working yesterday on a machine with VS2010 SP1. The same code wouldn't run today on a machine without SP1. If anyone gets a chance to test this one, i.e., by installing VS2010 SP1, let me know the outcome please.
None of the above solutions, or any else I could find on the Internet, worked for me... until...
I had to install both the x64 and x86 versions of SQLite (per Vadim's link: http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki), specifically in that order.
Initially, I installed the 32-bit before the 64-bit version, which did not fix anything. On a whim, I uninstalled them both and reinstalled them in the opposite order. Now it works!
I've tested this on 2 different machines and verified this was the fix for both of them. Wacky, but effective.
I also encountered this problem.
Noticed that the starter sample project targets .net framework 3.5 and the one I created (exercise project) is targeting .net framework 4.0 client profile (default of vs2010).
I changed the target version to 3.5 and I was able to work on my exercise project.
In my case it worked to install the System.Data.SqLite via NuGet.
So I opened the "Tools/NuGet Package Manager/Package Manager console" and typed:
Install-Package System.Data.SqLite