static void Main(string[] args) { string uri = "http://10.1.83.11:8889/calc"; try { //TraceSource wcf = new TraceSource("System.ServiceModel"); //wcf.TraceInformation("TracingLog"); //wcf.Flush(); //wcf.Switch.Level = SourceLevels.Off; ServiceHost host = new ServiceHost(typeof(Calculator), new Uri(uri)); BasicHttpBinding binding = new BasicHttpBinding(); host.AddServiceEndpoint(typeof(ICalculator), binding, ""); host.Description.Behaviors.Add(new ServiceMetadataBehavior() { HttpGetEnabled = true }); host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex"); host.Opened += delegate { Console.WriteLine("host opend"); }; host.Open(); ChannelFactory <ICalculator> factory = new ChannelFactory <ICalculator>(binding, uri); ICalculator proxy = factory.CreateChannel(); var result = proxy.Divide(2, 0); //Trace.WriteLine(result, "Debug"); //Trace.Flush(); //Debug.WriteLine(result, "Debug"); //Debug.Flush(); TraceSource ts = new TraceSource("TracingLog.TracingLog"); ts.TraceInformation("TracingLog"); ts.Listeners.Add(new TextWriterTraceListener("")); ts.Flush(); DisplayProperties(ts); Console.ReadLine(); } catch (FaultException <Calculator.MathError> ex) { Calculator.MathError error = ex.Detail; Console.WriteLine("An Fault is thrown.\n\tFault code:{0}\n\tFault Reason:{1}\n\tOperation:{2}\n\tMessage:{3}", ex.Code, ex.Reason, error.Operation, error.ErrorMessage); } catch (Exception ex) { Console.WriteLine(ex); } }
public double Divide(double x, double y) { if (y == 0) { //throw new DivideByZeroException("Divide By Zero Exception"); Calculator.MathError error = new Calculator.MathError("Divide", "Divide By Zero Exception"); throw new FaultException <Calculator.MathError>(error, new FaultReason("Parameters passed are not valid") { }, new FaultCode("Sender")); } else { var result = x / y; return(result); } }