从C#客户端调用soap服务

时间:2013-08-29 17:13:52

标签: c# wcf soap

我正在开展一个项目,我有三个C#项目。一个项目是C#WCF肥皂服务器,另一个是C#库,第三个是WPF项目。

我想要实现的是我的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的新手,如果这是一个基本问题但是找不到原因,请道歉。

感谢您提供的任何帮助。

更新 以下是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 support@boardiesitsolutions.com",
                    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 support@boardiesitsolutions.com 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 support@boardiesitsolutions.com");
                Console.ResetColor();
            }

            client.Close();

以下是从svcutil.exe应用程序

创建的output.config文件
<?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>

1 个答案:

答案 0 :(得分:1)

我正在将我的评论转换为请求,因为它有所帮助。

任何遇到此类问题的人都可以按照以下步骤进行操作

  1. 确保您的配置文件中包含endpointbinding
  2. 如果是,请确保您的端点名称与源代码和配置匹配。在这种情况下BasicHttpBinding_ISoapInterface
  3. 如果是,请确保已将配置文件复制到ApplicationDirectory或将CopyAlways设置为true。
  4. 如果是,请确保您的配置文件已在ApplicationDirectory
  5. 中更新

    如果上述步骤因某些原因无效,请从代码中执行。这应该有用。

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

    你已经完成了。