Using AssembliesFromPath from StructureMap throws MissingMethodException - c#

I am trying to scan all assemblies in a subfolder of my main project and then add the contained registry to my main registry in order to override default registry entries, where necessary and intercept types for dependency injection.
For this I create the following registry
public PluginRegistryAdder(string pluginPath)
{
Scan(x =>
{
x.AssembliesFromPath(pluginPath);
x.LookForRegistries();
});
}
which I plan to add to my main container like this:
var pluginRegistries = new PluginRegistryAdder(pluginPath);
Container.Configure(_ => _.IncludeRegistry(pluginRegistries));
The problem I am now facing is that I get this error, when using AssembliesFromPath and I don't know how to fix it:
System.MissingMethodException: Method not found: 'Void StructureMap.Graph.IAssemblyScanner.AssembliesFromPath(System.String)'.
Googling suggests that I should use the namespace StructureMap.Graph, but this did not solve the problem. Here are my using statements, of which only StructureMap is not greyed out (e.g. marked as not redundant) by Visual Studio (I was trying to find the missing reference, but nothing helped):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StructureMap;
using StructureMap.Graph;
using StructureMap.Configuration.DSL;
using StructureMap.Configuration;
using StructureMap.Graph.Scanning;
using StructureMap.Util;
Any suggestions, what I should try?!
UPDATES:
Note that when I comment out just the line with AssembliesFromPath, solution runs just fine, although LookForRegistries is definded in the same class as AssembliesFromPath.
AssembliesFromApplicationBaseDirectory gives me the same problem.

This problem seems to have been caused by a version mismatch between my DI project and my main project. After some trying I ended up reinstalling the newest version of StructureMap in both projects and the problem went away.

Related

ASP.NET Identity - HttpContext has no extension method for GetOwinContext

I have downloaded, and successfully ran the ASP.NET Identity sample from here:
https://github.com/rustd/AspnetIdentitySample
I am now in the middle of implementing the ASP.NET Identity framework in my project and have ran into a problem, that has driven me mad all day...
GetOwinContext() does not exist as an extension method on my HttpContext
I am implementing the identity framework in class library. I have installed all the latest (pre-release version) of the Identity framework and everything - apart from this - is working fine.
I have tried implementing the same code as the same direct in my controller, and find the same problem.
I'm clearly missing a reference somewhere, though I have no idea what..!..
The code-block that is killing me is:
private IAuthenticationManager AuthenticationManager
{
get
{
return HttpContext.GetOwinContext().Authentication;
}
}
I have added references to the following - tried these both in my class library and also direct on the controller, none of them work for me...
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Owin.Security;
using Microsoft.Owin;
using System.Web;
... this is driving me up the wall....any idea?
UPDATE
I have checked the versions of Identity & OWIN in the sample, and I have made sure I have the same versions in my solution.
More so, if I search the object browser on the sample for GetOwinContext I can find the method, however when I search for it in my solution it is nowhere to be found... I must have some library out of date, but I can't find it!
ARGH!
I found it... I didn't have an extra package, called Microsoft.Owin.Host.SystemWeb
Once i searched and installed this, it worked.
Now - i am not sure if i just missed everything, though found NO reference to such a library or package when going through various tutorials. It also didn't get installed when i installed all this Identity framework... Not sure if it were just me..
EDIT
Although it's in the Microsoft.Owin.Host.SystemWeb assembly it is an extension method in the System.Web namespace, so you need to have the reference to the former, and be using the latter.
I believe you need to reference the current HttpContext if you are outside of the controller. The MVC controllers have a base reference to the current context. However, outside of that, you have to explicitly declare you want the current HttpContext
return HttpContext.Current.GetOwinContext().Authentication;
As for it not showing up, a new MVC 5 project template using the code you show above (the IAuthenticationManager) has the following using statements at the top of the account controller:
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Owin.Security;
using WebApplication2.Models;
Commenting out each one, it appears the GetOwinContext() is actually a part of the System.Web.Mvc assembly.
After trial and error comparing the using statements of my controller and the Asp.Net Template controller
using System.Web;
Solved the problem for me.
You are also going to need to add:
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
To use GetUserManager method.
Microsoft couldn't find a way to resolve this automatically with right click and resolve like other missing using statements?
In my case adding Microsoft.AspNet.WebApi.Owin reference via nuget did the trick.
Make sure you installed the nuget package Microsoft.AspNet.Identity.Owin. Then add System.Net.Http namespace.
Just using
HttpContext.Current.GetOwinContext()
did the trick in my case.
For Devs getting this error in Web API Project -
The GetOwinContext extension method is defined in System.Web.Http.Owin dll and one more package will be needed i.e. Microsoft.Owin.Host.SystemWeb. This package needs to be installed in your project from nuget.
Link To Package: OWIN Package Install Command -
Install-Package Microsoft.AspNet.WebApi.Owin
Link To System.web Package : Package Install Command -
Install-Package Microsoft.Owin.Host.SystemWeb
In order to resolve this error you need to find why its occurring in your case. Please Cross check below points in your code -
You must have reference to Microsoft.AspNet.Identity.Owin;
using Microsoft.AspNet.Identity.Owin;
Define GetOwinContext() Under HttpContext.Current as below -
return _userManager1 ?? HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
OR
return _signInManager ?? HttpContext.Current.GetOwinContext().Get<ApplicationSignInManager>();
Complete Code Where GetOwinContext() is used -
public ApplicationSignInManager SignInManager
{
get
{
return _signInManager ?? HttpContext.Current.GetOwinContext().Get<ApplicationSignInManager>();
}
private set
{
_signInManager = value;
}
}
Namespace's I'm Using in Code File where GetOwinContext() Is used
using AngularJSAuthentication.API.Entities;
using AngularJSAuthentication.API.Models;
using HomeCinema.Common;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin.Security.DataProtection;
I got this error while moving my code from my one project to another.
To get UserManager in API
return HttpContext.Current.GetOwinContext().GetUserManager<AppUserManager>();
where AppUserManager is the class that inherits from UserManager.
I had all the correct packages and usings, but had to built first before I could get GetOwinContext() to work.
Just install this package and your code will work:=>
Install-Package Microsoft.Owin.Host.SystemWeb -Version 2.1.0

