Wcf Web API 沙箱端点处理

发布于 2024-11-14 23:24:06 字数 1281 浏览 4 评论 0原文

我有一个将公开暴露的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失眠症患者 2024-11-21 23:24:06

如果它应该是一个真正的沙箱,那么您不希望这两个站点在同一进程中运行。我将部署两个网站,并让 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文