1

The remote server returned an error: (415) Cannot process the message because the content type 'application / xml' was not the expected type 'application / soap + xml; charset = utf-8 '

I just try to launch my self host. I have 2 endpoints with Basic Authentication. So I had to use wsHttpBinding for that. CreateUser endpoint should use XML format and RemoveUser endpoint - json format.

I attached my selfhost app.config, client main function and contract.

server app.config

<services> <service name="Web.Service.Core.Services.UserContract" behaviorConfiguration="AuthBehavior" > <endpoint address="CreateUser" binding="wsHttpBinding" bindingNamespace="http://localhost/Auth/" contract="Web.Service.Library.Contracts.IUserContract" /> <endpoint address="RemoveUser" binding="wsHttpBinding" contract="Web.Service.Library.Contracts.IUserContract" /> 

IUserContract.cs

[ServiceContract(Namespace = "http://localhost/Auth/", ProtectionLevel = ProtectionLevel.None)] [XmlSerializerFormat] public interface IUserContract { [OperationContract] [WebInvoke(Method = "POST", UriTemplate = "Auth/CreateUser", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped)] Response CreateUser(Stream xml); [OperationContract] [WebInvoke(Method = "POST", UriTemplate = "Auth/RemoveUser", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)] Response RemoveUser(Stream stream); 

client main()

var webRequest = (HttpWebRequest) WebRequest.Create(CreateUserUrl); webRequest.Method = "POST"; webRequest.ContentType = "application/xml"; webRequest.ContentLength = data.Length; var rqStream = webRequest.GetRequestStream(); rqStream.Write(data, 0, data.Length); rqStream.Close(); var webResponse = webRequest.GetResponse(); var rsStream = webResponse.GetResponseStream(); var responseXml = new StreamReader(rsStream); var s = responseXml.ReadToEnd(); 
2
  • Do as the error says, send the proper content-type header. Or better, generate a WCF client. Commented Aug 14, 2019 at 17:46
  • I can not to generate a client. VS does not see my contracts :( Commented Aug 14, 2019 at 18:29

1 Answer 1

0

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.
enter image description here
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.
enter image description here

Sign up to request clarification or add additional context in comments.

13 Comments

Thank you for the response. But i have additional question: How can i use basic auth with webHttpBinding ?
We need to set up the client credential type of the webhttpbinding to basic, if necessary, l will post an example.
I added configuration for this. <endpoint address="CreateUser" binding="webHttpBinding" bindingConfiguration="WebHttpBinding" contract="Web.Service.Library.Contracts.IUserContract" /> and <webHttpBinding> <binding name="WebHttpBinding"> <security mode="Transport"> <transport clientCredentialType="Basic" /> </security> </binding> </webHttpBinding>
No problem, we also need to bind a certificate to the https address port if the transport security mode is specified.
Oh. So, for using xml/json i need to use webHttpBinding. Then i need to use Basic Authentication. For that i need use https instead http.If I understand correctly please tell me, how can i add https without using iis and asp ? Is there a better and easiest solution?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.