MEF giving CompositionException - mef

I am new to MEF and I am trying this following program.
The class library whose dll I am making has following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.Composition;
namespace MefTestsCore
{
[Export (typeof(MefTestsCore.IFace))]
public class CoreClass1 : IFace
{
public String getName()
{
return "CoreClass1";
}
}
interface IFace
{
String getName();
}
}
The program which wants to access the dll has following code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using MefTestsCore;
namespace MefTests
{
class Program
{
[Import (typeof(MefTestsCore.IFace), AllowRecomposition=true)]
public IFace iFaceObj;
static void Main(string[] args)
{
Program p = new Program();
p.Method();
}
public void Method()
{
DirectoryCatalog catalog = new DirectoryCatalog(#"C:\Users\username");
CompositionContainer container = new CompositionContainer(catalog);
container.ComposeParts(this);
Console.WriteLine(iFaceObj.getName());
Console.ReadKey();
}
}
}
I have the MEFLibrary.dll (dll containing MefTestsCore namespace, the first part of above code) at C:\Users\username. When I run the program, it throws CompositionException with following details
The composition produced a single composition error.
The root cause is provided below. Review the CompositionException.Errors property for more detailed information.
1) The export 'MefTestsCore.CoreClass1 (ContractName="MefTestsCore.IFace")' is not assignable to type 'MefTestsCore.IFace'.
Resulting in: Cannot set import 'MefTests.Program.iFaceObj (ContractName="MefTestsCore.IFace")' on part 'MefTests.Program'.
Element: MefTests.Program.iFaceObj (ContractName="MefTestsCore.IFace") --> MefTests.Program

In your test program you are using MefTestsCore. So I assume you referenced that assembly from your test program. But then you also load a copy from c:\Users\username. Now they types don't match because they come from different assemblies.
You have to put the interface into it's own assembly so that you then can reference it from your program and from MefTestsCore.

Related

No webpage was found for the web address: https://localhost:5001/

This localhost page can’t be foundNo webpage was found for the web address: https://localhost:5001/
HTTP ERROR 404
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
namespace CMS.WEBAPI.Controllers
{
[ApiController]
[Route("controller")]
public class CoursesController: ControllerBase
{
public CoursesController()
{
}
[HttpGet]
public string GetCourses()
{
return "hello world";
}
}
}
--
getting error as pagenot found
by changing the route attribute - working fine.
[Route("controller")]

Accessing struct list on Start() gives NullReferenceError

What in the name of god am I doing wrong here? Every time I run the game I get this: "NullReferenceException: Object reference not set to an instance of an object". I know what that means I just don't get why it is saying that?
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
public class Inventory : MonoBehaviour {
public static Inventory instance;
public List<InventoryItems> INVENTORY_ITEMS = new List<InventoryItems>();
void Awake(){
instance = this;
}
void Start(){
Debug.Log(instance.INVENTORY_ITEMS); // ERROR
Debug.Log(INVENTORY_ITEMS); // ERROR
}
}
[Serializable]
public struct InventoryItems
{
public string name;
}
This is how you should write this code:
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
public class Inventory : MonoBehaviour {
public static Inventory instance;
public List<InventoryItems> INVENTORY_ITEMS;//do not initialize here
void Awake(){
instance = this;
INVENTORY_ITEMS = new List<InventoryItems>();//init here instead
}
void Start(){
Debug.Log(instance.INVENTORY_ITEMS); //no ERROR
Debug.Log(INVENTORY_ITEMS); //no ERROR
}
}
[Serializable]
public struct InventoryItems
{
public string name;
}
Added note:
The reason for this error is that the value of a public serializable member in monobehaviour is being read from the editor (inspector) overwriting any value being assigned to it prior to Awake.
I suggest either change the list accessor to internal, or make it a property, or keep it public but let inspector handle the initialization and adding the initial elements.
Added another note:
According to Programmer and Uri Popov, your original code most certainly will work on unity 5.4 and later.

Generated moles type for a static internal class is not accessible in test class

