Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法处理邮件,因为内容类型为';应用程序/xml';不是预期的类型';应用程序/soap+;xml;字符集=utf-8';_C#_.net_Wcf - Fatal编程技术网

C# 无法处理邮件,因为内容类型为';应用程序/xml';不是预期的类型';应用程序/soap+;xml;字符集=utf-8';

C# 无法处理邮件,因为内容类型为';应用程序/xml';不是预期的类型';应用程序/soap+;xml;字符集=utf-8';,c#,.net,wcf,C#,.net,Wcf,远程服务器返回错误:(415)无法处理消息,因为内容类型“application/xml”不是预期的类型“application/soap+xml”;字符集=utf-8' 我只是试着启动我自己的主机。我有两个具有基本身份验证的端点。因此,我不得不使用wsHttpBinding来实现这一点。CreateUser端点应使用XML格式和RemoveUser端点-json格式 我附上了我的selfhost app.config、客户端主要功能和合同 服务器app.config 客户端main() 在使用

远程服务器返回错误:(415)无法处理消息,因为内容类型“application/xml”不是预期的类型“application/soap+xml”;字符集=utf-8'

我只是试着启动我自己的主机。我有两个具有基本身份验证的端点。因此,我不得不使用wsHttpBinding来实现这一点。CreateUser端点应使用XML格式和RemoveUser端点-json格式

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

服务器app.config 客户端main()
在使用wshttpbinding创建WCF服务时,该服务基于web服务规范,并使用简单的对象访问协议进行通信。我们可以使用fiddler检查通信细节。

内容类型为Application/soap+xml,而不是Application/xml,请求主体格式基于soap信封。

这种web服务称为SOAP web服务。通常,我们使用客户机代理类调用服务。

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

                throw;
            }

调用服务的方式通常适用于Restful风格的服务。

实例化HttpClient类并自定义请求主体。在这种情况下,我们应该在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()}";
    }
}
结果。

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

内容类型为Application/soap+xml,而不是Application/xml,请求主体格式基于soap信封。

这种web服务称为SOAP web服务。通常,我们使用客户机代理类调用服务。

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

                throw;
            }

调用服务的方式通常适用于Restful风格的服务。

实例化HttpClient类并自定义请求主体。在这种情况下,我们应该在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()}";
    }
}
结果。

按照错误所述,发送正确的内容类型标题。或者更好,生成一个WCF客户端。我无法生成客户端。VS看不到我的合同:(按错误所述执行,发送正确的内容类型标题。或者更好,生成WCF客户端。我无法生成客户端。VS看不到我的合同:(感谢您的回复。但我还有一个问题:如何将basic auth与webHttpBinding一起使用?我们需要将webHttpBinding的客户端凭据类型设置为basic,如有必要,我将发布一个示例。我为此添加了配置。
无pr。)问题是,如果指定了传输安全模式,我们还需要将证书绑定到https地址端口。哦。因此,对于使用xml/json,我需要使用webHttpBinding。然后我需要使用基本身份验证。为此,我需要使用https而不是http。如果我理解正确,请告诉我,在不使用iis和asp的情况下,如何添加https?是否存在问题更好、最简单的解决方案?感谢您的回复。但我还有其他问题:如何将basic auth与webHttpBinding一起使用?我们需要将webHttpBinding的客户端凭据类型设置为basic,如果需要,我将发布一个示例。我为此添加了配置。
没问题,如果指定了传输安全模式,我们还需要将证书绑定到https地址端口。哦。因此,对于使用xml/json,我需要使用webHttpBinding。然后我需要使用基本身份验证。为此,我需要使用https而不是http。如果我理解正确,请告诉我,没有我们,我如何添加https使用iis和asp?有更好、更简单的解决方案吗?
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()}";
    }
}
    netsh http add sslcert ipport=0.0.0.0:4386 certhash=cbc81f77ed01a9784a12483030ccd497f01be71c App
id='{61466809-CD17-4E31-B87B-E89B003FABFA}'