示例#1
0
        private void AddMinimum(
            WSD.ServiceDescription wsdl,
            string contractName,
            OperationDescriptionCollection operations,
            ServiceEndpoint endpoint)
        {
            // Absolut minimum in order to provide a WSDL
            var portType =
                new WSD.PortType
            {
                Name = contractName
            };

            wsdl.PortTypes.Add(portType);
            var address =
                new WSD.Soap12AddressBinding
            {
                Location = endpoint.Address.ToString()
            };
            var port =
                new WSD.Port
            {
                Name = contractName
            };

            port.Extensions.Add(address);
            var service =
                new WSD.Service
            {
                Name = wsdl.Name
            };

            service.Ports.Add(port);
            wsdl.Services.Add(service);
        }
示例#2
0
        static void Main(string[] args)
        {
            ServiceHost myHost = new ServiceHost(typeof(myCalculator));

            try
            {
                ServiceEndpoint endpoint1 = myHost.Description.Endpoints.Find(typeof(ICalculator));
                ServiceEndpoint endpoint2 = myHost.Description.Endpoints.Find(new Uri("http://localhost:10007/myService/endpoint2"));
                ServiceEndpoint endpoint3 = myHost.Description.Endpoints.Find(new Uri("http://localhost:20007/myService/endpoint3"));
                Uri             address4  = new Uri("net.tcp://localhost:30000/myServiceTCP");
                ServiceEndpoint endpoint4 = myHost.AddServiceEndpoint(typeof(ICalculator), new NetTcpBinding(), address4);

                //wyswietl endpointy
                Console.WriteLine("\n---> Endpointy:");

                Console.WriteLine("\nService endpoint {0}:", endpoint1.Name);
                Console.WriteLine("Binding: {0}", endpoint1.Binding.ToString());
                Console.WriteLine("ListenUri: {0}\n", endpoint1.ListenUri.ToString());

                Console.WriteLine("\nService endpoint {0}:", endpoint2.Name);
                Console.WriteLine("Binding: {0}", endpoint2.Binding.ToString());
                Console.WriteLine("ListenUri: {0}\n", endpoint2.ListenUri.ToString());

                Console.WriteLine("\nService endpoint {0}:", endpoint3.Name);
                Console.WriteLine("Binding: {0}", endpoint3.Binding.ToString());
                Console.WriteLine("ListenUri: {0}\n", endpoint3.ListenUri.ToString());

                Console.WriteLine("\nService endpoint {0}:", endpoint4.Name);
                Console.WriteLine("Binding: {0}", endpoint4.Binding.ToString());
                Console.WriteLine("ListenUri: {0}\n", endpoint4.ListenUri.ToString());

                myHost.Open();
                Console.WriteLine("\n--> Serwis 1 jest uruchomiony.");

                ContractDescription cd = ContractDescription.GetContract(typeof(ICalculator));
                Console.WriteLine("Informacje o kontrakcie:");
                Type contractType = cd.ContractType;
                Console.WriteLine("\tContract type: {0}", contractType.ToString());
                string name = cd.Name;
                Console.WriteLine("\tName: {0}", name);
                OperationDescriptionCollection odc = cd.Operations;
                Console.WriteLine("\tOperacje:");
                foreach (OperationDescription od in odc)
                {
                    Console.WriteLine("\t\t" + od.Name);
                }


                Console.WriteLine("\n--> Nacisnij <ENTER> aby zakonczyc.");
                Console.WriteLine();
                Console.ReadLine();
                myHost.Close();
            }
            catch (CommunicationException ce)
            {
                Console.WriteLine("Wystapil wyjatek: {0}", ce.Message);
                myHost.Abort();
            }
        }
示例#3
0
        public ContractDescription(string name, string ns)
        {
            // the property setter validates given value
            this.Name = name;
            if (!string.IsNullOrEmpty(ns))
                NamingHelper.CheckUriParameter(ns, "ns");

            _operations = new OperationDescriptionCollection();
            _ns = ns ?? NamingHelper.DefaultNamespace; // ns can be ""
        }