Windows Store Project - Assembly reference error when trying to run cmd commands from a C# file in VS2012

I'm doing some exercises with C# in the trial version of VS 2012. I want to execute a cmd command from a CS file. For this, I've tried Process.Start as well as System.Diagnostics.Process that are mentioned in these posts:
Run Command Prompt Commands
Execute CMD command from code
However, despite I added "using System.Diagnostics" and "using System.ComponentModel", I'm still getting "The type or namespace name 'Process' does not exist in the namespace 'System.Diagnostics', missing assembly reference" error. ¿Any suggestion so I can i get rid of this error? Thanks in advance.
This usually happens when you have Target framework = .NET Framework Client Profile, but DLL you reference is from .NET Framework (full). Make sure you have System.dll in your references from valid framework.
I just did the same - created empty console application with the following code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.ComponentModel;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var prc = Process.Start("explorer.exe");
}
}
}
Works perfectly fine for me.
Additional thing to check is Intellisense - when you start typing "System.Diagnostics.Proc"... - does it show you dropdown with "Process" there?
UPDATE:
Windows Store projects are based on different version of target .NET Framework - .NET for Windows Store apps, which does not support functionality you need.
For more details do web search:".NET for Windows Store apps". Helpful links:
http://msdn.microsoft.com/en-us/library/windows/apps/br230302.aspx
http://msdn.microsoft.com/en-us/library/windows/apps/br230232.aspx

ManagementObject Class not showing up in System.Management namespace

I'm trying to write some WMI in my windows form and the ManagementObject is givin me the
"The type or namespace name 'ManagementObject' could not be found" Error
Here is my un-complete code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using System.Security.Policy;
using System.Management;
using System.Management.Instrumentation;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
ManagementObject disk = new ManagementObject("Win32_LogicalDisk.DeviceID=\"C:\"");
Right-click References on the right and manually add System.Management. Even though I included it in the using statement I still had to do this. Once I did, all worked fine.
Have you added a reference to the System.Management assembly?
In Solution Explorer, right click on References, then Add Reference ... and under Framework, you should activate the System.Management framework.
You need to add a reference to System.Management.dll to your project.
You can see System.Management.Instrumentation without adding a reference to System.Management.dll because it is included in a different library (System.Core.dll, which is included as a reference automatically), but you cannot access the other types contained by that namespace without explicitly adding a reference to the System.Management.dll library.
~ just add System.management using nuget manager,
It worked for me! c#
I think the problem is there is no WMI object for Win32_LogicalDisk.DeviceID=\"C:\".
Try to replace:
ManagementObject disk = new ManagementObject("Win32_LogicalDisk.DeviceID=\"C:\"");
with:
ManagementObject disk = new ManagementObject("Win32_LogicalDisk");
and then to step through each field:
foreach (ManagementObject o in disk.Get()){
//Do what ever you need here.... For example:
Console.WriteLine(o.ToString());
}
Make sure your project isn't set up to compile against the .NET 4 Framework Client Profile.
Please see Namespace not recognized (even though it is there) for more details.
The version of Visual Studio that I have does not import ManagementObjectSearcher by importing "System.Management" namespace. If you have the same issue, try adding a reference to "System.Management.dll' by doing the following steps.
Click on project properties on solution explorer in Visual Studio.
Go to "References".
Click on "Add" to add a new reference.
Click on "Browse...".
Navigate to "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727".
Add a reference to "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Management.dll".
This is quite an old post but I just had to troubleshoot this. The only way I got it working with Visual Basic 2022 was to download and install through the NuGet Installer. Manually adding the .dll did not work for me. Once NuGet Manager is open Search: System.Management and download the latest from Microsoft, hope this helps someone.

namespace or class could not be found (ASP.NET WebSite "project")

I am currently trying to cleanup a bit of a corporate website that I inherited here. I managed to clean up most of the errors in the website but something is still up here.
I have one masterpage that have this code :
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class MasterPage : System.Web.UI.MasterPage {
public lists m_listsClass = new lists();
(no it's not a typo the S in lists).
Now in App_code I have one class lists.cs
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
/// <summary>
/// Summary description for lists
/// </summary>
public class lists
{
public lists()
{
When I try to build the website in visual studio 2008 I have this error :
Error 3 The type or namespace name 'lists' could not be found (are you missing a using directive or an assembly reference?) C:\Users\egirard\Documents\Visual Studio 2008\Projects\iFuzioncorp\iFuzioncorp\Masters\MasterPage.master.cs 23 12 iFuzioncorp
Am I missing something ?
Also I saw some strange behaviour on the server. Apparently according to IIS7 it is compiling using .net 2.0 (the pool is configured with .net 2) but there are some "using" statements that include Linq ... how is it possible to compile the page if Linq is not part of the .net 2 itself ?
Just edited with news details on the code itself. No namespace at all everywhere.
Hi There – i had a similar problem; all my namespaces and inheritance was in place. Then i then noticed that the class file’s build action was set to “Content” not “Compile” (in the properties window.
For whatever worth might there be for an answer (possibly not the right one) after many months, i think i should contribute this:
There is a case that this happens, when you place a web site inside another (ie in a subfolder).
In that case, the only App_Code folder that is legitimate is the App_code folder of the outer web site. That is, the App_Code folder right under the root of the master web site.
Maybe (say maybe) there should be no need to transform your web site to a web application, if you place the class file inside the App_code folder of the ROOT web site.
include the namespace under which lists calss is defined
or
define both the master page and lists class under the same namespace
Finally I understood quite lately that it was a website and not a web application I had to question the guys here to get it... So it's quite normal all the error I had. I haven't had the occasion to convert it first.
Make sure that in your masterpage, you have an #include statement for the namespace that the lists class is a part of (if they're in seperate namespaces, the masterpage isn't going to automatically pick up on it).
As for the strange server side behavior, .NET 2.0, 3.0, and 3.5 all run inside of .NET 2.0 app pools in IIS. It looks strange at first, but you get used to it. Here's a link with a little more in-depth explination:
The Way I See It: Where is ASP.NET 3.5 on IIS?
In order to use a type, you must reference the assembly which defines it and include the appropriate namespace.
Using only includes namespaces, if you don't use any types from said namespaces it has no effect.
is lists.cs wrapped in a namespace? if it is the case then you need to add the namespace (yournamespace.lists) or include it in masterpage. Check Also if your MasterPage is in a Namespace
Modify the Build Action of the App_Code Class to Compile.
Clean and Build the Project
This worked for me.
I had this problem myself
It wasn't working because the project I was referencing was set as a console application in its properties instead of a class library

Referencing namespaces globally?

Is there a way to reference a namespace globally across the whole solution?
So instead of having these lines in every code file:
using System;
using MyNamespace;
having to declare them only once, and every code file would use them.
Btw I am using Visual Studio.
No, C# doesn't have this concept. Each source file is independent in this respect. (And if the using directives are in a namespace declaration, those are independent from other using directives in peer namespace declarations, too. That's a pretty rare case though in my experience.)
You don't need ReSharper to change what gets included in a new class though. You can use the Visual Studio templates.
EDIT: Just to clarify the point about using directives within namespaces, suppose we had (all in one file):
using Foo;
namespace X
{
using Bar;
// Foo and Bar are searched for code in here, but not Baz
}
namespace Y
{
using Baz;
// Foo and Baz are searched for code in here, but not Bar
}
Usually I only have one namespace declaration in a file, and put all the using directives before it.
No, this is not possible.
If you're using ReSharper, you can set an option to include specific using directives in every new file you create though.
From this SO question and follow-up blog post. You can edit the Visual Studio default templates.
To do this, look at the file in this zip : [Program Files][Visual Studio]\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip
and modify the Class.cs file as needed. Additionally, Visual Studio may have cached this file here :
[Program Files][Visual Studio]\Common7\IDE\ItemTemplatesCache\CSharp\Code\1033\Class.zip
In C# 10.0 you can use Global Usings.
global using System;
global using MyNamespace;
No, you can not reference a namespace globally across the whole solution in .NET or .NET CORE.
But you can use project wise namespace globally in solution. this feature will be available from c#10/.NET 6. currently it's in preview but it will be released in NOV 2021
=========Project level .NET 6 global using namespace=========
Create a class file at root of the project e.g GlobalNamespace.cs
global using System;
global using System.Linq;
global using System.Text.RegularExpressions;
global using System.Threading.Tasks;
Then you don't need to declare using namespace in other .cs files of the project which are already declared globally.
As others have mentioned Visual Studio Templates are the way to go.
Note that simply adding a using statement to your template will not ensure that the compiler can resolve your types. So, if you are adding a using statement for MyNamespace in every class you may need to add an assembly reference to your project as well. See the C# FAQ for more information.
One trick I miss as a newb to CSharp is to look at the "refences" (in VS), to right click and "Add New Reference". This is especially handy when combining mulitple projects where I have made some generic class for reuse elsewhere.

Categories

Resources