简体   繁体   中英

Calling a soap service from C# client

I am working on a project where I have three C# projects. One project is a C# WCF Soap Server, the other being C# library and the third is a WPF project.

What I am trying to achieve is my WPF project is going to purposely throw an exception. This will then call a function within the C# library that will get the exception details and then post the data to the Soap server project, which will then process the data and store the stats in the database.

The problem I have is calling the soap methods from my library. I executed svcutil.exe http://localhost:8000/MyProject?wsdl which successfully created the client class file. I've add this file to my project along with the output.config file but when any of the soap methods is called, I get the following error message.

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.

I'm new to C# and Soap so apologise if this is a basic question but can't find the cause of this.

Thank for any help you can provide.

UPDATE Below is the code for the soap server

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

Below is how I am using the client class file

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

Below is the output.config file that is created from the svcutil.exe application

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

Am converting my comments to answer on request as it helped.

Anybody facing problems like this can follow these steps

  1. Make sure your have a config file with endpoint and binding defined.
  2. If yes, make sure your endpoint name matches with your source code and config. In this case BasicHttpBinding_ISoapInterface .
  3. If yes, make sure you have copied config file to ApplicationDirectory or you set CopyAlways to true.
  4. If yes, make sure your config file is updated in ApplicationDirectory

If above steps doesn't work for some reason, then do it from your code. That should work.

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

You're done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM