Wcf Web API 沙箱端点处理
我有一个将公开暴露的 api,并且有一个沙箱。我在 ResourceFactory 中编写了一些代码,因此 api.sandbox.whatever/whatever 可以工作,参数中的 sandbox=true 也可以工作,但这感觉像是一个巨大的黑客攻击。还有更好的方法吗?
这是我的代码:
public class NinjectResourceFactory : IResourceFactory
{
private readonly IKernel _productionKernel;
private readonly IKernel _sandboxKernel;
public NinjectResourceFactory()
{
_productionKernel = new StandardKernel(new QueryMasterModule());
_sandboxKernel = new StandardKernel(new QueryMasterModule(true));
}
public object GetInstance(Type serviceType, InstanceContext instanceContext, HttpRequestMessage request)
{
string uri = request.RequestUri.ToString();
if (uri.Contains(".sandbox."))
{
return _sandboxKernel.Get(serviceType);
}
else if (uri.Contains("sandbox=true"))
{
request.RequestUri = new Uri(uri.Replace("sandbox=true", ""));
return _sandboxKernel.Get(serviceType);
}
else
{
return _productionKernel.Get(serviceType);
}
}
public void ReleaseInstance(InstanceContext instanceContext, object service)
{
// todo do I need to implement this?
}
}
I have an api which will be publicly exposed and have a sandbox. I've written some code in my ResourceFactory so api.sandbox.whatever/whatever will work and also sandbox=true in the arguments will work but this feels like a giant hack. Any better ways to do it?
Here is my code:
public class NinjectResourceFactory : IResourceFactory
{
private readonly IKernel _productionKernel;
private readonly IKernel _sandboxKernel;
public NinjectResourceFactory()
{
_productionKernel = new StandardKernel(new QueryMasterModule());
_sandboxKernel = new StandardKernel(new QueryMasterModule(true));
}
public object GetInstance(Type serviceType, InstanceContext instanceContext, HttpRequestMessage request)
{
string uri = request.RequestUri.ToString();
if (uri.Contains(".sandbox."))
{
return _sandboxKernel.Get(serviceType);
}
else if (uri.Contains("sandbox=true"))
{
request.RequestUri = new Uri(uri.Replace("sandbox=true", ""));
return _sandboxKernel.Get(serviceType);
}
else
{
return _productionKernel.Get(serviceType);
}
}
public void ReleaseInstance(InstanceContext instanceContext, object service)
{
// todo do I need to implement this?
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果它应该是一个真正的沙箱,那么您不希望这两个站点在同一进程中运行。我将部署两个网站,并让 IIS 根据主机名决定哪一个。这样,沙箱将与生产隔离,这就是沙箱的目的。
If it's supposed to be a true sandbox then you don't want the two sites to run in the same process. I would deploy two web sites and let IIS decide which one based on host name. That way the sandbox will be isolated from production, which is the purpose of a sandbox.