can't register objects using unity 2.0 - unity-container

I created a web service recently and am using unity to inject my object dependencies. My composition root is the Application_Start in the web services and am using the web.config to do my object to interface mappings. Everything was working fine, however after i loaded my project into tfs i keep getting an error stating that it cant resolve one of the interfaces. I removed the code to register my objects from the web.config and regsistered them in code instead to test and it all works fine. Any ideas what the problem is. Any ideas how i can troubleshoot this problem.
Before TFs :-
UnityContainer uContainer = new UnityContainer();
UnityConfigurationSection Section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
Section.Configure(uContainer, "CentralRepositoryContainer");
Application["uContainer"] = uContainer;
Amended code which works fine :-
UnityContainer uContainer = new UnityContainer();
uContainer.RegisterType<ICentralRepositoryLifeTimehelper, CentralRepositoryLifeTimeHelper>();
uContainer.RegisterType<IJobsHandler, JobsHandler>();
Application["uContainer"] = uContainer;

I don't know the problem, but to troubleshoot print out all the Unity containers registrations and see what's missing/changed.
Use the code sample from Retrieving Container Registration Information

Related

How do you bootstrap an asp.net application with Ninject for VB.NET?

I've used Nuget to pull in Ninject and the .Web and .Web.Common extensions. It's my understanding that I shouldn't need to touch the global.asax file and only need to register my modules in NinjectWebCommon, but this file is pulled in from NuGet as a C# file, and simply converting it causes errors.
Does anyone have a working NinjectWebCommon.vb file they can share?
I hit the same issue - a legacy VB.NET web application hosting Web API 2 -
"Overload resolution failed because no Public 'ToMethod' can be called
with these arguments"
I've managed to get around this by moving the creation of the Bootstrapper and setting the reference to the Kernel property from being inline, to being performed before the binding:
Dim k = New Bootstrapper().Kernel
kernel.Bind(Of Func(Of IKernel))().ToMethod(Function(ctx) Function() k)
kernel.Bind(Of IHttpModule)().To(Of HttpApplicationInitializationHttpModule)()
Hope this helps,
Scott

Mock object injected via AutoFixture with AutoMoq, unexpected behavior

I've just created my first test using AutoFixture. The SUT has the following constructor.
public LoggingService(
IClientDataProvider clientDataProvider, ... other dependencies...)
The test setup has the following code.
var fixture = new Fixture().Customize(new AutoMoqCustomization());
string ipAddress = "whatever";// fixture.CreateAnonymous<string>();
var clientDataProviderMock = fixture.Freeze<Mock<IClientDataProvider>>();
clientDataProviderMock.Setup(cdp => cdp.IpAddress).Returns(ipAddress);
LoggingService sut = fixture.CreateAnonymous<LoggingService>();
Now, when I examine the contents of sut, I see that the property IpAddress of the injected instance of IClientDataProvider returns null instead of "whatever".
What did I do wrong?
I copied the service and the necessary interfaces to an empty project and then the mocks worked as expected.
Interfaces that are types of constructor arguments of the service in the real project are defined in 3 separate assemblies that have further dependencies. I had several unexpected "Cannot load assembly" errors on the test start because several further assemblies were needed for those directly referenced assemblies. So it seems to be an assembly loading issue.
However, I tried a variation of the test with manual creation of the SUT instance with mock objects created manually using Moq and the test worked as expected
The solution was pretty surprising. When I was creating my unit test project I added the reference to Moq 4.0 first. AutoFixture was added later and since it seems to require Moq 3.1, I copied that dll directly to bin\Debug. However, the corresponding HintPath element in the project file still pointed to the 4.0 dll. As soon as I changed HintPath to point to the place where Moq 3.1 sits the test started to work properly. So Mark was right with his suggestion but the symptoms were quite different.

Error when using DependencyResolver for Controller instantiation with MVC 3

I'm using MVC 3 and using the following code when the application starts...
UnityContainer container = new UnityContainer();
new UnityMappings(container);
DependencyResolver.SetResolver(new UnityServiceLocator(container));
Now when the app runs I'm getting the following error (but only sometimes)...
Activation error occured while trying to get instance of type
IControllerFactory, key ""
Interestingly, if I continue with the web request, the website works normally.
Any ideas? I can't see what I'm doing differently from before when this worked fine.
Cheers, Ian.
MVC3 requests a lot more than just controllers from the DependencyResolver. For most of them MVC3 falls back to the default implementation in case the DependencyResolver does not return an instance.
In your case it requests the IControllerFactory which is unknown to your IoC container and it throws an exception which is caught by the UnityServiceLocator implementation and null is returned. MVC then falls back to the default controller factory.
Unlike other IoC containers Unity does not provide an optional TryResolve and therefore does not support a proper exceptionless implementation of the DependencyResolver.
I would suggest first looking through the config and make sure everything is correct there, then I would make sure I had all the assemblies needed for Unity referenced in the project. That error message may (in my experience) point to an error in config or a missing DLL, perhaps an assembly you want to load in the container or another DLL needed by a DLL loaded by Unity?
Hope this helps.

eventlog not working in an asp.net application

from an ASP.Net 3.5 web application, I'm trying to log messages to the Windows EventLog.
I first tried with the EntLib Logginh block, but when this failed I tried with the EventLog class directly. It failed too. They do not throw any exception... the just don't write the message. EntLib did write the message to a file, but not to the Windows EventLog.
Here is my code:
public static void LogMessage(string title, string message){
//EventLog log = new EventLog();
//log.Source = LOG_SOURCE;
//log.WriteEntry(message, EventLogEntryType.Error);
//EventLog.WriteEntry(LOG_SOURCE, message);
LogWriter writer = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
writer.Write(message);
}
I create the log & source in an installer class. Let me know if I should place that code here. The log is created correctly, since I can see it in the EventViewer. The source is created correctly, since I can see it in the "EventLog\MyLog" folder at the regedit.
I've been reading and there is an article stating following line could help:
EventLogPermission perm = new EventLogPermission(EventLogPermissionAccess.Administer, ".");
perm.PermitOnly();
but it didn't.
If it helps, my code structure is as follows:
Class library project (here is the LogMessage method)
Class Library project (here are the methods which catch exceptions and call LogMessage)
ASP Net web application project (web pages. This layer calls layer #2. Here is my installer class too)
Web setup project (this has custom actions pointing to web setup project output)
Could you please help to figure out what's happening???
Thanks
I found the following resource: "http://www.netframeworkdev.com/net-base-class-library/trouble-writing-to-eventlog-16723.shtml", so it seems it is not possibly to create custom logs from ASP... still investigating
Try giving the Network Service account the appropriate permissions

simple webservice code?

i write a simple webservice code in asp.net when i build the service and run the service it is working fine. when i try to access the webservice it is giving some problem , problem means i am not getting that method (webservice method). After completing writing the webserivce i take a asp.net page (.aspx) and in solution explorer i add a webservices and it is added successfully. but when i adding namespace it is not getting the service name ( i not able to add the namespace of websercice
I am not exactly sure what could be the problem but you should only need to do the following to use the web service:
// Look at what you named your web reference, in my example it is
// called MyWebService. Check your solution explorer for the actual name.
// This is the alias you should be using.
MyWebService.YourWebServiceName ws = new MyWebService.YourWebServiceName();
var result = ws.MyMethod(someparameter);

Resources