wcf-web-api - 以编程方式设置 InstanceContextMode

标签 wcf-web-api

有没有办法做到这一点...

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]

...以编程方式?

原因是我想在集成测试我的服务时将我的服务实例直接传递到我的自托管帮助程序类中。

我使用 CaSTLe Windsor 创建所有对象,在使用测试网站时效果很好。但是当我尝试使用 HttpWebService 帮助程序类时出现以下错误...

System.InvalidOperationException was unhandled by user code
  Message=In order to use one of the ServiceHost constructors that takes a service instance, the InstanceContextMode of the service must be set to InstanceContextMode.Single.  This can be configured via the ServiceBehaviorAttribute.  Otherwise, please consider using the ServiceHost constructors that take a Type argument.
  Source=System.ServiceModel

这是我的辅助类的构造函数...

public HttpWebService(string baseUri, string acceptType, TApi serviceInstance = null)
{
    _baseUri = baseUri;
    _acceptType = acceptType.ToLower();

    _host = serviceInstance == null
                ? new HttpServiceHost(typeof (TApi), baseUri)
                : new HttpServiceHost(serviceInstance, baseUri);
    _host.Open();
    _client = new HttpClient();
    _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(_acceptType));
}

因此,我需要在“集成测试模式”(即在我的帮助程序类中)下以编程方式设置InstanceContextMode

我想我需要做这样的事情......

if (serviceInstance != null)
{
    _host = new HttpServiceHost(serviceInstance, baseUri);
    var whatDoIDoNow = null;
    _host.Description.Behaviors.Add(whatDoIDoNow);
}

任何帮助/指导都会很棒,因为我真的很困惑。

最佳答案

我正在回答我自己的问题,因为我在另一个 answer 中找到了解决方案在 stackoverflow 上,我认为 stackoverflow 是一个搜索的好地方,甚至不需要问问题,所以希望我能通过回答我自己的问题并链接到其他答案来增加这种丰富性,而不仅仅是结束我自己的问题。

我的代码现在看起来像这样......

public HttpWebService(string baseUri, string acceptType, TApi serviceInstance = null)
{
    _baseUri = baseUri;
    _acceptType = acceptType.ToLower();

    if (serviceInstance != null)
    {
        _host = new HttpServiceHost(serviceInstance, baseUri);
        var behaviour = _host.Description.Behaviors.Find<ServiceBehaviorAttribute>();
        behaviour.InstanceContextMode = InstanceContextMode.Single;
    }
    _host = _host ?? new HttpServiceHost(typeof (TApi), baseUri);

    _host.Open();
    _client = new HttpClient();
    _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(_acceptType));
}

我改变了这个...

_host = serviceInstance == null
            ? new HttpServiceHost(typeof (TApi), baseUri)
            : new HttpServiceHost(serviceInstance, baseUri);

...对此...

if (serviceInstance != null)
{
    _host = new HttpServiceHost(serviceInstance, baseUri);
    var behaviour = _host.Description.Behaviors.Find<ServiceBehaviorAttribute>();
    behaviour.InstanceContextMode = InstanceContextMode.Single;
}
_host = _host ?? new HttpServiceHost(typeof (TApi), baseUri);

关于wcf-web-api - 以编程方式设置 InstanceContextMode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8902203/

相关文章:

c# - ASP.NET WebApi SelfHost 服务在 HTTP URL 注册时失败

asp.net-mvc-3 - 具有 EF Code First 的 WebApi 在具有父子关系时会生成错误

c# - 从分段文件上传中读取内容到内存

.net - Autofac 和 IDisposable 接口(interface)

asp.net-mvc - 我可以在同一个 AppHarbor 站点上运行 MVC 应用程序和 WCF Web Api 吗?

c# - 在 WCF Webapi 站点上验证 Android 客户端?

WCF WebAPI 客户端不知道服务器类型

wcf-web-api - CaSTLe.ActiveRecord.dll 与 ASP.NET MVC 4 Web API 不兼容