0

我正在从事一个项目,其中我有三个 C# 项目。一个项目是 C# WCF Soap Server,另一个是 C# 库,第三个是 WPF 项目。

我想要实现的是我的 WPF 项目将故意抛出异常。然后这将调用 C# 库中的一个函数,该函数将获取异常详细信息,然后将数据发布到 Soap 服务器项目,该项目随后将处理数据并将统计信息存储在数据库中。

我遇到的问题是从我的库中调用肥皂方法。我执行svcutil.exe http://localhost:8000/MyProject?wsdl成功创建了客户端类文件。我已将此文件与 output.config 文件一起添加到我的项目中,但是当调用任何肥皂方法时,我会收到以下错误消息。

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 的新手,如果这是一个基本问题但找不到原因,我深表歉意。

感谢您提供的任何帮助。

更新 下面是肥皂服务器的代码

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>
4

1 回答 1

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);

你完成了。

于 2013-08-30T18:01:30.353 回答