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

时间:2011-03-14 18:50:09

标签: wcf wcf-binding wshttpbinding wcf-wshttpbinding

我创建了一个WCF服务:

Shared.dll:

[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; }
}

客户(尝试#1):

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
    }
}

客户端(尝试#2): - 注意:我添加了一个服务引用,而不是自己创建一个CalculatorClient(.NET为我创建了它)。

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
}

客户端(尝试#3): - 我将其更改为BasicHttpBinding()而不是

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!
}

的app.config:

<system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="MyConfig" /> <!-- did not add anything to this yet -->
      </wsHttpBinding>
    </bindings>
</system.serviceModel>

<小时/> 我得到的例外是:内容类型application / soap + xml;服务http://localhost/CalculatorService.svc不支持charset = utf-8。客户端和服务绑定可能不匹配。当我在服务器和客户端之间使用共享的dll文件时,我看不出它们是如何不匹配的。 BasicHttpBinding有效,而不是WSHttpBinding(我甚至没有尝过WS2007HttpBinding


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

1 个答案:

答案 0 :(得分:2)

您需要设置要在 WSHttpBinding

上使用的安全性

http://msdn.microsoft.com/en-us/library/ms731884(v=VS.90).aspx

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

客户端


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