尝试使用 WCF 服务时出现 System.Net.WebException

Posted

技术标签:

【中文标题】尝试使用 WCF 服务时出现 System.Net.WebException【英文标题】:System.Net.WebException when trying to consume WCF service 【发布时间】:2021-08-02 02:40:27 【问题描述】:

我正在开发由 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>

【问题讨论】:

您在服务主机上执行new WSHttpBinding(),,这与您在客户端上使用的 BasicHttpBinding 不同。 除了将 WSHttpBinding 更改为 BasicHttpBinding 之外,我还需要做更多的事情吗? 我不知道,这对我来说是显而易见且最容易检查的事情。如果我们很幸运,它可以解决您的问题。如果没有,我们的日子不好过...... 【参考方案1】:

如果您在配置文件中配置了端点信息,则无需使用托管程序配置端点信息。此外,如果使用 WCF 服务应用程序模板创建的项目不需要程序来托管它,则可以直接部署到 IIS。

本项目可以直接部署到IIS,也可以直接在VS中运行。

其次,我建议你使用Add Service Reference来生成客户端:

最后可以通过自动生成的代理类直接调用服务。

【讨论】:

【参考方案2】:

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

这样连接:


            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>

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

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

谢谢大家的帮助。

【讨论】:

以上是关于尝试使用 WCF 服务时出现 System.Net.WebException的主要内容,如果未能解决你的问题,请参考以下文章