Fody MethodDecorator not working - fody

I am trying to create a method decorator using Fody but it gives me the following error:
I have taken specific care to not wrap my IMethodDecorator inside any namespace as has been mentioned in a lot of places online. Following is the sample code I am trying in a console app.
IMethodDecorator
using System;
using System.Reflection;
public interface IMethodDecorator
{
void OnEntry(MethodBase method);
void OnExit(MethodBase method);
void OnException(MethodBase method, Exception exception);
}
MethodDecoratorAttribute
using System;
using System.Diagnostics;
using System.Reflection;
using FODYPOC;
// Atribute should be "registered" by adding as module or assembly custom attribute
[module: MethodDecorator]
namespace FODYPOC
{
// Any attribute which provides OnEntry/OnExit/OnException with proper args
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Assembly | AttributeTargets.Module)]
public class MethodDecoratorAttribute : Attribute, IMethodDecorator
{
// instance, method and args can be captured here and stored in attribute instance fields
// for future usage in OnEntry/OnExit/OnException
public MethodDecoratorAttribute() { }
public void OnEntry(MethodBase method)
{
Console.WriteLine();
}
public void OnExit(MethodBase method)
{
Console.WriteLine();
}
public void OnException(MethodBase method, Exception exception)
{
Console.WriteLine();
}
}
public class Sample
{
[MethodDecorator]
public void Method()
{
Debug.WriteLine("Your Code");
}
}
}
Can someone point me in the right direction. It looks pretty simple to implement and I know I am making a very silly mistake somewhere.

Apparently the latest version of MethodDecorator.Fody (Version 0.9.0.6 currently) was not working. Downgrading the version to version 0.8.1.1 fixed the issue for me.
After a little more investigation, it appears that the interface method signatures were different in the two versions. So when I had the new package, it was not expecting MethodBase as a parameter and due to not finding anything that matches the interface it expects, it was throwing the error.

Related

Why does 'InputField' not contain a definition for 'text'?

I'm currently working on a Unity project for a college assignment, and I'm currently trying to connect a login/registration through PlayFab into a teammate's main menu for the game.
I've connected the PlayFabManager.cs script to the Input Fields for the email and password in the Unity editor, and something about my InputFields.cs file is preventing me from making any more progress.
I had to change the passwordInput and emailInput variables to TMP_InputField variables to achieve this, but now I am getting a compilation error in my project that says the following:
Assets\Scripts\InputField.cs(13,24): error CS1061: 'InputField' does not contain a definition for 'text' and no accessible extension method 'text' accepting a first argument of type 'InputField' could be found (are you missing a using directive or an assembly reference?)
Most places I look have people not including the "using UnityEngine.UI;" header at the top of the file, but that's included in my InputField.cs file.
Here's the code for my InputField.cs file:
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.
public class InputField : MonoBehaviour
{
public InputField mainInputField;
public void Start()
{
mainInputField.text = "Enter text here...";
}
}
Here's the code for my PlayFabManager.cs file:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PlayFab;
using PlayFab.ClientModels;
using Newtonsoft.Json;
using UnityEngine.UI;
using TMPro; // Needed for login input fields
public class PlayFabManager : MonoBehaviour
{
[Header("UI)")]
public Text messageText;
public TMP_InputField emailInput;
public TMP_InputField passwordInput;
// Register/Login/ResetPassword
public void RegisterButton() {
var request = new RegisterPlayFabUserRequest {
Email = emailInput.text,
Password = passwordInput.text,
RequireBothUsernameAndEmail = false
};
PlayFabClientAPI.RegisterPlayFabUser(request, OnRegisterSuccess, OnError);
}
void OnRegisterSuccess(RegisterPlayFabUserResult result) {
messageText.text = "Registered and Logged in";
}
public void LoginButton() {
}
// Start is called before the first frame update
void Start() {
Login();
}
// Update is called once per frame
void Login() {
var request = new LoginWithCustomIDRequest {
CustomId = SystemInfo.deviceUniqueIdentifier,
CreateAccount = true
};
PlayFabClientAPI.LoginWithCustomID(request, OnSuccess, OnError);
}
void OnSuccess(LoginResult result) {
Debug.Log("Successful login/account create.");
}
void OnError(PlayFabError error) {
Debug.Log("Error while loggin in/creating account.");
Debug.Log(error.GenerateErrorReport());
}
}
I would just remove the InputField.cs class as it fixes my errors, but it changes the functionality of the following code that my teammate has contributed:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class MenuControl : MonoBehaviour
{
public string newGameLevel;
public void NewUser() {
SceneManager.LoadScene(newGameLevel);
}
public void ExitButton() {
Application.Quit();
}
}
Any help would be much appreciated!
Wanted to provide the solution in case this happens to anyone in the future:
I solved the problem by changing the
public InputField mainInputField;
into an input variable that could receive the TMP_Imput like so: public TMPro.TMP_InputField mainInputField;