示例#4
0
        static void Main(string[] args)
        {
            ServiceHost     mojHost   = new ServiceHost(typeof(DuplexOperations));
            ServiceEndpoint endpoint  = mojHost.Description.Endpoints.Find(typeof(Contract.IDuplexOperations));
            ServiceHost     mojHost2  = new ServiceHost(typeof(Service1));
            ServiceEndpoint endpoint2 = mojHost2.Description.Endpoints.Find(typeof(Contract.IService1));

            try
            {
                Console.WriteLine("\n--> Endpointy");
                Console.WriteLine("\nSerwise endpoint {0}:", endpoint.Name);
                Console.WriteLine("Binding: {0}", endpoint.Binding.ToString());
                Console.WriteLine("ListenUri: {0}", endpoint.ListenUri.ToString());
                Console.WriteLine("\nSerwise endpoint {0}:", endpoint2.Name);
                Console.WriteLine("Binding: {0}", endpoint2.Binding.ToString());
                Console.WriteLine("ListenUri: {0}", endpoint2.ListenUri.ToString());
                mojHost.Open();
                Console.WriteLine("Serwis 1 jest uruchomiony");
                ContractDescription cd = ContractDescription.GetContract(typeof(IDuplexOperations));
                Console.WriteLine("Informacje o kontrakcie");
                Type contractType = cd.ContractType;
                Console.WriteLine("\tContract type: {0}", contractType.ToString());
                string name = cd.Name;
                Console.WriteLine("\tName: {0}", name);
                OperationDescriptionCollection odc = cd.Operations;
                Console.WriteLine("\tOperacje");
                foreach (OperationDescription od in odc)
                {
                    Console.WriteLine("\t\t" + od.Name);
                }
                mojHost2.Open();
                Console.WriteLine("Serwis 2 jest uruchomiony");
                ContractDescription cd2 = ContractDescription.GetContract(typeof(IService1));
                Console.WriteLine("Informacje o kontrakcie");
                Type contractType2 = cd2.ContractType;
                Console.WriteLine("\tContract type: {0}", contractType2.ToString());
                string name2 = cd2.Name;
                Console.WriteLine("\tName: {0}", name2);
                OperationDescriptionCollection odc2 = cd2.Operations;
                Console.WriteLine("\tOperacje");
                foreach (OperationDescription od2 in odc2)
                {
                    Console.WriteLine("\t\t" + od2.Name);
                }
                Console.WriteLine("Nacisnij <ENTER> aby zakonczyc");
                Console.WriteLine();
                Console.ReadLine();
                mojHost.Close();
            }
            catch (CommunicationException ce)
            {
                Console.WriteLine("Wystapil wyjatek: {0}", ce.Message);
                mojHost.Abort();
            }
        }
 public ContractDescription(string name, string ns)
 {
     this.behaviors = new KeyedByTypeCollection<IContractBehavior>();
     this.Name = name;
     if (!string.IsNullOrEmpty(ns))
     {
         NamingHelper.CheckUriParameter(ns, "ns");
     }
     this.operations = new OperationDescriptionCollection();
     this.ns = ns ?? "http://tempuri.org/";
 }
示例#6
0
        public static void BuildingServiceaBehavior(WCFServiceMeta wCFServiceMeta, ServiceElement serviceElement, SpringServiceHost ssh)
        {
            ServiceBehaviorElement sbe = null;

            if (serviceElement.BehaviorConfiguration != "" && wCFServiceMeta.BehaviorsConfiguration != null)
            {
                if (wCFServiceMeta.BehaviorsConfiguration.ServiceBehaviors.ContainsKey(serviceElement.BehaviorConfiguration))
                {
                    ServiceBehaviorElementCollection sbec = wCFServiceMeta.BehaviorsConfiguration.ServiceBehaviors;
                    foreach (ServiceBehaviorElement o in sbec)
                    {
                        if (o.Name == serviceElement.BehaviorConfiguration)
                        {
                            sbe = o;
                            break;
                        }
                    }
                    if (sbe != null)
                    {
                        //ServiceBehavior smb = new ServiceMetadataBehavior();
                        foreach (var metadata in sbe)
                        {
                            switch (metadata.GetType().FullName)
                            {
                            case "System.ServiceModel.Configuration.DataContractSerializerElement":
                            {
                                DataContractSerializerElement dse = metadata as DataContractSerializerElement;
                                if (dse != null)
                                {
                                    int i = dse.MaxItemsInObjectGraph;

                                    ContractDescription            cd   = ssh.Description.Endpoints.FirstOrDefault(o => o.Name != "IMetadataExchange").Contract;
                                    OperationDescriptionCollection opdc = cd.Operations;
                                    foreach (OperationDescription odp in opdc)
                                    {
                                        DataContractSerializerOperationBehavior dsb = new DataContractSerializerOperationBehavior(odp);
                                        dsb.IgnoreExtensionDataObject = dse.IgnoreExtensionDataObject;
                                        dsb.MaxItemsInObjectGraph     = dse.MaxItemsInObjectGraph;
                                        odp.Behaviors.Remove <DataContractSerializerOperationBehavior>();
                                        odp.Behaviors.Add(dsb);
                                    }

                                    return;
                                }
                                break;
                            }
                            }
                        }
                    }
                }
            }
        }
