以编程方式设置InstanceContextMode [英] Programmatically set InstanceContextMode

查看:97
本文介绍了以编程方式设置InstanceContextMode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there a way to do this ...

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]

...以编程方式?

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

The reason is that I want to pass in an instance of my service directly into my self-hosting helper class when integration testing my service.

我正在使用温莎城堡创建我的所有对象,使用测试网站时可以正常工作.但是当我尝试使用HttpWebService helper类时出现以下错误...

I'm using Castle Windsor to create all my objects, which works fine when using the test web site. But I get the following error when I try to use my HttpWebService helper class ...

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

这是我的帮助器类的构造函数...

This is the constructor of my helper class ...

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.

So, I need to programmatically set the InstanceContextMode when in "integration test mode", ie - in my helper class.

我认为我需要做这样的事情...

I think I need to do something like this ...

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

任何帮助/指导都将是很棒的,因为我真的很坚持.

Any help/guidance would be great as I'm really stuck on this.

推荐答案

我正在回答自己的问题,因为我在另一个

I'm answering my own question as I've found a solution in another answer on stackoverflow, and I think stackoverflow is a great place to search without even having to ask a question, so hopefully I will add to that richness by answering my own question with a link to the other answer and not just closing my own question.

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

My code now looks like this ...

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);

这篇关于以编程方式设置InstanceContextMode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