I am exploring PostSharp. I ran in to an early snag I don't understand. I am hoping for insight.
I have VS 2013 12.0.3051.00 Update 2. I used NuGet to install PostSharp 3.1.43.0. I also use Resharper 8.2.1 and I have dotMemory 4.0 installed.
I implemented the first tutorial, at http://www.postsharp.net/aspects/getting-started?utm_source=vsx&utm_medium=app&utm_campaign=Learn. This behaves as expected. I do some refactoring. First, I extract the aspects to a library.
namespace AspectImplementations
{
using System;
using System.Diagnostics;
using PostSharp.Aspects;
[Serializable]
public sealed class TraceAttribute : OnMethodBoundaryAspect
{
/// <summary>
/// Initializes a new instance of the <see cref="TraceAttribute" /> class.
/// </summary>
/// <remarks>
/// http://www.postsharp.net/aspects/getting-started?utm_source=vsx&utm_medium=app&utm_campaign=Learn
/// </remarks>
public TraceAttribute(string category)
{
Category = category;
}
public string Category { get; private set; }
#region Overrides of OnMethodBoundaryAspect
public override void OnEntry(MethodExecutionArgs args)
{
Trace.WriteLine(
string.Format(
"Entering {0}.{1}.",
args.Method.DeclaringType.Name, args.Method.Name,
Category));
}
public override void OnExit(MethodExecutionArgs args)
{
Trace.WriteLine(
string.Format(
"Leaving {0}.{1}.",
args.Method.DeclaringType.Name, args.Method.Name,
Category));
}
#endregion
}
}
I compile the project and run it and get expected behavior.
Next, I factor out the methods attibuted with the aspects to their own library.
namespace Conversation
{
using System;
using AspectImplementations;
public class Greetings
{
[Trace("Hi")]
public void SayHello()
{
Console.WriteLine("Hello, PostSharp");
}
[Trace("Bye")]
public void SayGoodBye()
{
Console.WriteLine("Goodbye, PostSharp");
}
}
}
I compile the code, and ignore the warning that PostSharp isn't needed in HelloPostSharp, anymore, run the app, and it behaves as expected.
Now I want to add some unit tests. MSTest works for me. This is where my trouble starts.
namespace TestConversation
{
using Conversation;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class TestGreetings
{
[TestMethod]
public void TestCtor()
{
Greetings talker = new Greetings();
Assert.IsNotNull(talker);
}
[TestMethod]
public void TestSayHelloNotExceptional()
{
Greetings talker = new Greetings();
talker.SayHello();
}
[TestMethod]
public void TestSayGoodByeNotExceptional()
{
Greetings talker = new Greetings();
talker.SayGoodBye();
}
}
}
When I compile this, I get a dialog that suggests I need to add dependencies to PostSharp. I hit the OK button and let it add stuff. I get a build failure. I put "SkipPostSharp" in the HelloPostSharp project, where Main is. Still won't build. My Build output looks like
1>------ Rebuild All started: Project: AspectImplementations, Configuration: Debug Any CPU ------
1> : message : PostSharp 3.1 [3.1.43.0, 32 bit, CLR 4.5, Release] complete -- 0 errors, 0 warnings, processed in 899 ms
1> AspectImplementations -> C:\Users\ssailors.WPC\Documents\Visual Studio 2013\Projects\HelloPostSharp\AspectImplementations\bin\Debug\AspectImplementations.dll
2>------ Rebuild All started: Project: Conversation, Configuration: Debug Any CPU ------
2> : message : PostSharp 3.1 [3.1.43.0, 32 bit, CLR 4.5, Release] complete -- 0 errors, 0 warnings, processed in 1313 ms
2> Conversation -> C:\Users\ssailors.WPC\Documents\Visual Studio 2013\Projects\HelloPostSharp\Conversation\bin\Debug\Conversation.dll
3>------ Rebuild All started: Project: HelloPostSharp, Configuration: Debug Any CPU ------
4>------ Rebuild All started: Project: TestConversation, Configuration: Debug Any CPU ------
========== Rebuild All: 2 succeeded, 2 failed, 0 skipped ==========
My error list is empty. 0 Errors, 0 Warnings, 0 Messages.
Apparently, there is something I am not understanding about how to use unit testing with PostSharp. What am I missing?
Reboot to get the current assemblies in RAM
Related
I'm trying to write automated tests using Xamarin UI Test, within certain parts of those tests I need to know which platform they are running on i.e. Android or iOS.
I'm struggling to find a way of doing this, does anyone know of an API to do this or any other such trick?
Your tests class have a constructor like this:
[TestFixture(Platform.Android)]
[TestFixture(Platform.iOS)]
public class Tests
{
IApp app;
Platform platform;
public Tests(Platform platform)
{
this.platform = platform;
}
[SetUp]
public void BeforeEachTest()
{
app = AppInitializer.StartApp(platform);
}
}
Later on, in you test method you could do this:
[Test]
public void MyTest()
{
if (platform == Platform.Android)
{
// Do specific code here.
}
}
All, I am trying to add a Custom Action to my VS2010 SetUp Project. What I want to do is showing my custom win-form during the installation. And I want to show the custom win-form as a modal dialog so that the user can't ignore it during the installation.So far I inherit my install class from the System.windows.forms.IWin32Window .But I didn't know how to implement the get member public IntPtr Handle of the interface.
What I have done is below. please help to review it .thanks.
[RunInstaller(true)]
public partial class MyInstaller : System.Configuration.Install.Installer,IWin32Window
{
public MyInstaller ()
{
InitializeComponent();
}
[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
public override void Install(IDictionary stateSaver)
{
base.Install(stateSaver);
}
[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
protected override void OnAfterInstall(IDictionary savedState)
{
base.OnAfterInstall(savedState);
}
[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
public override void Commit(IDictionary savedState)
{
base.Commit(savedState);
Form frm = new frmSelectSource();
frm.ShowDialog(this);
}
[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
public override void Rollback(IDictionary savedState)
{
base.Rollback(savedState);
}
[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
public override void Uninstall(IDictionary savedState)
{
base.Uninstall(savedState);
}
[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
protected override void OnCommitted(IDictionary savedState)
{
base.OnCommitted(savedState);
}
public IntPtr Handle
{
get { throw new NotImplementedException(); }
}
}
I don't know if it is a right way to make it . If it is wrong or not possible to make it .please kindly to tell me . thanks.
This is one of the many reasons visual studio deployment projects were removed from VS2012. VDPROJ can only schedule custom actions in the deferred phase of the installation execute sequence. This is not an appropriate place to perform user interaction. Windows Installer is designed to perform UI first in the installation user interface sequence and then transfer control to the execute sequence. In a silent installation only the execute sequence is performed.
If you need custom UI you either need to go down a very complicated road of postbuild manipulations of the MSI to inject capabilities not exposed by VDPROJ or switch to a tool such as Windows Installer XML (WiX) of InstallShield Professional Edition that exposes this.
See the following for a better understanding of MSI:
Installation Phases and In-Script Execution Options for Custom Actions in Windows Installer
I am currently trying to complete this tutorial to get Prism to work with Spring.net.
After referencing Prism4 and Spring.Net through NuGet (or manualy referencing the assemblies), setting up a bootstrapper and running the application I get a "File or Assembly "System, Version=1.0.3300.0, Culture=neutral, ..." FileNotFoundException.
I am succesfully using Prism and Spring.Net in seperate projects. Above exception only occours in a project where Prism AND Spring.net is referenced. Spring.net is not even used in code or app.config. Searching various sites I could not find any informations on version issues or similar problems.
namespace PrismSpringSandbox {
/// <summary>
/// Interaktionslogik für "App.xaml"
/// </summary>
public partial class App : Application {
protected override void OnStartup(StartupEventArgs e) {
base.OnStartup(e);
Bootstrapper bootstrapper = new Bootstrapper();
bootstrapper.Run();
}
}
}
The Exception occours on "bootstrapper.Run()".
namespace HelloWorld {
public class Bootstrapper : UnityBootstrapper {
protected override DependencyObject CreateShell() {
Shell1 shell = new Shell1();
shell.Activate();
RegionManager.UpdateRegions();
shell.Show();
return shell;
}
protected override IModuleCatalog CreateModuleCatalog() {
DirectoryModuleCatalog catalog = new DirectoryModuleCatalog{ModulePath = #".\"};
//ModuleCatalog catalog =
// new ModuleCatalog().AddModule(typeof(HelloWorldModule.HelloWorldModule)).AddModule(
// typeof(SecondaryModule.SecondaryModule));
return catalog;
}
}
}
Maybe someone knows a solution for this problem when trying to use current Prism with current Spring.Net versions.
Ok, got it!
Problem was a reference to unity while referncing spring.net at the same time.
I would like to experiment with NServiceBus using ASP.NET MVC 3. I've got a solution with NServiceBus installed, plus NinjectMVC3 and NServiceBus.Ninject-CI. Trouble is, I have no idea how to setup NServiceBus stuff in the NinjectMVC3.cs file in App_Start.
Rather annoyingly I'm having trouble finding any examples of how to use NServiceBus.Ninject-CI (I hate it when people don't bother giving examples of how to use their stuff).
Can someone help me get started please?
Load a module like this into the kernel to provide access to the bus
public class NServiceBusModule : NinjectModule
{
public override void Load()
{
this.Bind<IBus>().ToConstant(this.CreateBus()).InSingletonScope();
}
private IBus CreateBus()
{
return NServiceBus.Configure.WithWeb()
.NinjectBuilder(this.Kernel)
... // put NServiceBus config here
.CreateBus()
.Start();
}
}
Read the NServiceBus documentation about how to configure NServiveBus:
http://docs.particular.net/nservicebus/containers/ninject
http://docs.particular.net/samples/web/asp-mvc-application/
Hopefully this will help someone. I had a lot of trouble finding sample code for getting ninject working within NServiceBus.
This code below works for me in place of the more common Castle version:
public class EndpointConfig : IConfigureThisEndpoint, AsA_Publisher, IWantCustomInitialization
{
#region IWantCustomInitialization Members
public void Init()
{
Configure
.With()
.NinjectBuilder(CreateKernel())
.XmlSerializer()
.MsmqTransport();
SetLoggingLibrary.Log4Net(XmlConfigurator.Configure);
}
protected IKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Load<MyCustomNinjectModule>();
return kernel;
}
#endregion
}
with the ninject module being the usual format, ie:
public class MyCustomNinjectModule : NinjectModule
{
public override void Load()
{
Bind(typeof(ILogger<>)).To(typeof(Log4NetLogger<>));
...
}
}
I get error
The resource cannot be found.
When I try to implement Ninject in my MVC-3 application. The problem appears to be coming from Global.asax during CreateKernel()
#region Inversion of Control
protected override IKernel CreateKernel()
{
return Container;
}
static IKernel _container;
public static IKernel Container
{
get
{
if (_container == null)
{
_container = new StandardKernel(new SiteModule());
}
return _container;
}
}
internal class SiteModule : NinjectModule
{
public override void Load()
{
bool MOCKDB = true;
//MOCKDB = false;//Stop Mocking
if (MOCKDB)
{
//Set up mock bindings
Bind<iItem>().To<LeadServiceMock>();
}
else
{
//Set up real bindings.
Bind<iItem>().To<LeadService>();
}
}
}
#endregion
If I take the code above out and revert back to System.Web.HttpApplication then things start to work again.
public class MvcApplication : NinjectHttpApplication//:System.Web.HttpApplication
{
I took this code from a previous implementation that I wrote that also still works. If I step through debug
protected override IKernel CreateKernel()
{
return Container;
}
I get an error in both the working program and this broken one:
Locating source for 'c:\Projects\Ninject\ninject.web.mvc\mvc3\src\Ninject.Web.Mvc\NinjectHttpApplication.cs'. Checksum: MD5 {b8 b2 52 86 ce 34 de 53 61 76 c9 df ff 65 8c 3f}
The file 'c:\Projects\Ninject\ninject.web.mvc\mvc3\src\Ninject.Web.Mvc\NinjectHttpApplication.cs' does not exist.
Looking in script documents for 'c:\Projects\Ninject\ninject.web.mvc\mvc3\src\Ninject.Web.Mvc\NinjectHttpApplication.cs'...
Looking in the projects for 'c:\Projects\Ninject\ninject.web.mvc\mvc3\src\Ninject.Web.Mvc\NinjectHttpApplication.cs'.
The file was not found in a project.
Looking in directory 'c:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\'...
Looking in directory 'c:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfc\src\mfc\'...
Looking in directory 'c:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfc\src\atl\'...
Looking in directory 'c:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfc\include\'...
The debug source files settings for the active solution indicate that the debugger will not ask the user to find the file: c:\Projects\Ninject\ninject.web.mvc\mvc3\src\Ninject.Web.Mvc\NinjectHttpApplication.cs.
The debugger could not locate the source file 'c:\Projects\Ninject\ninject.web.mvc\mvc3\src\Ninject.Web.Mvc\NinjectHttpApplication.cs'.
I suspect I did someting wrong in SiteModule. What am I doing wrong?
Replace Application_Start() with OnApplicationStarted()
//protected void Application_Start()
//{
// AreaRegistration.RegisterAllAreas();
// RegisterGlobalFilters(GlobalFilters.Filters);
// RegisterRoutes(RouteTable.Routes);
//}
protected override void OnApplicationStarted()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}