无法处理邮件,因为内容类型 'application / xml' 不是预期的类型 'application / soap + xml; charset = utf-8 '

Cannot process the message because the content type 'application / xml' was not the expected type 'application / soap + xml; charset = utf-8 '

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 '

我只是想启动我的自助主机。我有 2 个具有基本身份验证的端点。所以我不得不为此使用 wsHttpBinding 。 CreateUser 端点应使用 XML 格式和 RemoveUser 端点 - json 格式。

我附上了我的 selfhost app.config、客户端主要功能和合约。

服务器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);

客户端主函数()

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();

当我们使用wshttpbinding创建WCF服务时,该服务基于web服务规范,并使用简单对象访问协议进行通信。我们可以使用fiddler查看通信细节。

content-type是Application/soap+xml而不是Application/xml,请求体格式基于SOAP envelop.
https://en.wikipedia.org/wiki/SOAP
这种网络服务称为 SOAP 网络服务。通常,我们使用客户端代理调用服务 class.

   ServiceReference1.ServiceClient client = new ServiceClient();
            try
            {
                var result = client.GetData();
                Console.WriteLine(result);
            }
            catch (Exception)
            {

                throw;
            }

https://docs.microsoft.com/en-us/dotnet/framework/wcf/accessing-services-using-a-wcf-client
您调用服务的方式通常适用于 Restful 风格的服务。
https://docs.microsoft.com/en-us/azure/architecture/best-practices/api-design
实例化 HttpClient class 并自定义请求正文。在这种情况下,我们应该使用WCF中的WebHttpBinding来创建服务。请参考我的回复

如果有什么我可以帮忙的,请随时告诉我。

已更新。

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()}";
    }
}

将证书绑定到端口(Powershell 命令)。

    netsh http add sslcert ipport=0.0.0.0:4386 certhash=cbc81f77ed01a9784a12483030ccd497f01be71c App
id='{61466809-CD17-4E31-B87B-E89B003FABFA}'

结果。