BeforeScenario and AfterScenario hooks not working in specflow

I have the following code in my SeleniumSteps.cs code
I am trying to get the AfterScenario to fire on debugging these tests
using PrivateDomain;
using Machine.Specifications;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using TechTalk.SpecFlow;
namespace Functional.Steps
{
public class SeleniumSteps : PrivateDomain.Steps.SeleniumSteps
{
#region Hooks
[BeforeScenario]
public void Before()
{
// before
}
[AfterTestRun, Scope(Tag = null)]
public new static void AfterTestRun()
{
// after testrun
}
[AfterScenario]
public void AfterScenarioErrorScreenshot()
{
// after scenario
}
#endregion
}
}
using OpenQA.Selenium;
using TechTalk.SpecFlow;
namespace PrivateDomain.Steps
{
[Binding]
[Scope(Tag = "Selenium")]
public class SeleniumSteps
{
protected static IWebDriver webDriver;
public SeleniumSteps();
public virtual IWebDriver WebDriver { get; }
[AfterTestRun]
[Scope(Tag = null)]
public static void AfterTestRun();
[AfterScenarioAttribute(new[] { })]
public virtual void AfterScenario();
}
}
My feature file looks like this:
(Details removed)
#Customer_Portal
Feature: Account Management - Registration
In order to create an account
As a customer
I want to register my details with the application
Scenario: Register
# Registration Form
When I navigate to "/Customer/Account/Register"
// more code...
Scenario: Required Fields
// more code...
Scenario: Invalid Contact Details
// more code...
Scenario: Insufficient Password Strength
// more code...
Scenario: Password Mismatch
// more code...
Scenario: Already Registered
// more code...
Scenario: Invalid Activation
// more code...
Scenario: Already Activated
// more code...
When I debug a test, I can see the debugger hitting the AfterTestRun portion.
However, neither the BeforeScenario or the AfterScenario are being exercised.
Can someone tell me what I'm doing wrong?
First, as Sandesh noted in his answer, you are missing [Binding] attribute for your SeleniumSteps subclass. It's not enough to have [Binding] only in base class, you must apply it to every class where are your hook methods or step definitions (bindings), because that is the way how specflow is searching for hooks and bindings under the hood. It is like scope identifier. If you miss to place [Binding] attribute to class, specflow will not search for potential hook methods or bindings in that class. Link on documentation: https://specflow.org/documentation/Hooks/
This link can be useful also. Check answer given by RunOfTheShipe: Specflow test step inheritance causes "Ambiguous step definitions"
You have missed [Binding] attribute in your SeleniumSteps
namespace Functional.Steps
{
[Binding]
public class SeleniumSteps : PrivateDomain.Steps.SeleniumSteps
{
#region Hooks
[BeforeScenario]
public void Before()
{
// before
}
}
}

How to get first or guardian actor in akka.net?