I am trying to get access to an internal static class to override some of its methods so that I can test classes that depend on that class
From what I read that should be possible but I am not obviously not understanding everything as even a simple example seems to fail to generate a mole type for the internal static class.
I have two classes in a namespace and assembly
namespace SimpleClassToTest
{
public class Class1
{
public string SayOla() { return Class2.ReturnMe("Ola"); }
}
}
namespace SimpleClassToTest
{
internal static class Class2
{
static public string ReturnMe(string m)
{
return m;
}
}
}
In AssemblyInfo.cs I also have
[assembly: InternalsVisibleTo("SimpleClassToTest")]
[assembly: InternalsVisibleTo("SimpleClassToTest.Moles")]
In the test project I have a single test class
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SimpleClassToTest;
using SimpleClassToTest.Moles;
namespace SimpleClassToTest.Moles
{
[TestClass]
public class UnitTest1
{
public UnitTest1()
{
}
[TestMethod]
[HostType("Moles")]
public void TestMethod1()
{
Class1 c1 = new Class1();
Assert.AreEqual(c1.SayOla(), "Ola");
MClass2.ReturnMeString = (ignored) => { return "ReturnMe"; };
Assert.AreEqual(c1.SayOla(), "ReturnMe");
}
}
}
Unfortunately that is not compiling. The error is
UnitTest1.cs(25,13): error CS0122: 'SimpleClassToTest.Moles.MClass2' is inaccessible due to its protection level
Any tips to get this going would certainly be appreciated!
Thanks!
Peter
PS Tried this both on VS2008 and on VS2010 with moles version Microsoft Moles v0.94.51023.0
(Edit : From Comment Below)
As a workaround, in the unit test:
Type mClass2Type = typeof(SimpleClassToTest.Moles.MClass1)
.Assembly.GetType("SimpleClassToTest.Mole‌​s.MClass2");
PropertyInfo returnMeProp = mClass2Type.GetProperty("ReturnMeString");
Microsoft.Moles.Framework.MolesDelegates.Func<String, String> molesDelegate =
(ignore) => { return "ReturnMe"; };
returnMeProp.SetValue(mClass2Type, molesDelegate, null);
Assert.AreEqual(c1.SayOla(), "ReturnMe");
If your assembly under test is strongly signed, then the moles assembly is also strongly signed, and you must specify the public key in the InternalsVisibleTo attribute. See the section "Code Generation and Compilation" of the moles reference manual for details.
Here is a quote that might be relevant:
... use this snippet as a
starting point to add InternalsVisibleTo attribute to your project.
[assembly: InternalsVisibleTo(“FileSystem.Moles, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e92decb949446f688ab9f6973436c535bf50acd1fd580495aae3f875aa4e4f663ca77908c63b7f0996977cb98fcfdb35e05aa2c842002703cad835473caac5ef14107e3a7fae01120a96558785f48319f66daabc862872b2c53f5ac11fa335c0165e202b4c011334c7bc8f4c4e570cf255190f4e3e2cbc9137ca57cb687947bc”)]

Use repository in referenced dll using entity framework

I have created a DLL that contains lots of authentication and user management that I'm trying to use in a separate project (MVC 3 Website).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TestProj.Authentication;
namespace TestSite.MVC.Areas.Admin.Controllers
{
public class TestController : Controller
{
AuthenticationRepository authrep = new AuthenticationRepository();
public ActionResult Index()
{
authrep.DeleteUser(1);
return View();
}
}
}
Now this obviously does'nt work, which is understandable.
Is it dependency injection I need here?
And in that case, how would the basic code look for that?
Do I need to add something in the constructor for the referenced DLL?
Try structuring your controller like this:
public class TestController : Controller
{
IAuthenticationRepository AuthenticationRepository { get;set; }
public void TestController (IuthenticationRepository authenticationRepository)
{
this.AuthenticationRepository = authenticationRepository;
}
public ActionResult Index()
{
this.AuthenticationRepository.DeleteUser(1);
return View();
}
}
Create an interface for your repository. You could then use a DI framework (like Ninject for MVC 3) to inject instances of AuthenticationRepository into usages of IAuthenticationRepository.
https://github.com/ninject/ninject/wiki

Error when trying to populate listbox

Hi I am a student I am getting this error:
forreach statement cannot operate on
variables of type 'object' because
'object' does not contain a public
definition for 'GetEnumerator'
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string result = " ";
foreach (string activity in listBox1.SelectedItems)
{
result += activity + " ";
}
this.textBox1.Text = result;
}
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Add(textBox2.Text);
textBox2.Clear();
}
}
}
Pl. help me
Your code works fine for me. However, your code will break if you've added something other than a string to your ListBox's Items collection (although this will produce a different error than what you're apparently seeing).
Can you add the code you use to populate this ListBox?