2

我创建了一个 WCF 服务:

共享.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!
}

应用程序配置:

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


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


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

4

1 回答 1

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();
        }
    }
于 2011-03-14T19:10:25.190 回答