Edit : Executive summary: Where the bleep is 'Sys' defined? I see it in Akka.net code all over the internet, but my build is not finding it. Who or what do I have to import, use, link, do, bribe or kill?
Should be screamingly easy. Taking first steps in Akka.net, the sample does not build. This was copied from the [Getting Started example][1]
[1]: https://getakka.net/articles/intro/tutorial-1.html . It does not build, because 'Sys' is not defined. This obviously elementary step is nowhere described on their site, and I've given up on tweak-n-try.
Here is all of the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyAkka
{
class Program
{
public class PrintMyActorRefActor : UntypedActor
{
protected override void OnReceive(object message)
{
switch (message)
{
case "printit":
IActorRef secondRef = Context.ActorOf(Props.Empty, "second-actor");
Console.WriteLine($"Second: {secondRef}");
break;
}
}
}
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
var firstRef = Sys.ActorOf(Props.Create<PrintMyActorRefActor>(), "first-actor");
Console.WriteLine($"First: {firstRef}");
firstRef.Tell("printit", ActorRefs.NoSender);
Console.ReadKey();
}
}
}
Here is a working version of your code:
using System;
using Akka.Actor;
namespace SysInAkkaNet
{
class Program
{
public class PrintMyActorRefActor : UntypedActor
{
protected override void OnReceive(object message)
{
switch (message)
{
case "printit":
IActorRef secondRef = Context.ActorOf(Props.Empty, "second-actor");
Console.WriteLine($"Second: {secondRef}");
break;
}
}
}
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
using (var actorSystem = ActorSystem.Create("MyActorSystem"))
{
var firstRef = actorSystem.ActorOf(Props.Create<PrintMyActorRefActor>(), "first-actor");
Console.WriteLine($"First: {firstRef}");
firstRef.Tell("printit", ActorRefs.NoSender);
Console.ReadKey();
}
}
}
}
You need to create an actor system to put your actors in. And you need to add a reference to the Akka NuGet package, and a corresponding using Akka.Actor; statement.
I know that the Akka.TestKit has a property Sys, which gives you a reference to the actor system that is created for a given test.
Apart from that, I am not able to answer why the documentation you are referring to shows these "Sys.ActorOf(...)" examples like that (with a capital S), indicating that it is a (possibly built-in) property, so I kind of understand your confusion there.

IWindsorContainer RegisterControllers Missing Method AddLifestyle error

I recently updated a 2.5.2 project to 3.2.1 / MVC 4 recompiling the application after a few code fixes, I am getting a Register Controller issue at run-time. Here is the code:
public static void Register(string key, Type interfaceType, Type implementationType)
{
//IoC.Container.AddComponent(key, interfaceType, implementationType);
// IoC.Container.AddComponent is obsolete. Replaced by:
IoC.Container.Register(Component.For(interfaceType).ImplementedBy(implementationType).Named(key));
}
public static void RegisterControllers(params Assembly[] assemblies)
{
//IoC.Container.RegisterControllers(assemblies);
foreach (Assembly assembly in assemblies)
{
IoC.Container.RegisterControllers(assembly.GetExportedTypes());
}
}
public static void RegisterAllFromAssemblies(string baseAssembly, string relatedAssembly)
{
// AllTypes is obsolete
//
//IoC.Container.Register(AllTypes.
// FromAssemblyNamed(baseAssembly).
// WithService.
// FirstNonGenericCoreInterface(relatedAssembly));
//Update for Castle-Windor 3.2
IoC.Container.Register(Classes.FromAssemblyNamed(baseAssembly)
.Pick().WithServiceFirstInterface()
.LifestylePerWebRequest());
}
}
}
The MissingMethod Exception screen shows after execution of IoC.Container.RegisterControllers(assembly.GetExportedTypes());
"Method not found: 'Castle.Windsor.IWindsorContainer Castle.Windsor.IWindsorContainer.AddComponentLifeStyle(System.String, System.Type, Castle.Core.LifestyleType)'."
Pointing me in the right direction would be helpful
I found this unanswered question meanwhile upgrading a Sharp Architecture based project from SharpArch 1.6 to SharpArch 3.0 and thought a final answer to this might be useful. I got the same problem about
"Method not found: 'Castle.Windsor.IWindsorContainer
Castle.Windsor.IWindsorContainer.AddComponentLifeStyle(System.String,
System.Type, Castle.Core.LifestyleType)'."
and implemented a solution following the suggestion about IWindsorInstaller from
Charleh.
Basically, I created an IWindsorInstaller implementation moving there all my needed IoC dependencies registrations
public class ComponentsInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store)
{
AddApplicationServicesTo(container);
//eventually other services to install
}
private void AddApplicationServicesTo(IWindsorContainer container)
{
container.Register(Classes.FromAssemblyNamed("myAssemblyName1").Pick().WithService.FirstInterface());
container.Register(Classes.FromAssemblyNamed("myAssemblyName2").Pick().WithService.FirstInterface());
}
}
and called that installer from Global.asax Application_Start with
IWindsorContainer container = new WindsorContainer();
container.Install(FromAssembly.This());

