WCF:如何实际使用WSHttpBinding?我得到了例外

WCF:如何实际使用WSHttpBinding?我得到了例外,wcf,wcf-binding,wshttpbinding,wcf-wshttpbinding,Wcf,Wcf Binding,Wshttpbinding,Wcf Wshttpbinding,我创建了一个WCF服务: Shared.dll: 服务器: 客户机(尝试#1): 客户机(尝试#3):--我将其改为BasicHttpBinding() app.config: 我得到的例外是:内容类型为application/soap+xml;服务不支持字符集=utf-8。客户端和服务绑定可能不匹配。当我在服务器和客户端之间使用共享dll文件时,我看不出它们如何不匹配。BasicHttpBinding起作用,只是WSHttpBinding不起作用(我甚至没有尝试过WS2007HttpBi

我创建了一个WCF服务:

Shared.dll:

服务器:

客户机(尝试#1):

客户机(尝试#3):--我将其改为BasicHttpBinding()

app.config:



我得到的例外是:内容类型为application/soap+xml;服务不支持字符集=utf-8。客户端和服务绑定可能不匹配。当我在服务器和客户端之间使用共享dll文件时,我看不出它们如何不匹配。
BasicHttpBinding
起作用,只是
WSHttpBinding
不起作用(我甚至没有尝试过
WS2007HttpBinding


异常:[
System.ServiceModel.ProtocolException
] {“服务不支持Content-Type application/soap+xml;charset=utf-8。客户端和服务绑定可能不匹配。”} 内部异常:[
System.Net.WebException
]
远程服务器返回错误:(415)无法处理消息,因为内容类型“application/soap+xml;charset=utf-8”不是预期的类型“text/xml;charset=utf-8”…

您需要设置要在WSHttpBinding上使用的安全性

使用示例客户端/服务器WSHttpBinding更新,默认安全性

客户端

服务器


您介意发布完整的异常吗?服务配置是什么?您在哪里托管服务?您是否为服务配置了WSHttpBinding?您的服务器配置是什么样子的?ProtocolException几乎表明您有REST端点,但尝试使用SOAP调用它-或者您的调用会导致错误HTML错误页面。在您的服务项目中-您可以右键单击*.svc文件并执行“在浏览器中查看”吗?只要这不起作用,您就完全不走运了。@marc_s:如果我输入,我会得到标准的“您创建了一个服务”我还没有接触我的web.config文件,所以如果需要更新其中的内容以使我的服务器符合https,我很想知道什么。:)关于如何设置服务器以匹配安全性,有什么建议吗?当我选择一种安全模式(传输)时,它希望我使用
https
而不是
http
,这仅仅是更改uri不符合该要求?传输将需要SSL
[ServiceContract(ConfigurationName = "ICalculator")]
public interface ICalculator
{
    [OperationContract()]
    int Add(int a, int b);
}
[ServiceBehavior()]
public class Calculator : ICalculator
{
    public int Add(int a, int b) { return a + b; }
}
public class CalculatorClient : ClientBase<ICalculator>, ICalculator
{
    private static Binding binding = new WSHttpBinding("MyConfig");
    private static EndpointAddress remoteAddress = new EndpointAddress(...);

    public CalculatorClient() : base(binding, remoteAddress) { }

    public int Add(int a, int b)
    {
        return Channel.Add(a, b); //Exception
    }
}
static void Main(string[] args)
{
    Binding binding = new WSHttpBinding("MyConfig");
    EndpointAddress remoteAddress = new EndpointAddress(...);
    CalculatorClient client = new CalculatorClient(binding, remoteAddress);
    int result = client.Add(5, 4); //Exception
}
static void Main(string[] args)
{
    Binding binding = new BasicHttpBinding("MyConfig");
    EndpointAddress remoteAddress = new EndpointAddress(...);
    CalculatorClient client = new CalculatorClient(binding, remoteAddress);
    int result = client.Add(5, 4); //This works!
}
<system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="MyConfig" /> <!-- did not add anything to this yet -->
      </wsHttpBinding>
    </bindings>
</system.serviceModel>

    class Program
    {
        static void Main(string[] args)
        {
          var calcClient = new CalcClient();
          int i = 1;
          int j = 2;
          Console.WriteLine("Result of Adding {0} and {1} is {2}", i, j, calcClient.Add(i, j));
          Console.ReadKey();
        }
    }

    public class CalcClient : ICalculator
    {
        public CalcClient()
        {
            CalcProxy = ChannelFactory.CreateChannel(new WSHttpBinding(), new EndpointAddress("http://localhost:5050/CalcServer"));
        }

        ICalculator CalcProxy { get; set; }

        public int Add(int a, int b)
        {
            return CalcProxy.Add(a, b);
        }
    }

class Program
    {
        static void Main(string[] args)
        {
            var host = new ServiceHost(typeof (CalcSvr));
            host.AddServiceEndpoint(typeof (ICalculator), new WSHttpBinding(), "http://localhost:5050/CalcServer");
            host.Open();
            Console.WriteLine("Server Running");
            Console.ReadKey();
        }
    }