示例#7
0
 /// <summary>
 ///     This method sets the MaxItemsInObjectGraph for the WCF connection to the server
 /// </summary>
 /// <param name="operations">
 ///     The OperationDescriptionCollection defined for the channel that is to be updated with the
 ///     specified MaxItemsInObjectGraph.
 /// </param>
 /// <param name="maxItemsInObjectGraph"> The MaxItemsInObjectGraph value to be used. </param>
 public static void SetMaxItemsInObjectGraph(OperationDescriptionCollection operations, int maxItemsInObjectGraph)
 {
     if (maxItemsInObjectGraph > 0) // use the default if maxItemsInObjectGraph is not supplied
     {
         foreach (OperationDescription op in operations)
         {
             var dataContractBehavior = op.Behaviors.Find <DataContractSerializerOperationBehavior>();
             if (dataContractBehavior is not null)
             {
                 dataContractBehavior.MaxItemsInObjectGraph = maxItemsInObjectGraph;
             }
         }
     }
 }
示例#8
0
        internal bool HasDuplexOperations()
        {
            OperationDescriptionCollection operations = this.Endpoint.Contract.Operations;

            for (int i = 0; i < operations.Count; i++)
            {
                OperationDescription description = operations[i];
                if (description.IsServerInitiated())
                {
                    return(true);
                }
            }
            return(false);
        }
示例#9
0
    public static void XmlSerializerOperationBehavior_BasicUsage()
    {
        XmlSerializerOperationBehavior serializerBehavior;
        BasicHttpBinding binding = new BasicHttpBinding();
        string baseAddress = "http://localhost:1066/SomeService";
        ChannelFactory<IXmlTestingType> factory = new ChannelFactory<IXmlTestingType>(binding, new EndpointAddress(baseAddress));
        ContractDescription cd = factory.Endpoint.Contract;
        OperationDescriptionCollection collection = cd.Operations;

        foreach (OperationDescription description in collection)
        {
            // Find the serializer behavior for those operations that have the attribute set via the interface.
            serializerBehavior = description.Behaviors.Find<XmlSerializerOperationBehavior>();
            if (serializerBehavior == null)
            {
                // This operation was not set with XmlSerializerFormatAttribute
                // Here we add the attribute programatically using defaults.
                if (String.Equals(description.Name, nameof(IXmlTestingType.XmlSerializerFormatAttribute_NotSet_One)))
                {
                    // Default OperationFormatStyle is "Document"
                    serializerBehavior = new XmlSerializerOperationBehavior(description);
                    description.Behaviors.Add(serializerBehavior);
                }
                // There is one additional operation not set with XmlSerializerFormatAttribute
                // Here we add the attribute programatically and further set the OperationFormatStyle to 'Rpc'
                else
                {
                    XmlSerializerFormatAttribute serializerAttribute = new XmlSerializerFormatAttribute();
                    serializerAttribute.Style = OperationFormatStyle.Rpc;

                    serializerBehavior = new XmlSerializerOperationBehavior(description, serializerAttribute);
                    description.Behaviors.Add(serializerBehavior);
                }

            }

            if (String.Equals(description.Name, nameof(IXmlTestingType.XmlSerializerFormatAttribute_Set_StyleSetTo_Rpc)) || (String.Equals(description.Name, nameof(IXmlTestingType.XmlSerializerFormatAttribute_NotSet_Two))))
            {
                Assert.True(String.Equals(serializerBehavior.XmlSerializerFormatAttribute.Style.ToString(), "Rpc"));
            }
            else
            {
                Assert.True(String.Equals(serializerBehavior.XmlSerializerFormatAttribute.Style.ToString(), "Document"));
            }
        }
    }
