added samples
[windows-sources.git] / sdk / samples / WCFSamples / TechnologySamples / Tools / CustomChannelsTester / CS / Loader.cs
blob6bef963970edd6decc81ac68f65c2b794d5d191a
1 namespace System.ServiceModel.Samples.CustomChannelsTester
3 using System;
4 using System.Collections.Generic;
5 using System.Collections.ObjectModel;
6 using System.Diagnostics;
7 using System.IO;
8 using System.Reflection;
9 using System.ServiceModel;
10 using System.ServiceModel.Channels;
11 using System.Text;
12 using System.Xml;
13 using System.Xml.Schema;
14 using System.Xml.Serialization;
15 using System.Xml.XPath;
16 using TestSpec;
18 //This class parses the XML files and sets up the parameters for the tests accordingly.
20 public class Loader
22 readonly SchemaValidator validator;
24 public static readonly string TestSpecNamespace = "http://WCF/TestSpec";
25 public static readonly string TestSpecXsdLocation = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "testspec.xsd");
27 public Loader()
29 this.validator = new SchemaValidator(Loader.TestSpecNamespace, Loader.TestSpecXsdLocation);
32 internal void LoadSpec(string inputFileName)
34 if (Parameters.InputFileName != null)
37 XmlDocument doc = new XmlDocument();
38 doc.Load(inputFileName);
39 validator.Validate(doc);
41 XmlSerializer serializer = new XmlSerializer(typeof(TestSpec));
43 TextReader reader = new StreamReader(inputFileName);
44 TestSpec ts = (TestSpec)serializer.Deserialize(reader);
45 reader.Close();
47 Parameters.IsAsync = GetContractOption(ts.ServiceContract.IsAsync);
48 Parameters.IsSession = GetContractOption(ts.ServiceContract.IsSession);
49 Parameters.IsOneWay = GetContractOption(ts.ServiceContract.IsOneWay);
50 Parameters.IsCallBack = GetContractOption(ts.ServiceContract.IsCallBack);
52 if (ts.TestDetails.ServerMachineName == null)
53 Log.Trace("Running both Server and Client on the same machine");
54 else
55 Parameters.ServerMachineName = ts.TestDetails.ServerMachineName;
57 if (ts.TestDetails.ServerPortNumber == null)
58 Log.Trace("No port number on the server machine specified");
59 else
60 Parameters.ServerPortNumber = Int32.Parse(ts.TestDetails.ServerPortNumber);
62 if (ts.TestDetails.ClientCallBackAddress == null)
63 Log.Trace("No Client CallBack Address specified, using default address for Duplex CallBack contracts if required");
64 else
66 //The ClientBaseAddress specified should be a valid URI
67 try
69 Parameters.ClientCallBackAddress = new Uri(ts.TestDetails.ClientCallBackAddress);
71 catch (Exception e)
73 if (Parameters.IsCallBack != ContractOption.False)
74 Log.Exception(e);
78 if (ts.TestDetails.ServerTimeout == null)
79 Log.Trace("Default value used - Server will timeout after 120 seconds");
80 else
81 Parameters.ServerTimeout = new TimeSpan(0, 0, Int32.Parse(ts.TestDetails.ServerTimeout));
83 if (ts.TestDetails.ClientTimeout == null)
84 Log.Trace("Default value used - Client will wait for 30 seconds before starting");
85 else
86 Parameters.ClientTimeout = new TimeSpan(0, 0, Int32.Parse(ts.TestDetails.ClientTimeout));
88 if (ts.TestDetails.NumberOfClients == null)
89 Log.Trace("Default value used - Number of Client per Service = 1");
90 else
91 Parameters.NumberOfClients = Int32.Parse(ts.TestDetails.NumberOfClients);
93 if (ts.TestDetails.MessagesPerClient == null)
94 Log.Trace("Default value used - Number of Messages sent per Client = 1");
95 else
96 Parameters.MessagesPerClient = Int32.Parse(ts.TestDetails.MessagesPerClient);
99 Log.Trace("ServerMachineName = " + Parameters.ServerMachineName);
100 Log.Trace("ServerPortNumber Number = " + Parameters.ServerPortNumber);
101 if (Parameters.IsCallBack != ContractOption.False)
102 Log.Trace("ClientCallBack Address = " + Parameters.ClientCallBackAddress.ToString());
103 Log.Trace("ServerTimeout = " + Parameters.ServerTimeout.ToString());
104 Log.Trace("ClientTimeout = " + Parameters.ClientTimeout.ToString());
105 Log.Trace("NumberOfClients = " + Parameters.NumberOfClients);
106 Log.Trace("MessagesPerClient = " + Parameters.MessagesPerClient);
109 private ContractOption GetContractOption(Contract contractChoice)
111 if (contractChoice == null)
112 return ContractOption.False;
113 if (contractChoice.ExpandAllSpecified == true && contractChoice.ExpandAll == true)
114 return ContractOption.Both;
115 else if (contractChoice.Value == false)
116 return ContractOption.False;
117 else if (contractChoice.Value == true)
118 return ContractOption.True;
119 else
120 return ContractOption.Both;
123 internal void UpdateParameters()
125 if (Parameters.IsAsync == ContractOption.True || Parameters.IsAsync == ContractOption.Both)
127 if(Parameters.IsSession == ContractOption.True || Parameters.IsSession == ContractOption.Both)
129 if(Parameters.IsOneWay == ContractOption.False || Parameters.IsOneWay == ContractOption.Both)
130 Parameters.ServiceContracts.Add(ServiceContract.IAsyncSessionTwoWay);
131 if(Parameters.IsOneWay == ContractOption.True || Parameters.IsOneWay == ContractOption.Both)
132 Parameters.ServiceContracts.Add(ServiceContract.IAsyncSessionOneWay);
134 if(Parameters.IsSession == ContractOption.False || Parameters.IsSession == ContractOption.Both)
136 if(Parameters.IsOneWay == ContractOption.True || Parameters.IsOneWay == ContractOption.Both)
137 Parameters.ServiceContracts.Add(ServiceContract.IAsyncOneWay);
138 if(Parameters.IsOneWay == ContractOption.False || Parameters.IsOneWay == ContractOption.Both)
139 Parameters.ServiceContracts.Add(ServiceContract.IAsyncTwoWay);
143 if (Parameters.IsAsync == ContractOption.False || Parameters.IsAsync == ContractOption.Both)
145 if(Parameters.IsSession == ContractOption.True || Parameters.IsSession == ContractOption.Both)
147 if(Parameters.IsOneWay == ContractOption.False || Parameters.IsOneWay == ContractOption.Both)
148 Parameters.ServiceContracts.Add(ServiceContract.ISyncSessionTwoWay);
149 if(Parameters.IsOneWay == ContractOption.True || Parameters.IsOneWay == ContractOption.Both)
150 Parameters.ServiceContracts.Add(ServiceContract.ISyncSessionOneWay);
152 if(Parameters.IsSession == ContractOption.False || Parameters.IsSession == ContractOption.Both)
154 if(Parameters.IsOneWay == ContractOption.True || Parameters.IsOneWay == ContractOption.Both)
155 Parameters.ServiceContracts.Add(ServiceContract.ISyncOneWay);
156 if(Parameters.IsOneWay == ContractOption.False || Parameters.IsOneWay == ContractOption.Both)
157 Parameters.ServiceContracts.Add(ServiceContract.ISyncTwoWay);
161 if (Parameters.IsCallBack == ContractOption.True || Parameters.IsCallBack == ContractOption.Both)
163 if (Parameters.IsSession == ContractOption.True || Parameters.IsSession == ContractOption.Both)
164 Parameters.ServiceContracts.Add(ServiceContract.IDuplexSessionContract);
165 if (Parameters.IsSession == ContractOption.False || Parameters.IsSession == ContractOption.Both)
166 Parameters.ServiceContracts.Add(ServiceContract.IDuplexContract);
168 Log.Trace("Number of service contracts === " + Parameters.ServiceContracts.Count);
171 Parameters.TotalMessages = Parameters.NumberOfClients * Parameters.MessagesPerClient * Parameters.ServiceContracts.Count;