[Castle.DynamicProxy]The created proxy object does not have properties of original object set correctly - inversion-of-control

I'm new here and hope my first question does not confuse anyone.
I am using Castle Windsor and Castle DynamicProxy together, in order to integrate AOP with IOC, so that I can implement a logging service (and auditing, etc.) with will intercept all services in my project.
I'm trying to proxy the resolved instance to get this done but with no luck. Because the proxy returned will have the properties of the resolved instance set to null. Following is the debugging info for reference (the PROBLEMATIC property is TimeService).
the resolved instance before proxied
The resolved instance before proxied.
the proxied object
The proxied object
Did I miss something or did i understand Castle DynamicProxy in a wrong way? I did search for any solutions but had no luck.
Any answers is highly appreciated. Thanks in advance.

You should use Windsor's built-in support for AOP during registration, not overriding WindsorContainer.Resolve otherwise you'll also run into other problems around releasing components and container lifetimes:
container.Register(
Component.For<ICalcService>()
.Interceptors(InterceptorReference.ForType<ReturnDefaultInterceptor>()).Last,
Component.For<ReturnDefaultInterceptor>()
);
See the docs for more info: https://github.com/castleproject/Windsor/blob/master/docs/registering-interceptors-and-proxyoptions.md

Related

The requested service 'Microsoft.AspNetCore.Hosting.Server.IServer' has not been registered

After updating to .net core 2.2 we have the following issue:
Autofac.Core.Registration.ComponentNotRegisteredException: 'The
requested service 'Microsoft.AspNetCore.Hosting.Server.IServer' has
not been registered. To avoid this exception, either register a
component to provide the service, check for service registration using
IsRegistered(), or use the ResolveOptional() method to resolve an
optional dependency.'
We are using preBuilder.Populate(services);.
Any ideas?
Thanks for your help
I had the same problem when following Microsoft migration guide for migrating from Core 2.1 to 2.2.
The problem might occur if you are not using WebHost.CreateDefaultBuilder to create the default web host builder, and you change in the CreateWebHostBuilder method of the Program class to call ConfigureKestrel instead of UseKestrel, as suggested in the migration guide.
As far as I understand if you use WebHost.CreateDefaultBuilder to create the default web host builder, it already calls UseKestrel which registers the IServer service. But you might get into some conflicts if also using UseIIS, so to avoid this problems there is a new ConfigureKestrel call that does not register the IServer. So I think that if you are not using WebHost.CreateDefaultBuilder then you still need to call UseKestrel or UseIIS explicitly.
Of course it might be something else that is causing the problems in your case, but I suspect that following the migration guide blindly (as I did) could cause problems for many developers out there.

How to register Autofac component per http request in web.config?

In MVC3 application I have my custom implementation of IPrincipal.
I want to register it in web.config and then inject it to constructors by Autofac.
Autofac XML-configuration has attribute "instance-scope" but it does not support "per-http-request".
Any ideas how can I register component per http scope in web.config?
I already tried different combinations of per-dependency, single-instance or per-lifetime-scope with ownership external but results are very buggy.
As Nicholas Blumhardt from Autofac team told me per-lifetime-scope should be used.
When running under ASP.NET it is equivalent to per-HTTP-request in practical terms.
As far as I can see from the source, registering per http request in XML configuration is not supported. As you mention, only the three instance-scope values (per-dependency, single-instance and per-lifetime-scope) are allowed.
Also, looking at how per-http-request scoping works, it uses the InstancePerMatchingLifetimeScope registration call with a httpRequest key. Again, there is no XML config way of configuring a "matching-lifetime-scope".
The way to go is configuration through code.

Use pojo as gwt RequestFactory proxy instead of interface

Is there any easy way to use a pojo as a request factory proxy and not an interface? The case is that I would like to reuse the actual value object as is without creating an interface describing it.
I do not that this can not be done out of the box. GWT fails to compile with an error regarding non getter/setter methods insite the "proxy" class.
This not possible, by design. See this previous StackOverflow answer.

Using Castle Windsor to inject IRailsEngineContext

Issue
I am using Castle Windsor as an IoC container in a Castle Monorail project. I would like to inject the current instance of IRailsEngineContext into an object being resolved from the container in a controller.
Context
The object I would like inject the rails context into would be used to wrap the session object for the purpose of retaining the ids of previously viewed records. It would then be referenced to ensure that they aren't viewed again.
Alternate Solutions
I could pass the context to the methods with each call or inject it manually, but it would be nice to inject it directly from the container.
Question
I can't think of a way to inject the context within the container. Is there a way to do this? Does this even make sense?
Container.Register(
Component.For<IRailsEngineContext>()
.UsingFactoryMethod(()=>MonoRailHttpHandler.CurrentContext)
.LifeStyle.Transient
);
IRailsEngineContext - that's from an old version of MonoRail I guess. I'd advise you move to a newer one, the sooner the better.

How do forwarding types apply to IoC?

I'm still learning Castle Windsor and I see that it supports forwarded types - a term I was unfamiliar with.
Quick Google search and I found this article that explains it pretty throughly however I still don't understand how this applies to IoC or how I would use them from a container and for what reason.
Can anyone explain?
Type forwarding is a way to move type from one assembly to another without introducing breaking changes it has nothing to do with IoC. So you should not worry about this feature :)
Oh man, just did a google search on 'Castle Windsor "Forwarded Types"' and got redirected right back to SO:
Using Castle Windsor configuration files is it possible to delegate to another item declaration?
and from there:
What are “ForwardedTypes” in the context of Castle Windsor component registration?
Maybe Jeff and the team should just give up on trying to have their own search and use google's.