2013-08-29 360 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); 

你完成了。