2011-09-09 2 views

답변

6

추적에 로그온되어 있지 않습니다. 하지만 런타임 중에 정보를 찾을 수 있습니다 (OperationContext).

public class StackOverflow_7360920 
{ 
    [ServiceContract] 
    public interface ITest 
    { 
     [OperationContract] 
     int Add(int x, int y); 
    } 
    //[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] 
    public class Service : ITest 
    { 
     public Service() 
     { 
      Console.WriteLine(OperationContext.Current.Host.Description.Behaviors.Find<ServiceBehaviorAttribute>().InstanceContextMode); 
     } 
     public int Add(int x, int y) 
     { 
      return x + y; 
     } 
    } 
    static Binding GetBinding() 
    { 
     BasicHttpBinding result = new BasicHttpBinding(); 
     return result; 
    } 
    public static void Test() 
    { 
     string baseAddress = "http://" + Environment.MachineName + ":8000/Service"; 
     ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress)); 
     host.AddServiceEndpoint(typeof(ITest), GetBinding(), ""); 
     host.Open(); 
     Console.WriteLine("Host opened"); 

     ChannelFactory<ITest> factory = new ChannelFactory<ITest>(GetBinding(), new EndpointAddress(baseAddress)); 
     ITest proxy = factory.CreateChannel(); 

     Console.WriteLine(proxy.Add(3, 5)); 

     ((IClientChannel)proxy).Close(); 
     factory.Close(); 

     Console.Write("Press ENTER to close the host"); 
     Console.ReadLine(); 
     host.Close(); 
    } 
} 
관련 문제