2011-03-14 19 views
2

我创建了一个WCF服务:WCF:如何实际使用WSHttpBinding?我得到异常,而不是

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; charset = utf-8不支持服务 http://localhost/CalculatorService.svc。客户端和服务绑定可能不匹配。当我在我的服务器和客户端之间使用共享的dll文件时,我没有看到它们可能不匹配。的 BasicHttpBinding作品,只是不 WSHttpBinding(我没有甚至企图 WS2007HttpBinding


例外:[System.ServiceModel.ProtocolException] {“内容类型application /肥皂+ xml的;字符集= UTF-8不是由服务所支持的“http://localhost/CalculatorService.svc。”} 内部异常:[System.Net.WebException] 远程服务器返回错误:(415)由于内容类型'application/soap + xml; charset = utf- 8'不是预期的类型'text/xml; charset = utf-8'..

+0

你介意发布完整的例外呢? – 2011-03-14 18:55:47

+0

什么是服务配置? – 2011-03-14 19:11:08

+0

你在哪里托管你的服务?你有为服务配置的WSHttpBinding吗? – Zannjaminderson 2011-03-14 19:12:44

回答

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

有关如何设置服务器以配合安全性的任何建议?当我选择一种安全模式(传输)时,它期望我使用'https'而不是'http',这只是改变uri不符合要求? – michael 2011-03-14 19:15:35

+0

运输将要求SSL – 2011-03-15 14:29:28

相关问题