2013-08-29 531 views
0

我正在開發一個項目,其中有三個C#項目。一個項目是C#WCF Soap Server,另一個是C#庫,第三個是WPF項目。從C#客戶端調用soap服務

我想要實現的是我的WPF項目將故意拋出異常。然後,它將調用C#庫中的一個函數,該函數將獲取異常詳細信息,然後將數據發佈到Soap服務器項目,Soap服務器項目將處理數據並將數據存儲在數據庫中。

我遇到的問題是從我的庫中調用soap方法。我執行了svcutil.exe http://localhost:8000/MyProject?wsdl,它成功創建了客戶端類文件。我已經將這個文件與output.config文件一起添加到我的項目中,但是當任何soap方法被調用時,我會收到以下錯誤消息。

Invalid Operation Exception

Could not find default endpoint element that references contract 'CritiMonSoapService.ISoapInterface' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element. 

我是新的C#和SOAP所以道歉,如果這是一個基本的問題,但無法找到的這個原因。

感謝您提供的任何幫助。

UPDATE 下面是SOAP服務器

try 
      { 
       if (Environment.GetEnvironmentVariable("MONO_STRICT_MS_COMPLIANT") != "yes") 
       { 
        Environment.SetEnvironmentVariable("MONO_STRICT_MS_COMPLIANT", "yes"); 
       } 
       if (String.IsNullOrEmpty(soapServerUrl)) 
       { 
        string message = "Not starting Soap Server: URL or Port number is not set in config file"; 
        library.logging(methodInfo, message); 
        library.setAlarm(message, CommonTasks.AlarmStatus.Medium, methodInfo); 
        return; 
       } 

       Console.WriteLine("Soap Server URL: {0}", soapServerUrl); 
       baseAddress = new Uri(soapServerUrl); 
       host = new ServiceHost(soapHandlerType, baseAddress); 
       BasicHttpBinding basicHttpBinding = new BasicHttpBinding(); 

       //basicHttpBinding.Namespace = "http://tempuri.org/"; 


       var meta = new ServiceMetadataBehavior() 
       { 
        HttpGetEnabled = true, 
        HttpGetUrl = new Uri("", UriKind.Relative), 
        //HttpGetBinding = basicHttpBinding, 
       }; 
       //meta.MetadataExporter.PolicyVersion = PolicyVersion.Policy15; 

       host.Description.Behaviors.Add(meta); 

       host.AddServiceEndpoint(soapManagerInterface, basicHttpBinding, soapServerUrl); 
       host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex"); 

       var debugBehaviour = new ServiceDebugBehavior() 
       { 
        HttpHelpPageEnabled = true, 
        HttpHelpPageUrl = new Uri("", UriKind.Relative), 
        IncludeExceptionDetailInFaults = true, 
        //HttpHelpPageBinding = basicHttpBinding, 
       }; 

       host.Description.Behaviors.Remove(typeof(ServiceDebugBehavior)); 
       host.Description.Behaviors.Add(debugBehaviour); 
       host.Opened += new EventHandler(host_Opened); 
       host.Faulted += new EventHandler(host_Faulted); 
       host.Closed += new EventHandler(host_Closed); 
       host.UnknownMessageReceived += new EventHandler<UnknownMessageReceivedEventArgs>(host_UnknownMessageReceived); 
       host.Open(); 
      } 
      catch (InvalidOperationException ex) 
      { 
       library.logging(methodInfo, string.Format("Invalid Operation Exception: {0}", ex.Message)); 
      } 
      catch (ArgumentException ex) 
      { 
       library.logging(methodInfo, string.Format("Argument Exception: {0}", ex.Message)); 
      } 
      catch (NotImplementedException ex) 
      { 
       library.logging(methodInfo, string.Format("Not implemented: {0}, Inner: {1}", ex.Message, ex.StackTrace)); 
      } 
      catch (System.ServiceModel.FaultException ex) 
      { 
       library.logging(methodInfo, string.Format("Fault Exception: {0}", ex.Message)); 
      } 
      catch (System.ServiceModel.EndpointNotFoundException ex) 
      { 
       library.logging(methodInfo, string.Format("End point not found exception: {0}", ex.Message)); 
      } 
      catch (System.ServiceModel.ActionNotSupportedException ex) 
      { 
       library.logging(methodInfo, string.Format("Action not supported exception: {0}", ex.Message)); 
      } 
      catch (System.ServiceModel.AddressAccessDeniedException ex) 
      { 
       library.logging(methodInfo, string.Format("Address access denied exception: {0}", ex.Message)); 
      } 
      catch (System.ServiceModel.AddressAlreadyInUseException ex) 
      { 
       library.logging(methodInfo, string.Format("Address already in use exception: {0}", ex.Message)); 
      } 
      catch (System.ServiceModel.CommunicationException ex) 
      { 
       library.logging(methodInfo, string.Format("Communication Exception: {0}", ex.Message)); 
      } 
      catch (Exception ex) 
      { 
       library.logging(methodInfo, string.Format("General Exception: {0}", ex.Message)); 
      } 

