PreApplicationStartMethod cannot be resolved - c#

I'm trying to use Unity.Webforms in a web application without success. As per the documentation, I put the following line in the AssemblyInfo.cs to tell the runtime to invoke a method before the application starts:
[assembly: PreApplicationStartMethod(typeof(Unity.WebForms.PreApplicationStart), "PreStart")]
The error I'm getting says:
The method specified by the PreApplicationStartMethodAttribute on assembly ... cannot be resolved
And this is the class holding the method (within App_Start folder):
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using System;
using System.Collections.Generic;
using System.Web;
namespace Unity.WebForms
{
public static class PreApplicationStart
{
private static bool _isStarting;
public static void PreStart()
{
if (!_isStarting)
{
_isStarting = true;
DynamicModuleUtility.RegisterModule(typeof(UnityHttpModule));
}
}
}
}
Is there anything I'm missing or misunderstanding?
Thanks

Change Unity.Webforms namespace in your custom file with PreApplicationStart, because this namespace already exists.
My solution, that works:
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
namespace TestApp.WebUi
{
public static class PreApplicationStart
{
private static bool _isStarting;
public static void PreStart()
{
if (!_isStarting)
{
_isStarting = true;
DynamicModuleUtility.RegisterModule(typeof(UnityHttpModule));
}
}
}
}
And:
[assembly: PreApplicationStartMethod(typeof(TestApp.WebUi.PreApplicationStart), "PreStart")]

Related

Where in my static class can I initialize a Trace Listener?

I want to add Debug and Trace to a class that I've created. However, I don't know where in my code to initialize a Trace Listener.
Here is the code I want to add to my static class:
Trace.Listeners.Add(
new TextWriterTraceListener(
File.CreateText("log.txt")
)
);
Trace.AutoFlush = true;
Here is the class to which I want to add the Trace Listener:
using System;
using System.Diagnostics;
using System.IO;
namespace Example.Shared
{
public static class ExampleClass
{
public static string SaySomething()
{
Trace.WriteLine($"I'm gonna say something");
return "Something";
}
// etc.
}
}
Apart from this class, I have only created some unit tests using Xunit. I have not yet created the application that will use this class.
This is what the unit test code looks like:
using Example.Shared;
using Xunit;
namespace ClassLibTests
{
public class ExampleClassTests
{
[Fact]
public void TestSaySomething()
{
string expected = "Something";
string actual = ExampleClass.SaySomething();
Assert.Equal(expected, actual);
}
// etc.
}
}
I execute the above tests at the command line with the command dotnet test.
In order to initialize any static class or static members of a class you can use static constructors. So, you can update your code as follow:
using System;
using System.Diagnostics;
using System.IO;
namespace Example.Shared;
public static class ExampleClass
{
static ExampleClass()
{
Trace.Listeners.Add(
new TextWriterTraceListener(File.CreateText("log.txt"))
);
Trace.AutoFlush = true;
}
public static string SaySomething()
{
Trace.WriteLine($"I'm gonna say something");
return "Something";
}
// etc.
}
For more details check:
C# static class constructor
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-constructors
BTW, I've also updated code to use the newly introduced file scoped namespace. Less indentation.
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-10.0/file-scoped-namespaces

C# class can't see static method of another class

I've run into some accessibility problem with one of my Unity projects. I created a class that does NOT inherit from MonoBehaviour. It has a method public static void LoadScene(string sceneName).
When I try to call this method from another class, I get a syntax error. This is my first script with the static method:
public class GameLoader
{
public static void LoadScene(string sceneName)
{
SceneManager.LoadSceneAsync(sceneName);
}
}
And here is my other script:
public class GameHandler : MonoBehaviour
{
private void Start()
{
GameLoader.LoadScene("MyScene"); //Syntax error
}
}
Normally, I would have some idea about what might the problem be, but in this case, the GameHandler recognizes GameLoader as class, but after the dot (GameLoader.), it does not find any property or function at all. And I get a syntax error when I try to write anything after the dot.
I experimented a little and it doesn't seem like I would cross another class with the name GameLoader and the neccessary namespace was added as well.
I'm pretty lost here, I hope someone can help me out.
Original codeGameLoader:
using UnityEngine.SceneManagement;
using UnityEngine;
namespace MyGame
{
namespace System
{
public class GameLoader
{
public static void LoadScene()
{
}
}
}
}
Original UIHandler:
using UnityEngine;
using System;
namespace MyGame
{
namespace System
{
namespace UI
{
public class UIHandlerMenu : MonoBehaviour
{
GameLoader.LoadScene();
}
}
}
}
Error message:
Severity Code Description Project File Line Suppression State
Error IDE1007 The name 'GameLoader.LoadScene' does not exist in the current context.
And the same error for just LoadScene itself.
Edit:
After your edit it is now obvious where the problem lies.
Try moving your call GameLoader.LoadScene("bla"); into a method.
If you want this method getting called when instantiating your handler you can move it to a constructor.
Example:
public UiHandlerMenu() {
GameLoader.LoadScene("bla");
}

Call static method in another class library

How to call static method in another class library? Because I created 2 class library Common and another call DAL, and I already add Commmon dll reference in my DAL class library, after that I cannot find out my method name. Isn't got other method to solve it?
Class library : Common
using System;
namespace Common
{
public class Log
{
public static bool Error(Exception ex)
{
return true;
}
}
}
Class library : DAL
using Common
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DAL
{
public class MenuD
{
Log.;
}
}
You can't call the Log.Error() method from that location in the class, outside of a method.
Call it from within a method in the MenuD class.
public class MenuD
{
public void SomeMethod()
{
try
{
// do something that could throw an exception
}
catch (Exception ex)
{
var isLogged = Log.Error(ex);
}
}
}

