2011-03-14 22 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

相關問題