以编程方式设置InstanceContextMode

Ant*_*ott 40 wcf-web-api

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

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
Run Code Online (Sandbox Code Playgroud)

...编程?

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

我正在使用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
Run Code Online (Sandbox Code Playgroud)

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

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));
}
Run Code Online (Sandbox Code Playgroud)

所以,我需要以编程InstanceContextMode方式在"集成测试模式"中设置when,即 - 在我的帮助器类中.

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

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

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

Ant*_*ott 56

我正在回答我自己的问题,因为我已经在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));
}
Run Code Online (Sandbox Code Playgroud)

我改变了这个......

_host = serviceInstance == null
            ? new HttpServiceHost(typeof (TApi), baseUri)
            : new HttpServiceHost(serviceInstance, baseUri);
Run Code Online (Sandbox Code Playgroud)

......对......

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);
Run Code Online (Sandbox Code Playgroud)

  • 如果它只包含解决方案而不是完整代码,那么答案会更容易阅读. (4认同)