Error consuming webservice, content type application/xop+xml does not match expected type text/xml
For anyone suffering from the same problem; I've found a solution for consuming the web service as a Service Reference (WCF). The BasicHttpBinding.MessageEncoding property needs setting to "Mtom".
Here's a snippet of the required config setting:
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding messageEncoding="Mtom">
</binding>
</basicHttpBinding>
</bindings>
</system.serviceModel>
</configuration>
Edit: If you are having the same issue with a custom binding please refer to the answer from @robmzd.
I still haven't found a solution for consuming it as an old style Web Reference yet.
Cannot process the message because the content type 'application / xml' was not the expected type 'application / soap + xml; charset = utf-8 '
When we use wshttpbinding to create WCF service, the service based on the web service speficification, and use Simple Object Access Protocol to communicate. We could check the communication details by using fiddler.
The content-type is Application/soap+xml instead of the Application/xml, and the request body format based on SOAP envelop.
https://en.wikipedia.org/wiki/SOAP
This kind of web service is called SOAP web service. Ordinarily, we call the service by using the client proxy class.
ServiceReference1.ServiceClient client = new ServiceClient();
try
{
var result = client.GetData();
Console.WriteLine(result);
}
catch (Exception)
{
throw;
}
https://learn.microsoft.com/en-us/dotnet/framework/wcf/accessing-services-using-a-wcf-client
The way you call a service usually applies to Restful-style service.
https://learn.microsoft.com/en-us/azure/architecture/best-practices/api-design
Instantiate the HttpClient class and custom the request body. Under this circumstance, we should use WebHttpBinding in WCF to create the service. Please refer to my reply.
How to fix "ERR_ABORTED 400 (Bad Request)" error with Jquery call to C# WCF service?
Feel free to let me know if there is anything I can help with.
Updated.
class Program
{
/// <summary>
/// https webhttpbinding.
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
Uri uri = new Uri("https://localhost:4386");
WebHttpBinding binding = new WebHttpBinding();
binding.Security.Mode = WebHttpSecurityMode.Transport;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
using (WebServiceHost sh = new WebServiceHost(typeof(TestService), uri))
{
sh.AddServiceEndpoint(typeof(ITestService), binding, "");
ServiceMetadataBehavior smb;
smb = sh.Description.Behaviors.Find<ServiceMetadataBehavior>();
if (smb == null)
{
smb = new ServiceMetadataBehavior()
{
//HttpsGetEnabled = true
};
sh.Description.Behaviors.Add(smb);
}
Binding mexbinding = MetadataExchangeBindings.CreateMexHttpsBinding();
sh.AddServiceEndpoint(typeof(IMetadataExchange), mexbinding, "mex");
sh.Opened += delegate
{
Console.WriteLine("service is ready");
};
sh.Closed += delegate
{
Console.WriteLine("service is closed");
};
sh.Open();
Console.ReadLine();
sh.Close();
}
}
}
[ServiceContract]
public interface ITestService
{
[OperationContract]
[WebGet(ResponseFormat =WebMessageFormat.Json)]
string GetResult();
}
[ServiceBehavior]
public class TestService : ITestService
{
public string GetResult()
{
return $"Hello, busy World. {DateTime.Now.ToShortTimeString()}";
}
}
Bind a certificate to the port(Powershell command).
netsh http add sslcert ipport=0.0.0.0:4386 certhash=cbc81f77ed01a9784a12483030ccd497f01be71c App
id='{61466809-CD17-4E31-B87B-E89B003FABFA}'
Result.
MTOM - Root content type was always text/xml
Able to resolve this issue by using saaj-impl jar.
pom.xml
<dependency>
<groupId>com.sun.xml.messaging.saaj</groupId>
<artifactId>saaj-impl</artifactId>
<version>1.3.16</version>
<scope>provided</scope>
</dependency>
dispatcher-servlet.xml
<bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory">
<property name="messageFactory">
<bean class="com.sun.xml.internal.messaging.saaj.soap.ver1_1.SOAPMessageFactory1_1Impl" />
</property>
</bean>
Related Topics
How to Use C# 8 with Visual Studio 2017
C#: How to Edit Items and Subitems in a Listview
.Net: Unable to Cast Object to Interface It Implements
An Integer Array as a Key for Dictionary
How to Use Dependency Injection in a .Net Core Actionfilterattribute
Wpf Datagrid Get Selected Cell Value
How to Get the Name of Color While Having Its Rgb Value in C#
How to Compare Two Images Using Byte Arrays
Regex That Matches a Newline (\N) in C#
Convert.Toint32() a String with Commas
Regular Expression to Get the Src of Images in C#
Why Does the Async Keyword Exist
How to Hide Public Methods from Intellisense
Piping in a File on the Command-Line Using System.Diagnostics.Process