I have singleton object hosted under IIS. I can call any method in Singleton object without any problem. One method of singleton object returns a reference to another class object. When I try to call a method on this referenced object I get a message "Cannot connect to the server" e.g.
public class TestClass : MarshalByRefObject
{
public void TestMethod()
{
}
}
public class SingletonClass : MarshalByRefObject
{
public TestClass Test()
{
return new TestClass();
}
}
//Client calls example
SingletonClass c = Activating SingletonClass object here
var obj = c.Test(); //Succeeds
obj.TestMethod(); //Hangs and then I get msg "Cannot connect to the Server"
Please not that if I run claint and server on same machine there is no problem. If I run client on separate machine then I get this issue. The surprise is that one call succeeds and the other one fails. Both classes in same dll. What could be the problem? Any suggestion to look at security/permission settings?
Related
I have just implemented output caching of my Asp.Net Web API Controllers using StrathWeb's library connecting to the StackExchange.Redis library connecting through to an Azure-hosted Redis Cache.
I have written a custom class that implements the StrathWeb IApiOutputCache interface and calls the equivalent StackExchange methods. This is registered as the cache output provder in Global.asax.cs.
Here's an example of usage:
public class MyApiController : ApiController
{
private const int FIFTEEN_MINUTES_IN_SECONDS = 900;
[CacheOutput(ClientTimeSpan = FIFTEEN_MINUTES_IN_SECONDS, ServerTimeSpan = FIFTEEN_MINUTES_IN_SECONDS)]
async public Task<Data> GetAsync(int param1, string param2)
{
return await GetExpensiveData();
}
[Serializable]
public class Data
{
// Members omitted for brevity
}
}
When a call is made to the api endpoint I can see that the framework correctly calls all the required methods on my IApiOutputCache class: Contains, Set and Get. However, even when a cached copy is found and returned, the GetExpensiveData() method is always run and the 'fresh' data returned.
No errors are thrown. The cache seems to be working. Yet, my expensive code is always called.
Thanks for your help :).
Problem solved. I was incorrectly calling into Redis from my IApiOutputCache class.
Before...
public class AzureRedisApiOutputCache : IApiOutputCache
{
public object Get(string key)
{
return AzureRedisCache.Instance.GetDatabase().StringGet(key);
}
}
After...
public class AzureRedisApiOutputCache : IApiOutputCache
{
public object Get(string key)
{
// Call the extension method that also performs deserialization...
return AzureRedisCache.Instance.GetDatabase().Get(key);
}
}
public static class RedisDatabaseExtensions
{
public static object Get(this IDatabase cache, string key)
{
return Deserialize<object>(cache.StringGet(key));
}
}
This confused me for some time as the CacheOutput framework never reported an error. It just silently failed and fell back to the controller method.
I'm using unity in my MVC app
I have the following RegisterTypes method within my Bootstapper.cs file:
public static void RegisterTypes(IUnityContainer container)
{
container.RegisterType<AccountController>(new InjectionConstructor());
container.RegisterType<IModelContext, ModelContext>();
container.RegisterType<IModelRepository, ModelRepository>();
}
I have the following controller:
public class APIScoresController : ApiController
{
private IModelRepository _repo;
public APIScoresController(IModelRepository repo)
{
_repo = repo;
}
public IEnumerable<Result> Get()
{
return _repo.GetResults();
}
}
I have the following Model Repo:
public class ModelRepository : IModelRepository
{
ModelContext _ctx;
public ModelRepository(ModelContext ctx)
{
_ctx = ctx;
}
public IQueryable<DomainClasses.Result> GetResults()
{
return _ctx.Results;
}
}
When I try to execute the GET on the APIScoresController I get the following exception:
An error occurred when trying to create a controller of type 'APIScoresController'. Make sure that the controller has a parameterless public constructor
I would have expected unity to create the required ModelContext and ModelRepository objects. Any ideas why it's not doing this?
Problem caused by web api registration needing a different version of system.web.http. I was trying to add web api to an existing mvc5 app - bad idea! I entered a form of dll hell that I hadn't experienced since days of VB6 COM. In the end my solution was to create a new solution with a web api project then retro fit the mvc project.
I'm trying to get the following scenario using autofac but I'm not sure how my code will be built to get this up & running.
I have a repository class, this repository class needs to get a project key (string) on initialization (constructor). I want to instantiate this repository in initialization of my "Initialize" method provided to my by Web Api, because the project key will be available in my route.
so instead of calling "new ProductRepository(projectKey)", I want to use Autofac. Can someone point me in the right direction? I didn't find any way to send in specific data to the container in web api, since the container/builder is only available in the appStart.
Should I make the container available as a singleton so that I can approach it, or is this bad practice?
in your initialization code:
var builder = new ContainerBuilder();
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
var container = builder.Build();
var resolver = new AutofacWebApiDependencyResolver(container);
config.DependencyResolver = resolver;
in your controller:
public class MyController : ApiController
{
public MyController(IComponentContext container)
{
var key = new NamedParameter("projectKey", "keyFromRoute");
var repository = container.Resolve<ProductRepository>(key);
}
}
That should do it.
There is a nuget package that provides a DependencyResolver for WebApi that integrates with AutoFac. Create the DependencyResolver, assign it to the config, register your controllers in the autofac container.
I'm making some assumptions because you didn't provide your code, but I think you have something like this:
public class ProductRepository
{
public ProductRepository(DbContext dbContext, int projectKey)
{
}
}
public class SomeController : Controller
{
private readonly Func<int, ProductRepository> _repoFactory;
public SomeController(Func<int, ProductRepository> repoFactory)
{
_repoFactory = repoFactory;
}
public void DoStuff(int projectKey)
{
var repo = _repoFactory(projectKey);
repo.DoStuff();
}
}
public class RepositoryModule : Module
{
public override Load(ContainerBuilder builder)
{
builder.RegisterType<ProductRepository>();
}
}
I'm using Castle Windsor and DynamicProxy to implement persistence Lazy Loading from scratch (I know NHibernate could be an option etc.) I have implemented a custom component activator to always instantiate my business classes as proxies. I found that the default mixin proxies automatically created when using interceptors were not being used when class methods are called from inside the class itself, which was a problem. So I inherited DefaultComponentActivator and overriding CreateInstance() I'm calling CreateClassProxy() to get a proxy that inherits from the business class, which in that respect works fine.
Now I was expecting this 'ProxyComponentActivator' activator of mine to be instantiated by Castle only once, but a new instance is being created for each class type. Is that correct?
Current registration is like this:
public void Install(
IWindsorContainer container,
Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store) {
container.Register(
Classes
.FromAssemblyContaining(typeof(OneOfMyBusinessClasses))
.InNamespace(typeof(OneOfMyBusinessClasses).Namespace)
.WithService.DefaultInterfaces()
.Configure(reg => reg.Activator<ProxyComponentActivator>())
.LifestyleTransient(),
etc.
);
);
The activator implementation is the following:
public class ProxyComponentActivator : DefaultComponentActivator
{
protected Castle.DynamicProxy.ProxyGenerator ProxyGenerator { get; set; }
protected PersistenceInterceptor PersistenceInterceptor { get; set; }
public ProxyComponentActivator(ComponentModel model, Castle.MicroKernel.IKernelInternal kernel, ComponentInstanceDelegate onCreation, ComponentInstanceDelegate onDestruction)
: base(model, kernel, onCreation, onDestruction)
{
this.ProxyGenerator = kernel.Resolve<Castle.DynamicProxy.ProxyGenerator>();
this.PersistenceInterceptor = kernel.Resolve<PersistenceInterceptor>();
}
protected override object CreateInstance(CreationContext context, ConstructorCandidate constructor, object[] arguments) //, Type[] signature)
{
object instance;
Type implType = this.Model.Implementation;
ProxyGenerationOptions p = new ProxyGenerationOptions();
IPersistent ip = new Persistent();
p.AddMixinInstance(ip);
try
{
instance = this.ProxyGenerator.CreateClassProxy(implType, null, p, arguments, this.PersistenceInterceptor);
}
catch
{
throw new ComponentActivatorException("ComponentActivator: could not proxy " + implType.FullName, Model);
}
return instance;
}
}
I have also tried to register the activator like this, to no avail...
Component.For<ProxyComponentActivator>()
.ImplementedBy<ProxyComponentActivator>()
.LifestyleSingleton()
Thanks in advance for any help,
Luis
Every component in Windsor will get its own activator instance
I only recently completed a unit on software patterns and am now attempting to comprehend the PureMVC framework. One thing has got my stumped however, something which is simple to the gurus here.
I'm attempting to create an instance of the singleton Facade class. In the constructor, the comments state:
This IFacade implementation is a Singleton, so you should not call the constructor directly, but instead call the static Singleton Factory method Facade.Instance
How can you call the instance method when the Facade object has not even been created?
The Facade.Instance method looks like this:
public static IFacade Instance
{
get
{
if (m_instance == null)
{
lock (m_staticSyncRoot)
{
if (m_instance == null) m_instance = new Facade();
}
}
return m_instance;
}
}
You are accessing a static property. Static properties are part of the class definition, not class instances. To access a static member (property, field, method), simply use the class name dot member:
var myFacade = SomeClass.Instance;