C# subclass while maintaining name. Deep voodoo?

I have a dll that I'm working with, it contains a class foo.Launch. I want to create another dll that subclasses Launch. The problem is that the class name must be identical. This is used as a plugin into another piece of software and the foo.Launch class is what it looks foe to launch the plugin.
I've tried:
namespace foo
{
public class Launch : global::foo.Launch
{
}
}
and
using otherfoo = foo;
namespace foo
{
public class Launch : otherfoo.Launch
{
}
}
I've also tried specifying an alias in the reference properties and using that alias in my code instead of global, that also didn't work.
Neither of those methods work. Is there a way I can specify the name of the dll to look in within the using statement?
You'll need to alias the original assembly and use an extern alias to reference the original assembly within the new one. Here's an example of the use of the alias.
extern alias LauncherOriginal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace foo
{
public class Launcher : LauncherOriginal.foo.Launcher
{
...
}
}
Here's a walkthrough that explains how to implement that.
Also, you'd mentioned that you tried to use an alias before and encountered problems but you didn't say what they were, so if this won't work then please mention what went wrong.
as Chris said, you can use an alias on your original assembly.
If you can't you that, then you might be able to cheat by using a 3rd assembly
Assembly1.dll (your original)
namespace foo {
public class Launch {}
}
Assembly2.dll (dummy)
namespace othernamespace {
public abstract class Dummy: foo.Launch {}
}
Assembly3.dll (your plugin)
namespace foo{
public class Launch: othernamespace.Dummy{}
}
I'm not even proud of this!
Class name can be identical if it's defined in another namespace, but it boggles the mind why anybody would want to do that to themselves.
Maybe you need to use extern aliases.
For example:
//in file foolaunch.cs
using System;
namespace Foo
{
public class Launch
{
protected void Method1()
{
Console.WriteLine("Hello from Foo.Launch.Method1");
}
}
}
// csc /target:library /out:FooLaunch.dll foolaunch.cs
//now subclassing foo.Launch
//in file subfoolaunch.cs
namespace Foo
{
extern alias F1;
public class Launch : F1.Foo.Launch
{
public void Method3()
{
Method1();
}
}
}
// csc /target:library /r:F1=foolaunch.dll /out:SubFooLaunch.dll subfoolaunch.cs
// using
// in file program.cs
namespace ConsoleApplication
{
extern alias F2;
class Program
{
static void Main(string[] args)
{
var launch = new F2.Foo.Launch();
launch.Method3();
}
}
}
// csc /r:FooLaunch.dll /r:F2=SubFooLaunch.dll program.cs

C# web project complaining about a class

I created a new project (web project in C#).
Created a new folder in my project called App_Code.
I then proceeded to add a class with the following:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Shipper.DataLayer;
namespace Shipper.BusinessLayer
{
public static class BL
{
public static int JustSomeTest()
{
return 1;
}
}
}
And in one of my pages default.aspx in the code behind I tried to do:
int i = BL.JustSomeTest();
I am not getting any intellisense when I type in BL.. It says I am missing an assembly or reference. Or it will say The name BL does not exist in the current context.
But do I have to include a reference if the class file is in the same project? I even tried to Build my project and it at first generated a dll file with the name of my solution file, Shipper.dll but as soon as I add this reference it says The name BL does not exist in the current context.
In my default.aspx page I've tried to add a using statement
using Shipper.
But as soon as I do that my namespace BusinessLayer is not shown...
Im confused?
Edit
Here is default.aspx:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Shipper.BusinessLayer;
namespace Shipper
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SetDefaults();
int i = BL.JustSomeTest();
}
}
}
}
Here is my BL.cs file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Shipper.DataLayer;
namespace Shipper.BusinessLayer
{
public static class BL
{
public static int JustSomeTest()
{
return 1;
}
}
}
The error reads The type or namespace nameBusinessLayerdoes not exist in the namespace Shipper (are you missing an assembly reference)?
your...
public static int JustSomeTest()
{
return 1;
}
...is out of the 'class' - you cannot have methods defined for the namespace alone.
(at least from your example, it might be just a typo but then you'd need to give us a working example)
your method was outside a class. That's a no-no. Try this instead:
namespace Shipper.BusinessLayer
{
public static class BL
{
public static int JustSomeTest()
{
return 1;
}
}
}
Also, if you're expecting BL to pop up, make sure the namespace is referenced (using Shipper.BusinessLayer). Why is this a static class, though? I think you probably don't want that unless you're making extension methods.
Try to delete the namespace declaration:
using System;
public static class BL
{
public static int JustSomeTest()
{
return 1;
}
}
This forum thread on Wrox might be also useful.
try
using Shipper.BusinessLayer;
Things to try
Verify the namespace is the one you believe it is by viewing the properties of the target project in the solutions explorer.
In Visual Studio use the Object Browser and locate the namespace and verify the class can be seen. If the object browser can't see it, neither can the code.
First thing is make sure you have added a reference to the Shipper project, not the shipper DLL.
Then make sure the Shipper project is re-built, best bet to do a clean and build.
Then check your intellisense again, I suspect you are referencin an oleder version of the Shipper dll.
Try making your class not static but leave your method as static. (only because I never use static classes, but it doesn't appear to make any difference)
Or if you are seriously having problems and can't work it out install a copy of Resharper and it will probably help!

Categories

Resources