2017-01-11 191 views
0

我從提供的WSDL文件創建了WCF服務引用。在C#中我創建了代理客戶端的一個實例,一個基本的約束力,並要求所需的方法:Web服務調用錯誤

public static bool main() 
    { 
     Debugger.Launch(); 
     var binding = new BasicHttpsBinding(); 

     binding.Security.Mode = BasicHttpsSecurityMode.Transport; 
     binding.TextEncoding = System.Text.Encoding.UTF8; 

     var remoteAddress = new System.ServiceModel.EndpointAddress("https://tester.mysite.de:8443/webservice/OrderNumber"); 

     using (var orderNumberClient = new orderNumberClient(new System.ServiceModel.BasicHttpBinding(BasicHttpSecurityMode.Transport), remoteAddress)) 
     { 
      string IDSystem = "123"; 
      string IDOSystem = "abc"; 

      //set timeout 
      orderNumberClient.Endpoint.Binding.SendTimeout = new TimeSpan(0, 0, 0, 10000); 
      orderNumberClient.ClientCredentials.UserName.UserName = "test"; 
      orderNumberClient.ClientCredentials.UserName.Password = "test"; 

      //call web service method 
      string productResponse = orderNumberClient.getNewOrderNumber(IDSystem, "01", IDOSystem); ; 

      MessageBox.Show(productResponse); 
     } 

     return true; 
    } 

不幸的是我收到一個相當無用的錯誤時,我稱之爲「getNewOrderNumber」的方法:

System.Reflection.TargetInvocationException:Ein Aufrufziel hat einen Ausnahmefehler verursacht。 ---> System.ServiceModel.FaultException: WebService的處理異常

服務器堆棧跟蹤:

貝 System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime 操作,ProxyRpc & RPC)

bei System.ServiceModel.Channels.ServiceChannel.Call(String action, 單向布爾值,ProxyOperationRuntime操作,Object [] ins, Object []輸出,TimeSpan超時)

貝 System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage 包括MethodCall,ProxyOperationRuntime操作)

...

如在SOAPUI正常工作這不是在Web服務端錯誤,我可能在裝訂中錯過了一些東西?

希望有人對網絡服務有更多的瞭解可以讓我們瞭解根本原因。

+0

我同意這是一個相當模糊的例外。我相信這個特定的應該有一個內部異常,會更有幫助。注意:它可能嵌套幾層。繼續檢查你的內部例外,直到你沒有更多。 – Vahlkron

+0

在內部例外中沒有什麼可以看到的物質:幸運的是,我從客戶端發現我錯過了所需的標題!我會發佈一個答案。 –

回答

0

事實證明,我並沒有發送「授權」標題與呼叫。爲了克服這個問題,我用的OperationContextScope類在進行實際的Web服務方法調用之前所需的HttpRequestMessageProperty添加到代理實例內部通道:

   using (OperationContextScope scope = new OperationContextScope(orderNumberClient.InnerChannel)) 
       { 
        var httpRequestProperty = new HttpRequestMessageProperty(); 
        httpRequestProperty.Headers[System.Net.HttpRequestHeader.Authorization] = "Basic " + 
        Convert.ToBase64String(Encoding.ASCII.GetBytes(orderNumberClient.ClientCredentials.UserName.UserName + ":" + 
        orderNumberClient.ClientCredentials.UserName.Password)); 
        OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty; 

        string Response = orderNumberClient.getNewOrderNumber(IDSystem, "01", IDOSystem); 
       }