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

时间:2021-05-11 17:30:59

标签: c# wcf

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

我可以通过电脑和手机上的网络浏览器访问服务,地址为 http://localhost:4408/DatabaseService.svc 或 https://docs.microsoft.com/pl-pl/xamarin/xamarin-forms/data-cloud/web-services/wcf 但是当我尝试从我的应用调用任何方法时出现异常.

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

        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>

我尝试遵循此处包含的提示:{{3}} 并且我还应该提到,当通过 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>

2 个答案:

答案 0 :(得分:0)

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

enter image description here

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

其次,我建议您使用添加服务引用来生成客户端:

enter image description here

enter image description here

enter image description here

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

答案 1 :(得分:0)

好吧,因为我没有太多时间,我基本上放弃了这个,只是去我的项目的以前版本。现在我的文件看起来像这样:

像这样连接:

{
            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就足够了。

谢谢大家的帮助。