Using PostSharp OnExceptionAspect across mulit projects

Good afternoon everyone,
I am trying to use the example "Aspect Oriented Programming Using C# and PostSharp" by Reza Ahmadi
http://www.codeproject.com/Articles/337564/Aspect-Oriented-Programming-Using-Csharp-and-PostS and dnrTV http://dnrtv.com/dnrtvplayer/player.aspx?ShowNum=0190 for the exception handling. Everything works great if the "OnExceptionAspect" is in the same project/assembly, however the event does not work if it I move the class to it own dll.
[assembly: ExceptionAspect (AttributePriority = 1)]
[assembly: ExceptionAspect(AttributePriority = 2, AttributeExclude = true, AttributeTargetTypes = "HpsErp.Common.AspectObject.*")]
namespace AspectObject
[Serializable]
public class ExceptionAspect : OnExceptionAspect
{
public override void OnException(MethodExecutionArgs args)
{
Trace.TraceError("{0} in {1}.{2}",
args.Exception.GetType().Name,
args.Method.DeclaringType.FullName,
args.Method.Name);
if (args.Instance != null)
{
Trace.TraceInformation("this={0}", args.Instance);
}
foreach (ParameterInfo parameter in args.Method.GetParameters())
{
Trace.TraceInformation("{0}={1}", parameter.Name,
args.Arguments[parameter.Position] ?? "null");
}
}
I also created a class in the external dll for "Timing" and it works great if I add a custom attribute to the class.
namespace AspectObject
[Serializable]
[MulticastAttributeUsage(MulticastTargets.Method)]
public class TimingAspect : OnMethodBoundaryAspect
{
[NonSerialized]
Stopwatch _StopWatch;
public override void OnEntry(MethodExecutionArgs args)
{
_StopWatch = Stopwatch.StartNew();
base.OnEntry(args);
}
public override void OnExit(MethodExecutionArgs args)
{
Console.WriteLine(string.Format("[{0}] took {1}ms to execute",
new StackTrace().GetFrame(1).GetMethod().Name,
_StopWatch.ElapsedMilliseconds));
base.OnExit(args);
}
Using AspectObject;
namespace MyApp
{
public class Car
{
[TimingAspect]
private void Drive()
{
//...
}
}
}
In the end, I am hoping to have this is multi dlls so that I can reuse it ie: wcf.
Thanks for any and all help...
James
You can access your aspects if they are stored in a separate DLL.
I always create a DLL class project called Aspects. In the projects I want AOP, I add a reference to that dll class. Then decorate your methods/class/assembly like you normally do.
https://github.com/sharpcrafters/PostSharp-Toolkits <-- good examples
http://researchaholic.com/tag/postsharp/ <-- some more examples, just uploaded an example