MEF - Export Module not loaded - c#

Following MEF Export will not be loaded by ComposeParts(this).
using MyLib;
using System.Composition;
namespace Test
{
[Export(typeof(IExtension))]
public class Test : IExtension
{
}
}

This happens when you auto-install MEF to dotnetcore using Visual Studio Alt+Enter menu. The correct namespace is System.ComponentModel.Composition. Due to quite similarly named namespace this may be overseen.
using MyLib;
using System.ComponentModel.Composition; // <--
namespace Test
{
[Export(typeof(IExtension))]
public class Test : IExtension
{
}
}

Related

dotnet test command not finding any test to execute in nUnit

I am using 3.12.0 version of nunit and 3.15.1 version of nunit test adapter.
I have created a project in .net and added a simple code in class to run tests.
From Test->Windows->Test Explorer, I am able to view and run test cases but when I try to run from command line, It is not running anything and not giving any error also.
I am not sure what I am missing. Can anyone suggest what could be the possible reason for this?
screenshot
My code looks like this
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SpecFlow.API.Test
{
public class Class1
{
[SetUp]
public void setupclass()
{
// Console.ReadLine();
}
[Test]
public void setuptest()
{
Assert.Fail("ERROR");
Console.ReadLine();
}
[TearDown]
public void tearDown()
{
Assert.Fail("ERROR");
}
}
}
```
It seems that you are missing the TestFixture attribute
using System;
using NUnit.Framework;
namespace NUnit.Tests
{
// Add TestFixture attribute
[TestFixture]
public class SuccessTests
{
// ...
}
}

Visual Studio 2015 intellisense and ambiguous namespaces

The Xamarin.Auth project broke up i.e. the source code was compiling OK and after another Xamarin.Android update it was suddenly broken.
It turned out that the problem arose after the Mono.Android.dll (Xamarin.Android) was extended by several namespace definitions.
I hunted the problem down to the following:
There is a dll #1 (Mono.Android.dll):
namespace Xamarin.Android
{
public class Class1
{
}
}
namespace Android.OS
{
public class Class2
{
}
}
There is a dll #2 (Xamarin.Auth.Android.dll):
namespace Xamarin.Auth
{
//This does not compile. See the problem description below.
public class User1 : Android.OS.Class2
{
}
}
Intellisense shows the following problem:
Error CS0234 The type or namespace name 'OS' does not exist in the namespace 'Xamarin.Android' (are you missing an assembly reference?)
This can be fixed by changing the latter namespace to something else or by using the global:: identifier:
namespace SomeOtherNamespace
{
//This compiles ok.
public class User1 : Android.OS.Class2
{
}
}
namespace Xamarin.Auth
{
//This compiles ok.
public class User1 : global::Android.OS.Class2
{
}
}
The question is: why does not intellisense give a warning that the Android namespace branch is ambiguous between global::Xamarin.Android and global::Android? What is the good way out of it? Always use global:: namespace identifier?
You can use the global directive to tell the compiler it should evaluate the namespace from the root, else it tries to evaluate the namespace relative from the current namespace. Using global always uses the full qualified namespace name, so that is why it works when you prefix it.
Another option would be using an alias:
using AOS = Android.OS;
namespace Xamarin
{
public class User1 : AOS.Class2
{
}
}

TestContext is null when running test from a Windows Form

I have an issue related to running CodedUITests from a Windows Form. (it works in the CodedUITestProject with Test-Explorer).
This is my CodedUITest's structure :
[CodedUITest]
public class DBTest
{
#region Static Fields
public static CSVReader csvReader;
#endregion
#region Fields
public string logFileName;
public string timer = String.Empty;
public TestRead testRead;
public UploadResults uploadResults;
private DateTime testStart;
#endregion
[ClassInitialize]
public static void MyTestInitialize(TestContext test)
{
csvReader = new CSVReader();
csvReader.LoadTestValues("steam.csv");
}
[TestInitialize]
public void testInit()
{
testStart = DateTime.Now;
}
[TestMethod(), TestCategory("Reflection"), TestCategory("DataDriven"), DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", #"|DataDirectory|\CSV's\steam.csv", "steam#csv", DataAccessMethod.Sequential), DeploymentItem(#"..\..\CSV's\steam.csv")]
public void steamAccess()
{
testRead = new TestRead();
SteamMap a = new SteamMap();
testRead.Read(a, TestContext);
}
[TestCleanup]
public void TestCleanup()
{
uploadResults = new UploadResults();
//timer for each test ( in seconds )
double diffSecs = (DateTime.Now - testStart).TotalSeconds;
uploadResults.TestUpload(testRead.TestResults, csvReader.DataTable, diffSecs,TestContext);
}
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
private TestContext testContextInstance;
}
This runs perfectly from VS's Test Explorer, testContextInitialize variable gets initialized.
In the same Solution I've added a second project, a simple Windows Form application, added a reference to my DLL ( from References) which runs the following code :
Playback.Initialize();
DBTest a = new DBTest();
a.steamAccess();
Playback.Cleanup();
NullReferenceException occurs, my testContex is null when I run my test from outside it's assembly.
I need some help in this matter, Thanks
Edit 1:
Test Class :
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Windows.Input;
using System.Windows.Forms;
using System.Drawing;
using Microsoft.VisualStudio.TestTools.UITesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UITest.Extension;
using Keyboard = Microsoft.VisualStudio.TestTools.UITesting.Keyboard;
using System.Linq;
using System.Reflection;
using SimarpiDB.GLobal;
using SimarpiDB.UITestMaps.SteamMapClasses;
using SimarpiDB.Global;
using System.Data;
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("TestLauncher")]
namespace SimarpiDB
{
[CodedUITest]
public class.....
Edit 2:
Temporary workaround until I find the cause of this error :
From my Windows Form I launch a MSTest with few required parameters and most of the what-methods-to-run logic will come from my database. I already did this few months ago but I scraped it because it's an additional performance overhead to use a tool such as MsTest within a launcher such as mine.
For anyone interested, there's a file located within VS's installation directory, it's called VsDevCmd.bat, I load this .bat within a hidden-in-background cmd with few additional commands ( mstest, testcontainer, test). This works but as I said I have no other plausible ideas.
There may also be a lack of referenced libraries within my Form ? Maybe something, a .dll that initialized the testenvironment and the testContext variable.
I wrote this because there may be others seeking the same result.
To clarify on my comment: internal is default in C#, so declaring something as abstract is like declaring it as "internal abstract".
For InternalsVisibleTo, you must make the library project visible to the test project, not the other way around:
[assembly:InternalsVisibleTo("MyTestProject")]

interface with System.IO Object cannot be completed in Xamarin.iOS project

I'm trying to make an interface like this
using System;
using System.IO;
using System.Text;
using System.Threading;
using Xamarin.Forms;
namespace Foo
{
public interface IStreamProvider
{
Stream OutputStream { get; set; }
}
}
and implement it in the iOS project like this:
using System;
using System.IO;
using Foundation;
using Foo;
[assembly: Xamarin.Forms.Dependency(typeof(StreamProvider_iOS))]
namespace Foo.iOS
{
public class StreamProvider_iOS : IStreamProvider
{
public Stream OutputStream { get; set; }
}
}
but I end up getting this error:
'StreamProvider_iOS ' does not implement interface member
'IStreamProvider.OutputStream'. 'StreamProvider_iOS.OutputStream'
cannot implement 'IStreamProvider.OutputStream' because it does not
have the matching return type of 'Stream'.
I'm thinking it's because System.IO in the base xamarin project isn't the same as System.IO in the Xamarin.iOS project, but I'm not really sure and don't know how to work around that if it's the case.

C# - MEF - Importing from a Portable Class Library

I can't seem to be able to load a class exported from a PCL DLL in a regular Windows DLL.
I am using the Nuget Package: http://www.nuget.org/packages/Microsoft.Composition/ (Microsoft Composition (MEF 2) 1.0.27)
Passing Code (inside a regular DLL):
using System.ComponentModel.Composition;
namespace Normal
{
[Export]
public class TestExport
{
}
}
Failing Code (inside a PCL DLL):
using System.Composition;
namespace PCL
{
[Export]
public class TestExport
{
}
}
Unit Test (Regular Unit Test Project):
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PCL;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
namespace UnitTest
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
AggregateCatalog catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(typeof(PCL.TestExport).Assembly));
catalog.Catalogs.Add(new AssemblyCatalog(typeof(Normal.TestExport).Assembly));
var container = new CompositionContainer(catalog, CompositionOptions.DisableSilentRejection);
CompositionBatch batch = new CompositionBatch();
batch.AddExportedValue(container);
container.ComposeParts(batch);
//Passes
PCL.TestExport constructed = new PCL.TestExport();
Normal.TestExport constructed2 = new Normal.TestExport();
//Passes
Normal.TestExport passes = container.GetExportedValue<Normal.TestExport>();
//Fails
PCL.TestExport e = container.GetExportedValue<PCL.TestExport>();
}
}
}
Your regular DLL and unit test project are using System.ComponentModel.Composition, which is "MEF 1". MEF 1 doesn't know anything about the System.Composition attributes (MEF 2).
If you can use MEF 2 for all your projects, it should work.

Categories

Resources