added SSCLI 1.0
[windows-sources.git] / sdk / samples / WCFSamples / TechnologySamples / Extensibility / Channels / MessageInterceptor / CS / service / service.cs
blobca5f19f86512077ef9fe033bafc7fb82c02f717c
2 // Copyright (c) Microsoft Corporation. All Rights Reserved.
4 using System;
6 using System.ServiceModel.Channels;
7 using System.ServiceModel;
8 using System.Configuration;
9 using System.Collections;
11 namespace Microsoft.ServiceModel.Samples
13 // Create a service contract and define the service operations.
14 [ServiceContract]
15 public interface ISampleContract
17 [OperationContract(IsOneWay = true)]
18 void ReportWindSpeed(int speed);
21 // The Service implementation implements your service contract.
22 public class SampleService : ISampleContract
24 public void ReportWindSpeed(int speed)
26 if (speed >= 64)
28 Console.WriteLine("Dangerous wind detected! Reported speed (" + speed + ") is greater than 64 kph.");
33 class DroppingServerElement : InterceptingElement
35 protected override ChannelMessageInterceptor CreateMessageInterceptor()
37 return new DroppingServerInterceptor();
41 public class DroppingServerInterceptor : ChannelMessageInterceptor
43 int messagesSinceLastReport = 0;
44 readonly int reportPeriod = 5;
46 public DroppingServerInterceptor() { }
48 public override void OnReceive(ref Message msg)
50 if (msg.Headers.FindHeader("ByPass", "urn:InterceptorNamespace") > 0)
52 if (++messagesSinceLastReport == this.reportPeriod)
54 Console.WriteLine(reportPeriod + " wind speed reports have been received.");
56 return;
58 // Drop incoming Message if the Message does not have the special header
59 msg = null;
62 public override ChannelMessageInterceptor Clone()
64 return new DroppingServerInterceptor();
68 class Service
70 static void Main(string[] args)
72 ServiceHost serviceHost = new ServiceHost(typeof(SampleService));
73 bool success = false;
75 try
77 serviceHost.Open();
79 System.Console.WriteLine("Press ENTER to exit.");
80 System.Console.ReadLine();
82 serviceHost.Close();
83 success = true;
85 finally
87 if (!success)
88 serviceHost.Abort();