I apologise for this - it's an idiot question and I hate myself for asking it, but I can't figure out a reason for this behaviour.
I have a namespace which contains a mixture of static and non-static classes.
namespace MyNameSpace.UI.Helpers
{
public static class OrderHelper
{ // static methods
}
}
namespace MyNameSpace.UI.Helpers
{
public class CountToCampaignConverter : IMultiValueConverter
{ // non-static methods
}
}
These being helper classes, they're used throughout the application. In this class:
using MyNameSpace.UI.Helpers;
namespace MyNameSpace.UI.ViewModels
{
public class QuickCountViewModel : BaseViewModel
{
private void BuildOrderExclusionsOnCount()
{
CurrentAvailability.OrderExclusions = CountHelper.BuildOrderExclusionAsCsv(ordersToExclude);
}
}
}
However in this class:
using MyNameSpace.UI.Helpers;
namespace MyNameSpace.Service.Services
{
public class FulfillJobs : BaseService
{ // stuff
}
}
When I try to use my helpler classes, I've got access to the non-static ones, but none of the static ones.
I can't see any naming clash here - if that were the problem, surely I wouldn't be able to get the non-static ones either?
So where else can I look to resolve the issue?
This is an old post, but no fix was discovered and this issue just happened to me. Here's my particular scenario and the resolution.
I had a Visual Studio 2019 C# solution with two projects: a .NET Standard 2.0 class library and a .NET Core 3.1 console app that used the library. Everything worked fine until I changed the library from .NET Standard 2.0 to 2.1. Afterward, new static items (classes, properties, methods) defined in the library would not be recognized in the app or Visual Studio. If I reset the library back to .NET Standard 2.0 then the static items became visible, but that was not a viable solution for me.
To solve the problem, I set the library's target framework back to .NET Standard 2.1. Then I opened the app project's Reference Manager (Solution Explorer > App Project > Dependencies (right click) > Add Project Reference). I cleared the existing reference to the library and clicked OK. Then I repeated the process, but restored the reference to the library. Suddenly, the app and Visual Studio recognized all the new static items in the library.
The reason is because in the second sample, the class is in a different namespaceMyNameSpace.Service.Services.
If you add a "using" for the namespace MyNameSpace.UI.Helpers in your file, it will be visible.
You can also reference your class as UI.OrderHelper from your services, since both share the root "MyNamespace".
Related
I use a simple postsharp.config file :
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.postsharp.org/1.0/configuration">
<Multicast xmlns:my="clr-namespace:ExceptionAutoSerializer.Aspects;assembly:ExceptionAutoSerializer">
<my:MethodBoundaryAspect AttributeTargetTypes="MyTopLevelNamespace.*" />
<my:MethodBoundaryAspect AttributeTargetMembers="*ctor*" AttributeExclude="true"/>
<my:MethodBoundaryAspect AttributeTargetMembers="get_*" AttributeExclude="true"/>
<my:MethodBoundaryAspect AttributeTargetMembers="set_*" AttributeExclude="true"/>
</Multicast>
</Project>
All my project in my solution are under the namespace MyTopLevelNamespace. And every single project in the solution has the aspect applied to it correctly except for my website project. I'm not familiar with the solution as I just got in the dev team.
All I know is that I would like to apply aspect to classes within this project and that postsharp seems to ignore that particular project. The config file is located in the src/ folder and should be applied to all project.
I've made sure the types I'm applying my aspect to are under the namespace specified in the config file and that it doesn't match any of the excluding patterns.
Did I provide enough information ? I'm not sure it is due to the project beeing a website project but I can't see anything else.
Edit: I've made sure I added the nuget package to the project. I also tried to manually add the aspect with an attribute to a specific method of this project and the aspect doesn't trigger.
Edit2: this is the method that I use to test:
[MethodBoundaryAspect]
public bool Foo(string bar1, string bar2)
{
// at runtime test contains indeed one attribute MethodBoundaryAspect
var test = this.GetType().GetMethod("ValidateUser").GetCustomAttributes(false);
//here the exception is caught higher up but the "onException" of my attribute doesn't trigger
throw new Exception("test exception");
}
and my postsharp aspect :
namespace ExceptionAutoSerializer.Aspects
{
[Serializable]
public class MethodBoundaryAspect : OnMethodBoundaryAspect
{
//[...]
public override void OnEntry(MethodExecutionArgs args)
{
//[...]
}
public override void OnSuccess(MethodExecutionArgs args)
{
//[...]
}
public override void OnException(MethodExecutionArgs args)
{
//[...]
}
}
}
According to an old answer of a developper at PostSharp Technologies :
PostSharp is (currently) integrated via MSBuild (as also mentioned by
John Saunders), which is not used by website projects.
While in it's core PostSharp is a command-line tool, it gets so much
information from MSBuild that it's quite hard to make it work
separately (and neither advised nor documented nor supported in the
first place). Daniel Balas
I didn't find any update on that subject for version older than 4.3. For 4.3 and earlier according to the previous answer and this documentation (p. 45) website project are not supported.
Edit: when questioning the previously quoted author about the current validity of they answer they responded this in comment :
#Amon For project-less websites (which IIRC are not creatable from the
UI in VS 2017 and later) it is still true. However, all web projects
that have csproj/vbproj (.NET or .NET Core) are working as expected
(this also didn't change since then). – Daniel Balas
I'm trying to get .Net Framework and NetStandard assemblies to communicate with each other (to learn what is possible). I currently have four projects, two Framework 4.5.2 projects and two NetStandard1.2 projects:
Framework452.Library
NetStandard12.CentralLibrary
NetStandard12.BaseLibrary
Framework452.Tests
The referencing structure is:
Framework452.Tests references NetStandard12.CentralLibrary: working by adding the NetStandard.Library nuget package to Framework452.Tests.
NetStandard12.CentralLibrary references NetStandard12.BaseLibrary: working without modification.
NetStandard12.CentralLibrary references Framework452.Library: Not working, even when Framework452.Library has the NetStandard.Library nuget package installed.
Can NetStandard projects reference Framework projects? If so, what do I need to do to get them to communicate? At the moment I can add the reference, but it is not visible to the code.
Update
I recreated the solution and added the code below, which when I try to compile gives the following error from the Framework452.Tests project:
Error CS0006: Metadata file
'~\TryNETStandard\NetStandard12.CentralLibrary\bin\Debug\netstandard1.2\NetStandard12.CentralLibrary.dll'
could not be found.
namespace Framework452.Library
{
public class Returner452 {
public static bool ReturnTrue() { return true; }
}
}
using Xunit;
namespace Framework452.Tests
{
public class Class1 {
[Fact]
public void FrameworkTest() {
Assert.True(NetStandard12.CentralLibrary.Class1.Return452());
}
[Fact]
public void NetStandardTest() {
Assert.True(NetStandard12.CentralLibrary.Class1.Return12());
}
}
}
namespace NetStandard12.BaseLibrary
{
public class Returner12 {
public static bool ReturnTrue() { return true; }
}
}
using Framework452.Library;
using NetStandard12.BaseLibrary;
namespace NetStandard12.CentralLibrary
{
public class Class1
{
public static bool Return452() { return Returner452.ReturnTrue(); }
public static bool Return12() { return Returner12.ReturnTrue(); }
}
}
According to this page https://learn.microsoft.com/en-us/dotnet/standard/net-standard#net-platforms-support you should be able to achieve your purpose because .NET Standard 1.2 support .NET Framework 4.5.1 (UPDATE: This statement is not 100% correct. Please see the Update section below.)
I tried to set up a solution in VS 2017 and set the references as you described. Here is the result.
and this is the Class1.cs in NetStandard12.CentralLibrary
The code compiles fine without any errors.
Note: your code may fail if the Framework452.Library uses an API that is not supported by .NET Standard 1.2 (e.g Winforms, Win32 API or any Microsoft proprietary library that does not make sense for cross platform).
I recommend this youtube playlist on the .NET standard introduction from one of the MSFT https://www.youtube.com/watch?v=YI4MurjfMn8&list=PLRAdsfhKI4OWx321A_pr-7HhRNk7wOLLY
In .NET Standard - Checking Compatibilty , he recommended tools to help you find out what API is not supported in the .NET Standard.
Thing will become easier with .NET Standard 2.0 and 'compat shim'
UPDATE:
After trying again with more data provided by the question, it's true that a library targeting (depends) .NET Standard could not depend on a library that target .NET Framework. For some strange reason, the compiler allows me to compile the example that I gave above. This could be a bug in tooling.
After a little more research, I found a good example demonstrate the relationship between NetStandard and NetFramework: How .NET Standard relates to .NET Platform.
The graph here show the dependencies
According to the graph, there is no way a library that depends on .NET Standard could see/use the .NET framework implementation.
When .NET Standard 2 is released, this may change a little bit and you could reference .NET Framework via Compatibility Shim. See this video for more in-depth explanation https://youtu.be/vg6nR7hS2lI?list=PLRAdsfhKI4OWx321A_pr-7HhRNk7wOLLY&t=169
No, .NET Standard projects cannot reference framework projects.
.NET Standard projects need to be usable across platforms, forcing a dependency on the .NET framework by referencing an assembly targeting it makes this impossible.
Note that with some of the magic Microsoft is doing with .NET Standard 2.0 this is less true but the overall idea still stands.
Let's say I have:
public abstract ServiceBase : RoleEntryPoint {
...
}
public MyRealService : RoleEntryPoint {
...
}
Will my Azure WorkerRole project be able to correctly figure out MyRealService class as the WorkerRole entry point?
This used to be working well for me but now that I've updated to Azure Tooling 2.0 it doesn't hit any breakpoint so I'm not even sure what entry point class it is loading.
Problem solved. It basically had to do with some references pointing to the old 1.8.0.0 DLL and some to the 2.0.0.0.
So following the example above:
ServiceBase: was defined in a different project that was depending on 1.8.0.0.
MyRealService: was defined in the top-level project that depends on 2.0.0.0.
So since the base class inheriting from RoleEntryPoint was from an older version of Microsoft.WindowsAzure.ServiceRuntime, MyRealService class was never found despite the actual C# project depending on version 2.0.0.0 of Microsoft.WindowsAzure.ServiceRuntime.
Thanks so much to the answer in this thread:
Azure WorkerRole Stopping Immediately After Starting
I created a C# class library using visual studio and it works well in visual studio, and when trying to execute it in mono develop I get error the library code:
namespace ClassLibrary2
{
public class Class2
{
public static void dosomething() {Console.WriteLine("dfss"); }
}
}
the code in mono develop:
namespace cons
{
class MainClass
{
public static void Main (string[] args)
{
ClassLibrary2.Class2.dosomething();
}
}
}
the error
What target framework (2.0, 3.5, 4.0) did you specify in VS?? It seems that your mono develop uses different (lower) version.
You need to check that your .Net Framework versions are the same. You will have chosen a framework in VS that is different to that which mono develop is trying to use. This is a very common cause of this error.
The missing assembly is ClassLibrary2 and your code only deals with ClassLibrary1.
Maybe you could add the registry key to enable a more verbose logging.
I have a project that I'm upgrading from 2008 to 2010.
My problem is with the code being generated by a service reference. Any classes used in the Reference.cs are given by their full name according to the reference
The difference results in a 'cannot convert from foo.Data.MtkBaseRequest to foo.WebServicesClient.ModelDataService.MtkBaseRequest'
So I have the old code
namespace foo
{
public abstract class MtkBaseRequest{}
}
And this code being called
List<MtkBaseRequest> newList = new List<MtkBaseRequest>(requestArray);
this.DataAccessWebService.doStuff(newList);
but this is the method it means to call(which is generated)
public void doStuff(System.Collections.Generic.List<foo.WebServicesClient.ModelDataService.MtkBaseRequest> requests)
So what's happening is that the namespace no longer matches for the class which should be the same. In the code generated by VS 2008 the method being called is:
public void doStuff(System.Collections.Generic.List<foo.Data.MtkBaseRequest> requests)
Is there a work-around, or something I can do to get the code generating properly?
BTW: the project this is in is called WebServicesClient, the service reference is ModelDataService
I fixed it, turns out there were some assemblies hanging around in the GAC.
stupid GAC