private string ComplexFaultToString(ClientContract.ComplexFault cf) { StringBuilder sb = new StringBuilder(); sb.Append(cf.ErrorInt); sb.Append(':'); sb.Append(cf.ErrorString); sb.Append(':'); sb.Append(cf.SomeFault.ID); sb.Append(':'); sb.Append(cf.SomeFault.message); sb.Append(':'); for (int i = 0; i < cf.ErrorByteArray.Length; i++) { sb.Append(cf.ErrorByteArray[i]); } sb.Append(':'); for (int i = 0; i < cf.ErrorIntArray.Length; i++) { sb.Append(cf.ErrorIntArray[i]); } sb.Append(':'); for (int i = 0; i < cf.SomeFaultArray.Length; i++) { if (cf.SomeFaultArray[i] != null) { sb.Append(cf.SomeFaultArray[i].ID); sb.Append(':'); sb.Append(cf.SomeFaultArray[i].message); } else { sb.Append("null"); } } return(sb.ToString()); }
public void DatacontractFaults(string f) { var host = ServiceHelper.CreateWebHostBuilder <Startup>(_output).Build(); using (host) { host.Start(); var httpBinding = ClientHelper.GetBufferedModeBinding(); var factory = new System.ServiceModel.ChannelFactory <ClientContract.ITestDataContractFault>(httpBinding, new System.ServiceModel.EndpointAddress(new Uri("http://localhost:8080/BasicWcfService/DatacontractFaults.svc"))); var channel = factory.CreateChannel(); var factory2 = new System.ServiceModel.ChannelFactory <ClientContract.ITestDataContractFaultTypedClient>(httpBinding, new System.ServiceModel.EndpointAddress(new Uri("http://localhost:8080/BasicWcfService/DatacontractFaults.svc"))); var channel2 = factory2.CreateChannel(); //test variations int count = 9; try { channel.TwoWayVoid_Method(f); } catch (Exception e) { count--; FaultExceptionValidation(f, e); } try { string s = channel.TwoWay_Method(f); } catch (Exception e) { count--; FaultExceptionValidation(f, e); } try { Stream inputStream = new MemoryStream(); byte[] bytes = Encoding.UTF8.GetBytes(f.ToCharArray()); foreach (byte b in bytes) { inputStream.WriteByte(b); } inputStream.Position = 0; Stream outputStream = channel.TwoWayStream_Method(inputStream); StreamReader sr = new StreamReader(outputStream, Encoding.UTF8); string outputText = sr.ReadToEnd(); Assert.False(true, $"Error, Received Input: {outputText}"); } catch (Exception e) { count--; FaultExceptionValidation(f, e); } try { string response = channel.TwoWayAsync_Method(f).GetAwaiter().GetResult(); Assert.False(true, $"Error, Client received: {response}"); } catch (Exception e) { count--; FaultExceptionValidation(f, e); } try { var fmc = new ClientContract.FaultMsgContract(); fmc.ID = 123; fmc.Name = f; ClientContract.FaultMsgContract fmcResult = channel.MessageContract_Method(fmc); Assert.False(true, $"Error, Client received: {fmcResult.Name}"); } catch (Exception e) { count--; FaultExceptionValidation(f, e); } System.ServiceModel.Channels.Message msgOut = System.ServiceModel.Channels.Message.CreateMessage(System.ServiceModel.Channels.MessageVersion.Soap11, "http://tempuri.org/ITestDataContractFault/Untyped_Method", f); System.ServiceModel.Channels.Message msgIn = channel.Untyped_Method(msgOut); if (msgIn.IsFault) { System.ServiceModel.Channels.MessageFault mf = System.ServiceModel.Channels.MessageFault.CreateFault(msgIn, int.MaxValue); switch (f.ToLower()) { case "somefault": count--; ClientContract.SomeFault sf = mf.GetDetail <ClientContract.SomeFault>(); Assert.Equal(123456789, sf.ID); Assert.Equal("SomeFault", sf.message); break; case "outerfault": count--; ClientContract.OuterFault of = mf.GetDetail <ClientContract.OuterFault>(); sf = of.InnerFault; Assert.Equal(123456789, sf.ID); Assert.Equal("SomeFault as innerfault", sf.message); break; case "complexfault": count--; ClientContract.ComplexFault cf = mf.GetDetail <ClientContract.ComplexFault>(); string exp = "50:This is a test error string for fault tests.:123456789:SomeFault in complexfault:0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127:2147483647-214748364801-150-50:123456789:SomeFault in complexfaultnull234:Second somefault in complexfault"; Assert.Equal(exp, ComplexFaultToString(cf)); break; default: break; } } msgOut = System.ServiceModel.Channels.Message.CreateMessage(System.ServiceModel.Channels.MessageVersion.Soap11, "http://tempuri.org/ITestDataContractFault/Untyped_MethodReturns", f); msgIn = channel.Untyped_MethodReturns(msgOut); if (msgIn.IsFault) { System.ServiceModel.Channels.MessageFault mf = System.ServiceModel.Channels.MessageFault.CreateFault(msgIn, int.MaxValue); switch (f) { case "somefault": count--; ClientContract.SomeFault sf = mf.GetDetail <ClientContract.SomeFault>(); Assert.Equal(123456789, sf.ID); Assert.Equal("SomeFault", sf.message); break; case "outerfault": count--; ClientContract.OuterFault of = mf.GetDetail <ClientContract.OuterFault>(); sf = of.InnerFault; Assert.Equal(123456789, sf.ID); Assert.Equal("SomeFault as innerfault", sf.message); break; case "complexfault": count--; ClientContract.ComplexFault cf = mf.GetDetail <ClientContract.ComplexFault>(); string exp = "50:This is a test error string for fault tests.:123456789:SomeFault in complexfault:0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127:2147483647-214748364801-150-50:123456789:SomeFault in complexfaultnull234:Second somefault in complexfault"; Assert.Equal(exp, ComplexFaultToString(cf)); break; default: break; } } try { string response = channel2.Untyped_Method(f); } catch (Exception e) { count--; FaultExceptionValidation(f, e); } try { string response = channel2.Untyped_MethodReturns(f); } catch (Exception e) { count--; FaultExceptionValidation(f, e); } Assert.Equal(0, count); } }