FaultException does not work when using BasicHttpBinding

So, I noticed that a FaultException does not give me the correct result when I use BasicHttpBinding . When I use WSHttpBinding , the file works.

The problem is that from the WCF service, if I selected a FaultException , as shown below,

 var translations = new List<FaultReasonText> { new FaultReasonText("FaultReasonText 1"), new FaultReasonText("FaultReasonText 2") }; throw new FaultException<MessageServiceFault>(MessageServiceFault.Fault1, new FaultReason(translations)); 

When it reaches the client, the fault.Reason.Translations counter is 1. This means that the first ( FaultReasonText 1 ) returns to the client.

But when I use WSHttpBinding , the score is 2. Where is the problem? Can anyone help me on this.

This gives me a different result when I test the code below with the BasicHttpBinding and WSHttpBinding bindings .

 class Program { static void Main(string[] args) { try { string baseAddress = "http://" + Environment.MachineName + ":8000/Service"; ServiceHost host = new ServiceHost(typeof(MessageService), new Uri(baseAddress)); host.AddServiceEndpoint(typeof(IMessageService), new WSHttpBinding(), ""); host.Open(); Console.WriteLine("Host opened"); ChannelFactory<IMessageService> myChannelFactory = new ChannelFactory<IMessageService>(new WSHttpBinding(), new EndpointAddress(baseAddress)); IMessageService channel = myChannelFactory.CreateChannel(); var response = channel.GetMessage(); } catch (FaultException fault) { fault.Reason.Translations.ToList().ForEach(i => Console.WriteLine(i.Text)); Console.WriteLine(false); } } } [ServiceContract] public interface IMessageService { [OperationContract] [FaultContract(typeof(MessageServiceFault))] string GetMessage(); } public class MessageService : IMessageService { public string GetMessage() { var translations = new List<FaultReasonText> { new FaultReasonText("FaultReasonText 1"), new FaultReasonText("FaultReasonText 2") }; throw new FaultException<MessageServiceFault>(MessageServiceFault.Fault1, new FaultReason(translations)); } } [DataContract] public enum MessageServiceFault { [EnumMember] Fault1, [EnumMember] Fault2 } 

EDIT :

But this article says: “You can provide several different text strings that will be selected depending on the user's language settings. The Translations bucket contains all the text strings and their associated cultural identifiers (FaultReasonText related). If no culture is specified for the reason for the failure or search translation, the intended culture is the current culture of the stream. For example, if you need a translation in "en-UK", we will first search for "en-UK" and then we will search for "en". If we still cannot find a match, we will take the first translation on a list, which can be any.

If so, why in the case of WSHttpBinding it return me 2 FaultReasonText ?

+4
source share
1 answer

To use FaultException, you need to activate SOAP 1.2 on the web service.

BasicHttpBinding uses SOAP 1.1, WSHttpBinding uses SOAP 1.2. Therefore, it works with WSHttpBinding, not BasicHttpBinding.

Instead of using BasicHttpBinding, you'd better use customBindings with textMessageEncoding and httpTransport:

 <customBinding> <binding name="simpleBinding"> <textMessageEncoding messageVersion="Soap12" writeEncoding="utf-8" /> <httpTransport /> </binding> </customBinding> 

If you convert the default default httpbinding with this tool : you will get:

 <!-- generated via Yaron Naveh http://webservices20.blogspot.com/ --> <customBinding> <binding name="NewBinding0"> <textMessageEncoding MessageVersion="Soap11" /> <httpTransport /> </binding> </customBinding> <!-- generated via Yaron Naveh http://webservices20.blogspot.com/ --> 

Source Binding:

 <bindings> <basicHttpBinding> <binding name="NewBinding0" /> </basicHttpBinding> </bindings> 

Try activating SOAP 12 for your service and it will work

0
source

Source: https://habr.com/ru/post/1469334/


All Articles