diff --git a/docs/framework/wcf/samples/announcements-sample.md b/docs/framework/wcf/samples/announcements-sample.md index a34c7452d4d40..0e99416220dcd 100644 --- a/docs/framework/wcf/samples/announcements-sample.md +++ b/docs/framework/wcf/samples/announcements-sample.md @@ -9,7 +9,7 @@ This sample shows how to use the Announcement functionality of the Discovery fea ## Service This project contains a self-hosted calculator service. In the `Main` method, a service host is created and a service endpoint is added to it. Next, a is created. To enable announcements, an announcement endpoint must be added to the . In this case a standard endpoint, using UDP multicast is added as the announcement endpoint. This broadcasts the announcements over a well-known UDP address. -``` +```csharp Uri baseAddress = new Uri("http://localhost:8000/" + Guid.NewGuid().ToString()); // Create a ServiceHost for the CalculatorService type. @@ -33,7 +33,7 @@ using (ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), base ## Client In this project, note that the client hosts an . Furthermore, two delegates are registered with events. These events dictate what the client does when online and offline announcements are received. -``` +```csharp // Create an AnnouncementService instance AnnouncementService announcementService = new AnnouncementService(); @@ -44,7 +44,7 @@ announcementService.OfflineAnnouncementReceived += OnOfflineEvent; The `OnOnlineEvent` and `OnOfflineEvent` methods handle the hello and bye announcement messages respectively. -``` +```csharp static void OnOnlineEvent(object sender, AnnouncementEventArgs e) { Console.WriteLine(); diff --git a/docs/framework/wcf/samples/asmx-client-with-a-wcf-service.md b/docs/framework/wcf/samples/asmx-client-with-a-wcf-service.md index e2a3326984aa3..411f80dbd4289 100644 --- a/docs/framework/wcf/samples/asmx-client-with-a-wcf-service.md +++ b/docs/framework/wcf/samples/asmx-client-with-a-wcf-service.md @@ -13,7 +13,7 @@ This sample demonstrates how to create a service using Windows Communication Fou The service implements an `ICalculator` contract as defined in the following code. -``` +```csharp [ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples"), XmlSerializerFormat] public interface ICalculator { @@ -77,7 +77,7 @@ wsdl /n:Microsoft.ServiceModel.Samples /o:generatedClient.cs /urlkey:CalculatorS The client implementation constructs an instance of the typed proxy to begin communicating with the service. -``` +```csharp // Create a client to the CalculatorService. using (CalculatorService client = new CalculatorService()) { @@ -114,7 +114,7 @@ Console.ReadLine(); When you run the sample, the operation requests and responses are displayed in the client console window. Press ENTER in the client window to shut down the client. -``` +```console Add(100,15.99) = 115.99 Subtract(145,76.54) = 68.46 Multiply(9,81.25) = 731.25 diff --git a/docs/framework/wcf/samples/authorizing-access-to-service-operations.md b/docs/framework/wcf/samples/authorizing-access-to-service-operations.md index 55c9d68fbb515..e5375386d497b 100644 --- a/docs/framework/wcf/samples/authorizing-access-to-service-operations.md +++ b/docs/framework/wcf/samples/authorizing-access-to-service-operations.md @@ -35,7 +35,7 @@ This sample demonstrates how to use the [\](../../../../do The is applied to each operation to require the caller to be part of the Windows administrators group, as shown in the following sample code. -``` +```csharp [PrincipalPermission(SecurityAction.Demand, Role = "Builtin\\Administrators")] public double Add(double n1, double n2) diff --git a/docs/framework/wcf/samples/basic-data-contract.md b/docs/framework/wcf/samples/basic-data-contract.md index 4d8105ba14ac3..02847f4a996d2 100644 --- a/docs/framework/wcf/samples/basic-data-contract.md +++ b/docs/framework/wcf/samples/basic-data-contract.md @@ -15,7 +15,7 @@ This sample demonstrates how to implement a data contract. Data contracts allow The service contract for this service uses complex numbers, as shown in the following sample code. -``` +```csharp // Define a service contract. [ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")] public interface ICalculator @@ -33,7 +33,7 @@ public interface ICalculator The and attributes have been applied to the definition of the `ComplexNumber` class to indicate which fields of the class can be passed over the wire between the client and the service, as shown in the following sample code. -``` +```csharp [DataContract(Namespace="http://Microsoft.ServiceModel.Samples")] public class ComplexNumber { @@ -52,7 +52,7 @@ public class ComplexNumber The service implementation calculates and returns the appropriate result, accepting and returning numbers of the `ComplexNumber` type. -``` +```csharp // This is the service class that implements the service contract. public class CalculatorService : ICalculator { @@ -91,16 +91,20 @@ public class CalculatorService : ICalculator The client implementation also uses complex numbers. Both the service contract and the data contract are defined in the source file generatedClient.cs, which is generated by the [ServiceModel Metadata Utility Tool (Svcutil.exe)](../../../../docs/framework/wcf/servicemodel-metadata-utility-tool-svcutil-exe.md) from service metadata. -``` +```csharp // Create a client. DataContractCalculatorClient client = new DataContractCalculatorClient(); // Call the Add service operation. -ComplexNumber value1 = new ComplexNumber(); -value1.Real = 1; -value1.Imaginary = 2; -ComplexNumber value2 = new ComplexNumber(); -value2.Real = 3; -value2.Imaginary = 4; +ComplexNumber value1 = new ComplexNumber() + { + Real = 1, + Imaginary = 2 + }; +ComplexNumber value2 = new ComplexNumber() + { + Real = 3, + Imaginary = 4 + }; ComplexNumber result = proxy.Add(value1, value2); Console.WriteLine("Add({0} + {1}i, {2} + {3}i) = {4} + {5}i", value1.Real, value1.Imaginary, value2.Real, value2.Imaginary, @@ -111,7 +115,7 @@ Console.WriteLine("Add({0} + {1}i, {2} + {3}i) = {4} + {5}i", When you run the sample, the requests and responses of the operation are displayed in the client console window. Press ENTER in the client window to shut down the client. -``` +```console Add(1 + 2i, 3 + 4i) = 4 + 6i Subtract(1 + 2i, 3 + 4i) = -2 + -2i Multiply(2 + 3i, 4 + 7i) = -13 + 26i diff --git a/docs/framework/wcf/samples/basic-sample.md b/docs/framework/wcf/samples/basic-sample.md index 06278b24607a0..b9dcfd82534af 100644 --- a/docs/framework/wcf/samples/basic-sample.md +++ b/docs/framework/wcf/samples/basic-sample.md @@ -12,7 +12,7 @@ This sample shows how to make a service discoverable and how to search for and c ## Service This is a simple calculator service implementation. The discovery related code can be found in `Main` where a is added to the service host and a is added as shown in the following code. -``` +```csharp using (ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), baseAddress)) { serviceHost.AddServiceEndpoint(typeof(ICalculatorService), new diff --git a/docs/framework/wcf/samples/concurrency.md b/docs/framework/wcf/samples/concurrency.md index 0c60469457298..90d012f26d354 100644 --- a/docs/framework/wcf/samples/concurrency.md +++ b/docs/framework/wcf/samples/concurrency.md @@ -26,7 +26,7 @@ The Concurrency sample demonstrates using the )]` attribute as shown in the code sample that follows. By changing which lines are commented out, you can experiment with the `Single` and `Multiple` concurrency modes. Remember to rebuild the service after changing the concurrency mode. -``` +```csharp // Single allows a single message to be processed sequentially by each service instance. //[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single, InstanceContextMode = InstanceContextMode.Single)] diff --git a/docs/framework/wcf/samples/concurrencymode-reentrant.md b/docs/framework/wcf/samples/concurrencymode-reentrant.md index a2ac73819c02f..42c0de141f923 100644 --- a/docs/framework/wcf/samples/concurrencymode-reentrant.md +++ b/docs/framework/wcf/samples/concurrencymode-reentrant.md @@ -8,7 +8,7 @@ This sample demonstrates the necessity and implications of using ConcurrencyMode The contract defined is a duplex contract with the `Ping` method being implemented by the service and the callback method `Pong` being implemented by the client. A client invokes the server's `Ping` method with a tick count thereby initiating the call. The service checks whether the tick count is not equal to 0 and then invokes the callbacks `Pong` method while decrementing the tick count. This is done by the following code in the sample. -``` +```csharp public void Ping(int ticks) { Console.WriteLine("Ping: Ticks = " + ticks); @@ -22,7 +22,7 @@ public void Ping(int ticks) The callback's `Pong` implementation has the same logic as the `Ping` implementation. That is, it checks whether the tick count is not zero and then invokes the `Ping` method on the callback channel (in this case, it is the channel that was used to send the original `Ping` message) with the tick count decremented by 1. The moment the tick count reaches 0, the method returns thereby unwrapping all the replies back to the first call made by the client that initiated the call. This is shown in the callback implementation. -``` +```csharp public void Pong(int ticks) { Console.WriteLine("Pong: Ticks = " + ticks); @@ -49,7 +49,7 @@ public void Pong(int ticks) ## Demonstrates To run the sample, build the client and server projects. Then open two command windows and change the directories to the \\CS\Service\bin\debug and \\CS\Client\bin\debug directories. Then start the service by typing `service.exe` and then invoke the Client.exe with the initial value of ticks passed as an input argument. A sample output for 10 ticks is shown. -``` +```console Prompt>Service.exe ServiceHost Started. Press Enter to terminate service. Ping: Ticks = 10 diff --git a/docs/framework/wcf/samples/configuration-channel-factory.md b/docs/framework/wcf/samples/configuration-channel-factory.md index b34ad12a96e74..8a6c68822b8f0 100644 --- a/docs/framework/wcf/samples/configuration-channel-factory.md +++ b/docs/framework/wcf/samples/configuration-channel-factory.md @@ -16,7 +16,7 @@ This sample covers the usage of the . -``` +```csharp class MyDataContractResolver : DataContractResolver { private Dictionary dictionary = new Dictionary(); diff --git a/docs/framework/wcf/samples/datacontractserializer-datacontractresolver-netdatacontractserializer.md b/docs/framework/wcf/samples/datacontractserializer-datacontractresolver-netdatacontractserializer.md index c674ef785cdef..34e7b83319c04 100644 --- a/docs/framework/wcf/samples/datacontractserializer-datacontractresolver-netdatacontractserializer.md +++ b/docs/framework/wcf/samples/datacontractserializer-datacontractresolver-netdatacontractserializer.md @@ -13,7 +13,7 @@ This sample demonstrates how the use of named `MyDataContractResolver` that is added to the in the DCSwithDCR project. -``` +```csharp class MyDataContractResolver : DataContractResolver { private XmlDictionary dictionary = new XmlDictionary(); diff --git a/docs/framework/wcf/samples/datacontractserializer-sample.md b/docs/framework/wcf/samples/datacontractserializer-sample.md index 9fb60fffffd30..7c9922a509e27 100644 --- a/docs/framework/wcf/samples/datacontractserializer-sample.md +++ b/docs/framework/wcf/samples/datacontractserializer-sample.md @@ -13,7 +13,7 @@ The DataContractSerializer sample demonstrates the to serialize `record1` into a memory stream. -``` +```csharp MemoryStream stream1 = new MemoryStream(); //Serialize the Record object to a memory stream using DataContractSerializer. @@ -85,7 +85,7 @@ serializer.WriteObject(stream1, record1); Next, the sample uses the to deserialize the memory stream back into a new `Record` object and displays it. -``` +```csharp stream1.Position = 0; //Deserialize the Record object back into a new record object. @@ -96,7 +96,7 @@ Console.WriteLine("Deserialized record: {0}", record2.ToString()); By default, the `DataContractSerializer` encodes objects into a stream using a textual representation of XML. However, you can influence the encoding of the XML by passing in a different writer. The sample creates a binary writer by calling . It then passes the writer and the record object to the serializer when it calls . Finally, the sample flushes the writer and reports on the length of the streams. -``` +```csharp MemoryStream stream2 = new MemoryStream(); XmlDictionaryWriter binaryDictionaryWriter = XmlDictionaryWriter.CreateBinaryWriter(stream2); @@ -110,7 +110,7 @@ Console.WriteLine("Binary Stream is {0} bytes long", stream2.Length); When you run the sample, the original record and the deserialized record are displayed, followed by the comparison between the length of the text encoding and the binary encoding. Press ENTER in the client window to shut down the client. -``` +```console Original record: Record: 1 + 2 = 3 Deserialized record: Record: 1 + 2 = 3 Text Stream is 233 bytes long diff --git a/docs/framework/wcf/samples/default-message-contract.md b/docs/framework/wcf/samples/default-message-contract.md index 428a48175c376..ccfe724092337 100644 --- a/docs/framework/wcf/samples/default-message-contract.md +++ b/docs/framework/wcf/samples/default-message-contract.md @@ -15,7 +15,7 @@ The Default Message Contract sample demonstrates a service where a custom user-d In the service, a single service operation is defined that accepts and returns custom messages of type `MyMessage`. Although in this sample the request and response messages are of the same type, they could of course be different message contracts if necessary. -``` +```csharp [ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")] public interface ICalculator { @@ -27,7 +27,7 @@ public interface ICalculator The custom message `MyMessage` is defined in a class annotated with , and attributes. Only the third constructor is used in this sample. Using message contracts allows you to exercise full control over the SOAP message. In this sample, the attribute is used to put `Operation` in a SOAP header. The operands `N1`, `N2` and the `Result` appear within the SOAP body because they have the attribute applied. -``` +```csharp [MessageContract] public class MyMessage { @@ -93,7 +93,7 @@ public class MyMessage The implementation class contains the code for the `Calculate` service operation. The `CalculateService` class obtains the operands and operator from the request message and creates a response message that contains the result of the requested calculation, as shown in the following sample code. -``` +```csharp // Service class which implements the service contract. public class CalculatorService : ICalculator { @@ -127,29 +127,31 @@ public class CalculatorService : ICalculator The generated client code for the client was created with the [ServiceModel Metadata Utility Tool (Svcutil.exe)](../../../../docs/framework/wcf/servicemodel-metadata-utility-tool-svcutil-exe.md) tool. The tool automatically creates message contract types in the generated client code if necessary. The `/messageContract` command option may be specified to force the generation of message contracts. -``` +```console svcutil.exe /n:"http://Microsoft.ServiceModel.Samples,Microsoft.ServiceModel.Samples" /o:client\generatedClient.cs http://localhost/servicemodelsamples/service.svc/mex ``` The following sample code demonstrates the client using the `MyMessage` message. -``` +```csharp // Create a client with given client endpoint configuration CalculatorClient client = new CalculatorClient(); // Perform addition using a typed message. -MyMessage request = new MyMessage(); -request.N1 = 100D; -request.N2 = 15.99D; -request.Operation = "+"; +MyMessage request = new MyMessage() + { + N1 = 100D, + N2 = 15.99D, + Operation = "+" + }; MyMessage response = ((ICalculator)client).Calculate(request); Console.WriteLine("Add({0},{1}) = {2}", request.N1, request.N2, response.Result); ``` When you run the sample, the calculations are displayed in the client console window. Press ENTER in the client window to shut down the client. -``` +```console Add(100,15.99) = 115.99 Subtract(145,76.54) = 68.46 Multiply(9,81.25) = 731.25 diff --git a/docs/framework/wcf/samples/default-service-behavior.md b/docs/framework/wcf/samples/default-service-behavior.md index fffdb016715ff..5428ef6491503 100644 --- a/docs/framework/wcf/samples/default-service-behavior.md +++ b/docs/framework/wcf/samples/default-service-behavior.md @@ -16,7 +16,7 @@ This sample demonstrates how service behavior settings can be configured. The sa The service class specifies behaviors with the and the as shown in the following code sample. All values specified are the defaults. -``` +```csharp [ServiceBehavior( AutomaticSessionShutdown=true, ConcurrencyMode=ConcurrencyMode.Single, @@ -62,7 +62,7 @@ public class CalculatorService : ICalculator When you run the sample, the operation requests and responses are displayed in the client console window. The delay between the calls is the result of the calls to `System.Threading.Thread.Sleep()` made in the service operations. The rest of the behavior samples explain these behaviors in more detail. Press ENTER in the client window to shut down the client. -``` +```console Add(100,15.99) = 115.99 Subtract(145,76.54) = 68.46 Multiply(9,81.25) = 731.25 diff --git a/docs/framework/wcf/samples/duplex.md b/docs/framework/wcf/samples/duplex.md index 9dd7acaf8c309..85e8fcb1c8b67 100644 --- a/docs/framework/wcf/samples/duplex.md +++ b/docs/framework/wcf/samples/duplex.md @@ -13,7 +13,7 @@ The Duplex sample demonstrates how to define and implement a duplex contract. Du In this sample, the client is a console application (.exe) and the service is hosted by Internet Information Services (IIS). The duplex contract is defined as follows: -``` +```csharp [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples", SessionMode=SessionMode.Required, CallbackContract=typeof(ICalculatorDuplexCallback))] public interface ICalculatorDuplex @@ -41,7 +41,7 @@ public interface ICalculatorDuplexCallback The `CalculatorService` class implements the primary `ICalculatorDuplex` interface. The service uses the instance mode to maintain the result for each session. A private property named `Callback` is used to access the callback channel to the client. The service uses the callback for sending messages back to the client through the callback interface. -``` +```csharp [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)] public class CalculatorService : ICalculatorDuplex { @@ -65,7 +65,9 @@ public class CalculatorService : ICalculatorDuplex equation += " + " + n.ToString(); Callback.Result(result); } - ... + + //... + ICalculatorDuplexCallback Callback { get @@ -78,7 +80,7 @@ public class CalculatorService : ICalculatorDuplex The client must provide a class that implements the callback interface of the duplex contract, for receiving messages from the service. In the sample, a `CallbackHandler` class is defined to implement the `ICalculatorDuplexCallback` interface. -``` +```csharp public class CallbackHandler : ICalculatorDuplexCallback { public void Result(double result) @@ -95,7 +97,7 @@ public class CallbackHandler : ICalculatorDuplexCallback The proxy that is generated for a duplex contract requires a to be provided upon construction. This is used as the site for an object that implements the callback interface and handles messages that are sent back from the service. An is constructed with an instance of the `CallbackHandler` class. This object handles messages sent from the service to the client on the callback interface. -``` +```csharp // Construct InstanceContext to handle messages on callback interface. InstanceContext instanceContext = new InstanceContext(new CallbackHandler()); @@ -166,14 +168,14 @@ client.Close(); ```xml - + ... - - + + ``` diff --git a/docs/framework/wcf/samples/fault-contract.md b/docs/framework/wcf/samples/fault-contract.md index ea5946fa8a1f9..261b6c4c9b322 100644 --- a/docs/framework/wcf/samples/fault-contract.md +++ b/docs/framework/wcf/samples/fault-contract.md @@ -11,7 +11,7 @@ The Fault Contract sample demonstrates how to communicate error information from The calculator contract has been modified to include a as shown in the following sample code. -``` +```csharp [ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")] public interface ICalculator { @@ -29,7 +29,7 @@ public interface ICalculator The attribute indicates that the `Divide` operation may return a fault of type `MathFault`. A fault can be of any type that can be serialized. In this case, the `MathFault` is a data contract, as follows: -``` +```csharp [DataContract(Namespace="http://Microsoft.ServiceModel.Samples")] public class MathFault { @@ -54,7 +54,7 @@ public class MathFault The `Divide` method throws a exception when a divide by zero exception occurs as shown in the following sample code. This exception results in a fault being sent to the client. -``` +```csharp public int Divide(int n1, int n2) { try @@ -73,7 +73,7 @@ public int Divide(int n1, int n2) The client code forces an error by requesting a division by zero. When you run the sample, the operation requests and responses are displayed in the client console window. You see the division by zero being reported as a fault. Press ENTER in the client window to shut down the client. -``` +```console Add(15,3) = 18 Subtract(145,76) = 69 Multiply(9,81) = 729 @@ -84,7 +84,7 @@ Press to terminate client. The client does this by catching the appropriate `FaultException` exception: -``` +```csharp catch (FaultException e) { Console.WriteLine("FaultException: Math fault while doing " + e.Detail.operation + ". Problem: " + e.Detail.problemType); diff --git a/docs/framework/wcf/samples/hello-world-with-the-routing-service.md b/docs/framework/wcf/samples/hello-world-with-the-routing-service.md index 3d387b93bfdd1..fd975f8116b9a 100644 --- a/docs/framework/wcf/samples/hello-world-with-the-routing-service.md +++ b/docs/framework/wcf/samples/hello-world-with-the-routing-service.md @@ -23,6 +23,7 @@ This sample demonstrates the Windows Communication Foundation (WCF) Routing Serv You should see the following output: + ```console Add(100,15.99) = 115.99 Subtract(145,76.54) = 68.46 @@ -30,6 +31,7 @@ This sample demonstrates the Windows Communication Foundation (WCF) Routing Serv Multiply(9,81.25) = 731.25 Divide(22,7) = 3.14285714285714 + ``` ## Configurable via Code or App.Config The sample ships configured to use an App.config file to define the router’s behavior. You can also change the name of the App.config file to something else so that it is not recognized and uncomment the method call to ConfigureRouterViaCode(). Either method results in the same behavior from the router. diff --git a/docs/framework/wcf/samples/iis-hosting-using-inline-code.md b/docs/framework/wcf/samples/iis-hosting-using-inline-code.md index 186c110b45320..b11bc178e1e47 100644 --- a/docs/framework/wcf/samples/iis-hosting-using-inline-code.md +++ b/docs/framework/wcf/samples/iis-hosting-using-inline-code.md @@ -23,7 +23,7 @@ This sample demonstrates how to implement a service hosted by Internet Informati The sample demonstrates a typical service that implements a contract that defines a request-reply communication pattern. The service is hosted in IIS and the service code is entirely contained in the Service.svc file. The service is host-activated and compiled on-demand by the first message sent to the service. There is no pre-compilation necessary. The service implements an `ICalculator` contract as defined in the following code: -``` +```csharp // Define a service contract. [ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")] public interface ICalculator @@ -41,9 +41,10 @@ This sample demonstrates how to implement a service hosted by Internet Informati The service implementation calculates and returns the appropriate result. -``` +```svc <%@ServiceHost language=c# Debug="true" Service="Microsoft.ServiceModel.Samples.CalculatorService" %> -… +``` +```csharp // Service class that implements the service contract. public class CalculatorService : ICalculator { @@ -68,7 +69,7 @@ public class CalculatorService : ICalculator When you run the sample, the operation requests and responses are displayed in the client console window. Press ENTER in the client window to shut down the client. -``` +```console Add(100,15.99) = 115.99 Subtract(145,76.54) = 68.46 Multiply(9,81.25) = 731.25 diff --git a/docs/framework/wcf/samples/imperative.md b/docs/framework/wcf/samples/imperative.md index a15e62eba4280..5957087d3e9eb 100644 --- a/docs/framework/wcf/samples/imperative.md +++ b/docs/framework/wcf/samples/imperative.md @@ -11,7 +11,7 @@ This sample demonstrates how to define a <