Search code examples
c#wcf

System.Net.WebException when trying to consume WCF service


I am developing system composed of WCF service and Xamarin.Forms client app. Seems like my application connects to server just fine (client has status Open when I check before invoking any methods), but after I try to invoke a service method, I am getting System.Net.WebException:

There was no endpoint listening at {0} that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.

And the inner exception is:

System.Net.WebException: There was an error on processing web request: Status code 404(NotFound): Not Found

I can access service via web browsers on both pc and phone with address http://localhost:4408/DatabaseService.svc or http://192.168.8.106:4408/DatabaseService.svc but I get the exception when I am trying to invoke any methods from my app.

My client app is connecting to the host and invokes methods like this:

        public App()
        {
            var binding = new BasicHttpBinding();
            var timeout = new TimeSpan(0, 0, 30);
            binding.SendTimeout = timeout;
            binding.ReceiveTimeout = timeout;
            dbClient = new DatabaseServiceClient(binding, new EndpointAddress("http://192.168.8.106:4408/DatabaseService.svc"));

            dbClient.Test();
        }

My service is hosted by this simple program:

    class Program
    {
        static void Main()
        {
            Uri baseAddress = new Uri("http://192.168.8.106:4408/DatabaseService.svc");
            ServiceHost selfHost = new ServiceHost(typeof(DatabaseService.DatabaseService), baseAddress);

            try
            {
                selfHost.AddServiceEndpoint(typeof(DatabaseService.IDatabaseService), new WSHttpBinding(), "DatabaseService");

                ServiceMetadataBehavior smb = new ServiceMetadataBehavior
                {
                    HttpGetEnabled = true
                };
                selfHost.Description.Behaviors.Add(smb);
                selfHost.Open();

                Console.WriteLine("Host Status: " + selfHost.State);
                Console.WriteLine("Host Addresses: " + selfHost.BaseAddresses.Count);
                foreach (var x in selfHost.BaseAddresses)
                    Console.WriteLine("  - " + x.AbsoluteUri);
                Console.WriteLine("<q to close >");
                string command = "";
                while (command != "q")
                    command = Console.ReadLine();


                selfHost.Close();
                Console.WriteLine("Host shutdown");
                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception thrown:\n" + e.Message);
                Console.ReadLine();
                selfHost.Abort();
            }
        }
    }

Part of my applicationhost.config edited by me is:

            <site name="OutdoorGame" id="2">
                <application path="/" applicationPool="Clr4IntegratedAppPool">
                    <virtualDirectory path="/" physicalPath="F:\Studia\Magisterka\Github\Serwer\OutdoorGame\OutdoorGame" />
                </application>
                <bindings>
                    <binding protocol="http" bindingInformation="*:4408:localhost" />
            <binding protocol="http" bindingInformation="*:4408:127.0.0.1" />
            <binding protocol="http" bindingInformation="*:4408:192.168.8.106" />
                </bindings>
            </site>

I tried to stick to the tips included here: https://learn.microsoft.com/pl-pl/xamarin/xamarin-forms/data-cloud/web-services/wcf and I should also mention, that my services are working just fine, when accesed via ISS Express WCF Test Client.

I think I am missing something very simple but I have no clue what would it be. Any help will be much appreciated.

EDIT1

I probably should also show part of Web.config of the server:

 <system.web>
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <system.serviceModel>
    <bindings>      
      <basicHttpBinding>
        <binding name="basicHttp"
          bypassProxyOnLocal="false"
          hostNameComparisonMode="StrongWildcard"
          maxBufferPoolSize="524288"
          maxReceivedMessageSize="65536"
          messageEncoding="Text"
          textEncoding="utf-8"
          useDefaultWebProxy="true"
          allowCookies="false">
          <security mode="Transport">
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="false" />
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <services>
      <service name="DatabaseService.DatabaseService">
        <endpoint
           address="http://192.168.8.106:4409/DatabaseService.svc" 
           binding="basicHttpBinding" bindingConfiguration="basicHttp"
           contract="DatabaseService.IDatabaseService"/>
      </service>
    </services>
    <protocolMapping>
      <add scheme="http" binding="basicHttpBinding" bindingConfiguration="basicHttp"/>
    </protocolMapping>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true" />
  </system.webServer>

Solution

  • Okey, so since I don't have much time, I basically gave up on this and just went to previous version of my project. Now my files look like this:

    Connecting like this:

    {
                dbClient = new DatabaseServiceClient(new BasicHttpBinding(), new EndpointAddress("http://192.168.8.106:13409/DatabaseService.svc"));
    }
    

    applicationhost.config

                <site name="DatabaseService" id="2">
                    <application path="/" applicationPool="Clr4IntegratedAppPool">
                        <virtualDirectory path="/" physicalPath="F:\Studia\Magisterka\Github\Serwer\OutdoorGame\DatabaseService" />
                    </application>
                    <bindings>
                        <binding protocol="http" bindingInformation="*:13409:localhost" />
                      <binding protocol="http" bindingInformation="*:13409:192.168.8.106" />
                      <binding protocol="http" bindingInformation="*:13409:127.0.0.1" />
                    </bindings>
                </site>
    

    My web.config is basically default.

    Ports used are different because meanwhile I tried to create a new service (thought that could solve the problem) and that could be the thing. Thanks to Ding Peng now I know, that I don't need service host, IIS Express is enough to host it.

    Thanks guys for help.