2012-01-17 11 views
34

Czy istnieje sposób, aby to zrobić ...Programowo ustawiony InstanceContextMode

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] 

... programowo?

Powodem jest to, że chcę przekazać instancję mojej usługi bezpośrednio do mojej klasy hostingu samoprowadzącego podczas testowania integracji mojej usługi.

Używam Castle Windsor do tworzenia wszystkich moich obiektów, co działa dobrze podczas korzystania z witryny testowej. Ale pojawia się następujący błąd, gdy próbuję użyć mojego HttpWebService klasy pomocnika ...

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 

Jest to konstruktor mojej klasy pomocnika ...

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

Więc muszę ustawić programowo InstanceContextMode w "trybie testu integracji", tj. - w mojej klasie pomocnika.

myślę, że trzeba zrobić coś takiego ...

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

Każda pomoc/wskazówki byłoby świetnie jak ja naprawdę zatrzymany w tej sprawie.

Odpowiedz

47

Odpowiadam na własne pytanie, ponieważ znalazłem rozwiązanie w innym answer na stackoverflow, i myślę, że stackoverflow jest doskonałym miejscem do wyszukiwania bez konieczności zadawania pytania, więc mam nadzieję, że dodam do tego bogactwa odpowiadając na moje własne pytanie z linkiem do drugiej odpowiedzi, a nie tylko zamykając moje własne pytanie.

Mój kod wygląda teraz tak ...

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

Zmieniłem to ...

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

... do tego ...

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

odpowiedź będzie łatwiejsza do odczytania, jeśli spowoduje ponowne wyświetlenie tylko rozwiązania zamiast pełnego kodu. – Offler

1

Nawet oryginalna odpowiedź zawiera rozwiązanie Jest to prosta odpowiedź na pytanie

ServiceHost host = new ServiceHost(typeof(YourService)); //Or get the Servicehost 
((ServiceBehaviorAttribute)host.Description. 
Behaviors[typeof(ServiceBehaviorAttribute)]).InstanceContextMode 
= InstanceContextMode.Single; 
Powiązane problemy