示例#10
0
        /// <summary>
        /// Metoda główna programu, to tutaj włącza się serwer
        /// </summary>
        /// <param name="args">Argumenty uruchomienia - tutaj nie używane</param>
        public static void Main(string[] args)
        {
            ServiceHost mojHost = new ServiceHost(typeof(MyCalculator));

            try
            {
                ServiceEndpoint endpoint1 = mojHost.Description.Endpoints.Find(typeof(ICalculator));

                //wyswietl endpointy
                Console.WriteLine("\n---> Endpointy:");

                ShowEndpoint(endpoint1);

                mojHost.Open();
                Console.WriteLine("\n--> Serwis 1 jest uruchomiony.");

                ContractDescription cd =
                    ContractDescription.GetContract(typeof(ICalculator));
                Console.WriteLine("Informacje o kontrakcie:");
                Type contractType = cd.ContractType;
                Console.WriteLine("\tContract type: {0}", contractType.ToString());
                string name = cd.Name;
                Console.WriteLine("\tName: {0}", name);
                OperationDescriptionCollection odc = cd.Operations;
                Console.WriteLine("\tOperacje:");
                foreach (OperationDescription od in odc)
                {
                    Console.WriteLine("\t\t" + od.Name);
                }

                Console.WriteLine("\n--> Nacisnij <ENTER> aby zakonczyc.");
                Console.WriteLine();
                Console.ReadLine();
                Console.ReadLine();

                mojHost.Close();
            }
            catch (CommunicationException ce)
            {
                Console.WriteLine("Wystapil wyjatek: {0}", ce.Message);
                mojHost.Abort();
            }
        }
示例#11
0
 protected void SetProtectionLevelForFederation(OperationDescriptionCollection operations)
 {
     foreach (OperationDescription operation in operations)
     {
         foreach (MessageDescription message in operation.Messages)
         {
             if (message.Body.Parts.Count > 0)
             {
                 foreach (MessagePartDescription part in message.Body.Parts)
                 {
                     part.ProtectionLevel = System.Net.Security.ProtectionLevel.EncryptAndSign;
                 }
             }
             if (OperationFormatter.IsValidReturnValue(message.Body.ReturnValue))
             {
                 message.Body.ReturnValue.ProtectionLevel = System.Net.Security.ProtectionLevel.EncryptAndSign;
             }
         }
     }
 }
示例#12
0
        /// <summary>
        /// Metoda Main, uruchamiająca hosta
        /// </summary>
        /// <param name="args">parametry dla klasy Main, domyślny argument</param>
        public static void Main(string[] args)
        {
            ServiceHost host = new ServiceHost(typeof(MyDictionary));

            try
            {
                ServiceEndpoint endpoint1 = host.Description.Endpoints.Find(typeof(IMyDictionary));
                Console.WriteLine("\n---> Endpointy:");
                Console.WriteLine("\nService endpoint {0}:", endpoint1.Name);
                Console.WriteLine("Binding: {0}", endpoint1.Binding.ToString());
                Console.WriteLine("ListenUri: {0}", endpoint1.ListenUri.ToString());

                host.Open();
                Console.WriteLine("\n--> Service1 jest uruchomiony.");

                ContractDescription cd = ContractDescription.GetContract(typeof(IMyDictionary));
                Console.WriteLine("Informacje o kontrakcie:");
                Type contractType = cd.ContractType;
                Console.WriteLine("\tContract type: {0}", contractType.ToString());
                string name = cd.Name;
                Console.WriteLine("\tName: {0}", name);
                OperationDescriptionCollection odc = cd.Operations;
                Console.WriteLine("\tOperacje:");
                foreach (OperationDescription od in odc)
                {
                    Console.WriteLine("\t\t" + od.Name);
                }

                Console.WriteLine("\n--> Nacisnij <ENTER> aby zakonczyc.");
                Console.WriteLine();
                Console.ReadLine();
                host.Close();
            }
            catch (CommunicationException ce)
            {
                Console.WriteLine("Wystapil wyjatek: {0}", ce.Message);
                host.Abort();
            }
        }
		public ContractDescription (string name, string ns)
		{
			if (name == null)
				throw new ArgumentNullException ("name");
			if (name.Length == 0)
				throw new ArgumentOutOfRangeException ("ContractDescription's Name must be a non-empty string.");
			if (ns == null)
				ns = "http://tempuri.org/";

			this.name = name;
			this.ns = ns;
			behaviors = new KeyedByTypeCollection<IContractBehavior>  ();
			operations = new OperationDescriptionCollection ();
		}
