示例#1
0
    public static void Test()
    {
        string         baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        WebServiceHost host        = new WebServiceHost(typeof(Service), new Uri(baseAddress));

        host.Open();
        Console.WriteLine("Host opened");
        WebChannelFactory <ICalculator> factory = new WebChannelFactory <ICalculator>(new Uri(baseAddress));
        ICalculator proxy = factory.CreateChannel();

        using (new OperationContextScope((IContextChannel)proxy))
        {
            WebOperationContext.Current.OutgoingRequest.Headers.Add("referer", "http://stackoverflow.com");
            WebOperationContext.Current.OutgoingRequest.Headers.Add("user-agent", "Mozilla/5.0");
            Console.WriteLine("Add: {0}", proxy.Add(33, 55));
            Console.WriteLine();
        }
        using (new OperationContextScope((IContextChannel)proxy))
        {
            WebOperationContext.Current.OutgoingRequest.Headers.Add("referer", "http://stackoverflow.com");
            WebOperationContext.Current.OutgoingRequest.Headers.Add("user-agent", "Mozilla/5.0");
            Console.WriteLine("Subtract: {0}", proxy.Subtract(44, 33));
            Console.WriteLine();
        }
        MyWebClient client = new MyWebClient(new Uri(baseAddress));

        client.AddOutgoingHeader("referer", "http://stackoverflow.com");
        client.AddOutgoingHeader("user-agent", "Mozilla/5.0");
        Console.WriteLine("Add (via client): {0}", client.Add(44, 77));
        Console.WriteLine();
        client.AddOutgoingHeader("referer", "http://stackoverflow.com/another");
        client.AddOutgoingHeader("user-agent", "Mozilla/5.0-b");
        Console.WriteLine("Add (via client): {0}", client.Subtract(44, 77));
        Console.WriteLine();
        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }