首页 > 解决方案 > 尝试使用 WCF 服务时出现 System.Net.WebException

问题描述

我正在开发由 WCF 服务和 Xamarin.Forms 客户端应用程序组成的系统。似乎我的应用程序连接到服务器很好(当我在调用任何方法之前检查时客户端的状态为打开),但是在我尝试调用服务方法之后,我得到 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.

内部的例外是:

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

我可以通过 PC 和手机上的 Web 浏览器访问服务,地址为 http://localhost:4408/DatabaseService.svc 或http://192.168.8.106:4408/DatabaseService.svc但是当我尝试调用时出现异常我的应用程序中的任何方法。

我的客户端应用程序正在连接到主机并调用如下方法:

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

我的服务由这个简单的程序托管:

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

我编辑的 applicationhost.config 的一部分是:

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

我试图坚持这里包含的提示:https ://docs.microsoft.com/pl-pl/xamarin/xamarin-forms/data-cloud/web-services/wcf我还应该提到,我的服务正在运行很好,当通过 ISS Express WCF 测试客户端访问时。

我想我错过了一些非常简单的东西,但我不知道会是什么。任何帮助都感激不尽。

编辑1

我可能还应该显示服务器的 Web.config 的一部分:

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

标签: c#wcf

解决方案


好的,所以由于我没有太多时间,我基本上放弃了这个,只是去了我项目的以前版本。现在我的文件如下所示:

像这样连接:

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

应用程序主机配置

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

我的 web.config 基本上是默认的。

使用的端口不同,因为同时我尝试创建一个新服务(认为可以解决问题),这可能就是问题所在。感谢丁鹏现在我知道,我不需要服务主机,IIS Express 足以托管它。

谢谢大家的帮助。


推荐阅读