示例#14
0
 private void createServiceInterface(string name, OperationDescriptionCollection operations, XmlTypeExtractor schemaImporter)
 {
     var members = operations.Select(_ => operationMember(_, schemaImporter)).ToList();
     var classGenerator = templateOperations.ClassGenerator(
         name,
         members,
         null,
         new[]
             {
                 Attributes.GeneratedCodeAttribute,
                 Attributes.ServiceContractAttribute(name),
             },
         true);
     templateOperations.CreateFile(name, classGenerator.TransformText());
 }
示例#15
0
        private void createServiceClient(string name, OperationDescriptionCollection operations, XmlTypeExtractor schemaImporter)
        {
            var interfaceMembers = operations.Select(_ => operationMember(_, schemaImporter)).ToList();
            foreach (var memberDescription in interfaceMembers)
            {
                memberDescription.Attributes = new List<AttributeDescription>();
                memberDescription.Scope = MemberDescription.MethodScopeValues.Public;
                // TODO: Extract to template/make use of arguments properly
                memberDescription.SetBody("return base.Channel." +
                                          memberDescription.Name +
                                          "(" +
                                          string.Join(", ", memberDescription.Arguments.Select(_ => _.Name).ToArray()) +
                                          ");");
            }

            var baseTypes = new[] {"System.ServiceModel.ClientBase<" + name + ">", name};
            string className = name.Remove(0, 1) + "Client";

            var members = new List<MemberDescription>();

            var endpointConfigArg = new MemberDescription.ArgumentDefinition() {Name = "endpointConfigurationName", Type = "string",};
            var remoteAddressArg = new MemberDescription.ArgumentDefinition() { Name = "remoteAddress", Type = "string", };
            var remoteAddressStrongArg = new MemberDescription.ArgumentDefinition() { Name = "remoteAddress", Type = "System.ServiceModel.EndpointAddress", };
            var bindingArg = new MemberDescription.ArgumentDefinition() { Name = "binding", Type = "System.ServiceModel.Channels.Binding", };

            members.Add(MemberDescription.Method(className, null, null, null,
                                                 MemberDescription.MethodScopeValues.Public, false, true));
            var constructor2 = MemberDescription.Method(className, null, new[] {endpointConfigArg,}, null, MemberDescription.MethodScopeValues.Public, false, true);
            constructor2.SetBaseCall(new[] { new ArgumentDescription() { Value = "endpointConfigurationName" }, });
            members.Add(constructor2);

            var constructor3 = MemberDescription.Method(className, null, new[] {endpointConfigArg, remoteAddressArg}, null, MemberDescription.MethodScopeValues.Public, false, true);
            constructor3.SetBaseCall(new[] { new ArgumentDescription() { Value = "endpointConfigurationName" }, new ArgumentDescription() {Value = "remoteAddress"}, });
            members.Add(constructor3);

            var constructor4 = MemberDescription.Method(className, null, new[] {endpointConfigArg, remoteAddressStrongArg}, null, MemberDescription.MethodScopeValues.Public, false, true);
            constructor4.SetBaseCall(new[] { new ArgumentDescription() { Value = "endpointConfigurationName" }, new ArgumentDescription() { Value = "remoteAddress" }, });
            members.Add(constructor4);

            var constructor5 = MemberDescription.Method(className, null, new[] {bindingArg, remoteAddressStrongArg}, null, MemberDescription.MethodScopeValues.Public, false, true);
            constructor5.SetBaseCall(new[] { new ArgumentDescription() { Value = "binding" }, new ArgumentDescription() { Value = "remoteAddress" }, });
            members.Add(constructor5);

            members.AddRange(interfaceMembers);
            var classGenerator = templateOperations.ClassGenerator(
                className,
                members,
                baseTypes,
                new[]
                    {
                        Attributes.GeneratedCodeAttribute,
                    },
                false);
            templateOperations.CreateFile(className, classGenerator.TransformText());
        }
示例#16
0
    // Helper method to validate the given ContractDescription has the expected type and operations.
    public static string ValidateContractDescription(ContractDescription contract, Type expectedType, ContractDescriptionData expectedContract)
    {
        OperationDescriptionData[] expectedOperations = expectedContract.Operations;

#if DEBUGGING
        DumpContractDescription(contract);
#endif
        StringBuilder errorBuilder = new StringBuilder();
        string        prefix       = String.Format("    Contract type: {0}", expectedType);
        try
        {
            // ContractType must match expected type
            Type contractType = contract.ContractType;
            if (!expectedType.Equals(contractType))
            {
                errorBuilder.AppendLine(String.Format("{0} expected Type = {0}, actual = {1}", prefix, expectedType, contractType));
            }

            // Must have exactly the expected number of OperationDescriptions
            OperationDescriptionCollection ops = contract.Operations;
            if (ops.Count != expectedOperations.Length)
            {
                errorBuilder.AppendLine(String.Format("{0} operations.Count: expected={1}, actual = {2}", prefix, expectedOperations.Length, ops.Count));
            }

            foreach (OperationDescriptionData expectedOp in expectedOperations)
            {
                // Must have each operation by name
                OperationDescription op = ops.Find(expectedOp.Name);
                if (op == null)
                {
                    errorBuilder.AppendLine(String.Format("{0} operations: could not find operation {1}", prefix, expectedOp.Name));
                }
                else
                {
                    // Has expected operation name
                    if (!op.Name.Equals(expectedOp.Name))
                    {
                        errorBuilder.AppendLine(String.Format("{0} expected operation Name = {1}, actual = {2}",
                                                              prefix, expectedOp.Name, op.Name));
                    }

                    // Has expected one-way setting
                    if (op.IsOneWay != expectedOp.IsOneWay)
                    {
                        errorBuilder.AppendLine(String.Format("{0} expected operation {1}.IsOneWay = {2}, actual = {3}",
                                                              prefix, expectedOp.Name, expectedOp.IsOneWay, op.IsOneWay));
                    }

                    // If contains XxxAsync operation, op.TaskMethod will be non-null.  Verify it as expected.
                    bool hasTask = op.TaskMethod != null;
                    if (hasTask != expectedOp.HasTask)
                    {
                        errorBuilder.AppendLine(String.Format("{0} expected operation {1}.HasTask = {2}, actual = {3}",
                                                              prefix, expectedOp.Name, expectedOp.HasTask, hasTask));
                    }

                    // Validate each MessageDescription for each OperationDescription
                    MessageDescriptionCollection messages = op.Messages;
                    foreach (MessageDescriptionData messageData in expectedOp.Messages)
                    {
                        // Must find each expected MessageDescription by Action name
                        MessageDescription messageDesc = messages.FirstOrDefault(m => m.Action.Equals(messageData.Action));
                        if (messageDesc == null)
                        {
                            errorBuilder.AppendLine(String.Format("{0} could not find expected message action {1} in operation {2}",
                                                                  prefix, messageData.Action, op.Name));
                        }
                        else
                        {
                            // Must have expected direction
                            if (messageDesc.Direction != messageData.Direction)
                            {
                                errorBuilder.AppendLine(String.Format("{0} message action {1} expected Direction = {2}, actual = {3}",
                                                                      prefix, messageData.Action, messageData.Direction, messageDesc.Direction));
                            }

                            // MessageType is non-null for operations containing MessageContract types.
                            // Verify we were able to build a "typed message" from the MessageContract.
                            if (messageData.MessageType != null && !messageData.MessageType.Equals(messageDesc.MessageType))
                            {
                                errorBuilder.AppendLine(String.Format("{0} message action {1} expected MessageType = {2}, actual = {3}",
                                                                      prefix, messageData.Action, messageData.MessageType, messageDesc.MessageType));
                            }
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            errorBuilder.AppendLine(String.Format("{0} unexpected exception was caught: {1}",
                                                  prefix, ex.ToString()));
        }

        return(errorBuilder.Length == 0 ? null : errorBuilder.ToString());
    }
示例#17
0
        public static void Main()
        {
            // <Snippet0>
            Uri         baseAddress = new Uri("http://localhost:8001/Simple");
            ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), baseAddress);

            serviceHost.AddServiceEndpoint(
                typeof(ICalculator),
                new WSHttpBinding(),
                "CalculatorServiceObject");

            // Enable Mex
            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();

            smb.HttpGetEnabled = true;
            serviceHost.Description.Behaviors.Add(smb);

            serviceHost.Open();

            // <Snippet1>
            ContractDescription cd0 = new ContractDescription("ICalculator");
            // </Snippet1>
            // <Snippet2>
            ContractDescription cd1 = new ContractDescription("ICalculator", "http://www.tempuri.org");
            // </Snippet2>
            // <Snippet13>
            ContractDescription cd2 = ContractDescription.GetContract(typeof(ICalculator));
            // </Snippet13>
            // <Snippet14>
            CalculatorService   calcSvc = new CalculatorService();
            ContractDescription cd3     = ContractDescription.GetContract(typeof(ICalculator), calcSvc);
            // </Snippet14>
            // <Snippet15>
            ContractDescription cd4 = ContractDescription.GetContract(typeof(ICalculator), typeof(CalculatorService));
            // </Snippet15>
            ContractDescription cd = serviceHost.Description.Endpoints[0].Contract;

            Console.WriteLine("Displaying information for contract: {0}", cd.Name.ToString());

            // <Snippet3>
            KeyedByTypeCollection <IContractBehavior> behaviors = cd.Behaviors;

            Console.WriteLine("\tDisplay all behaviors:");
            foreach (IContractBehavior behavior in behaviors)
            {
                Console.WriteLine("\t\t" + behavior.ToString());
            }
            // </Snippet3>

            // <Snippet4>
            Type type = cd.CallbackContractType;
            // </Snippet4>

            // <Snippet5>
            string configName = cd.ConfigurationName;

            Console.WriteLine("\tConfiguration name: {0}", configName);
            // </Snippet5>

            // <Snippet6>
            Type contractType = cd.ContractType;

            Console.WriteLine("\tContract type: {0}", contractType.ToString());
            // </Snippet6>

            // <Snippet7>
            bool hasProtectionLevel = cd.HasProtectionLevel;

            if (hasProtectionLevel)
            {
                // <Snippet11>
                ProtectionLevel protectionLevel = cd.ProtectionLevel;
                Console.WriteLine("\tProtection Level: {0}", protectionLevel.ToString());
                // </Snippet11>
            }
            // </Snippet7>


            // <Snippet8>
            string name = cd.Name;

            Console.WriteLine("\tName: {0}", name);
            // </Snippet8>

            // <Snippet9>
            string namespc = cd.Namespace;

            Console.WriteLine("\tNamespace: {0}", namespc);
            // </Snippet9>

            // <Snippet10>
            OperationDescriptionCollection odc = cd.Operations;

            Console.WriteLine("\tDisplay Operations:");
            foreach (OperationDescription od in odc)
            {
                Console.WriteLine("\t\t" + od.Name);
            }
            // </Snippet10>

            // <Snippet12>
            SessionMode sm = cd.SessionMode;

            Console.WriteLine("\tSessionMode: {0}", sm.ToString());
            // </Snippet12>

            // <Snippet16>
            Collection <ContractDescription> inheretedContracts = cd.GetInheritedContracts();

            Console.WriteLine("\tInherited Contracts:");
            foreach (ContractDescription contractdescription in inheretedContracts)
            {
                Console.WriteLine("\t\t" + contractdescription.Name);
            }
            // </Snippet16>

            Console.WriteLine("The service is ready.");
            Console.WriteLine("Press <ENTER> to terminate service.");
            Console.WriteLine();
            Console.ReadLine();

            // Close the ServiceHostBase to shutdown the service.
            serviceHost.Close();
            // </Snippet0>
        }