下面的代碼是我如何使用客戶端類文件

CritiMonSoapService.SoapInterfaceClient client = new CritiMonSoapService.SoapInterfaceClient(); 
      string result = client.checkAppIDExists(appID); 

      if (result.Equals("200 OK")) 
      { 
       CritiMon.isInitialised = true; 
       CritiMon.appIDValid = true; 
       Console.WriteLine("CritiMon successfully initialised"); 
      } 
      else if (result.Contains("App ID does not exist")) 
      { 
       Console.ForegroundColor = ConsoleColor.Red; 
       Console.WriteLine("App ID {0} does not exist. Please login to CritiMon and check your app ID. If you have any issues, then please contact [email protected]", 
        CritiMon.appID); 
       Console.ResetColor(); 
      } 
      else if (result.Contains("Failed to check app ID exists")) 
      { 
       Console.ForegroundColor = ConsoleColor.Red; 
       Console.WriteLine("Unable to check your App ID at this time. Please contact [email protected] if you continue to see this error"); 
       Console.ResetColor(); 
      } 
      else if (result.Equals("App ID Not Sent")) 
      { 
       Console.ForegroundColor = ConsoleColor.Red; 
       Console.WriteLine("No App ID was passed into CritiMon.Initialise() function. If you believe you are seeing this in error please send us an email to [email protected]"); 
       Console.ResetColor(); 
      } 

      client.Close(); 

下面是創建output.config文件來自svcutil.exe應用程序

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <system.serviceModel> 
     <bindings> 
      <basicHttpBinding> 
       <binding name="BasicHttpBinding_ISoapInterface" /> 
      </basicHttpBinding> 
     </bindings> 
     <client> 
      <endpoint address="http://localhost:8000/CritiMon" binding="basicHttpBinding" 
       bindingConfiguration="BasicHttpBinding_ISoapInterface" contract="ISoapInterface" 
       name="BasicHttpBinding_ISoapInterface" /> 
     </client> 
    </system.serviceModel> 
</configuration> 
+0

用於創建SoapClient的郵政編碼 –

+0

@SriramSakthivel,我已根據要求發佈了代碼 – Boardy

+0

不確定此墊子是否可以解決問題。試一下。創建客戶端配置名稱爲'CritiMonSoapService.SoapInterfaceClient client = new CritiMonSoapService.SoapInterfaceClient(「clienBasicHttpBinding_ISoapInterface」);' –

回答

1

正在轉換我的意見,並根據要求給予回覆。

任何人都面臨這樣的問題,可以按照以下步驟

  1. 確保您有一個配置文件與endpointbinding定義。
  2. 如果是,請確保您的端點名稱與您的源代碼和配置相匹配。在這種情況下BasicHttpBinding_ISoapInterface
  3. 如果是,請確保已將配置文件複製到ApplicationDirectory或將CopyAlways設置爲true。
  4. 如果是,請確保您的配置文件在ApplicationDirectory

更新如果上述步驟不出於某種原因,那麼它從你的代碼。這應該工作。

EndpointAddress endpointAdress = new EndpointAddress(path to your web service/wcf service); 
BasicHttpBinding binding = new BasicHttpBinding(); 
YourSoapClient client = new YourSoapClient(binding, endpointAddress); 

你完成了。