2009-11-18 3 views
2

Si vous avez un serveur qui héberge plusieurs points de terminaison du même type à des adresses différentes, est-il possible d'identifier l'adresse à partir de laquelle une demande particulière est venue? Dire à des fins de journalisation?Comment identifiez-vous le point de terminaison WCF qui a appelé votre opération?

exemple Contrived:

_serviceHost = new ServiceHost(
    typeof(Service), 
    new Uri("http://localhost:8000/SomeAddress")); 

_serviceHost.AddServiceEndpoint(
    typeof(IService), 
    new BasicHttpBinding(), 
    string.Empty); 

_serviceHost2 = new ServiceHost(
    typeof(Service), 
    new Uri("http://localhost:8000/SomeOtherAddress")); 

_serviceHost2.AddServiceEndpoint(
    typeof(IService), 
    new BasicHttpBinding(), 
    string.Empty); 

[ServiceContract()] 
public interface IService 
{ 
    [OperationContract] 
    void Operation(); 
} 

public class Service 
{ 

    void Operation() 
    {   
     //Which endpoint made this call? 
    } 
} 

Je préfère ne pas créer une instance singleton et le transmettre avec un id.

Répondre

4

Bien sûr, vous pouvez obtenir cette information de la OperationContext comme ceci:

EndpointAddress address = OperationContext.Current.EndpointDispatcher.EndpointAddress; 

Debug.WriteLine(address.Uri); 
+0

Un grand merci, cela fonctionnera. – RMK

Questions connexes