System.Net.WebException 尝试使用 WCF 服务时
System.Net.WebException when trying to consume WCF service
我正在开发由 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 和 phone 上的网络浏览器访问服务,地址为 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>
如果您在配置文件中配置了端点信息,则无需使用托管程序配置端点信息。而且,如果使用WCF服务应用模板创建的项目不需要程序托管,直接部署到IIS即可。
这个项目可以直接部署到IIS或者直接在VS中运行
其次,我建议您使用添加服务引用来生成客户端:
终于可以通过自动生成的代理直接调用服务了class。
好吧,因为我没有太多时间,所以我基本上放弃了这个,只去了我项目的前一个版本。现在我的文件看起来像这样:
这样连接:
{
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基本都是默认的
使用的端口不同,因为同时我尝试创建一个新服务(认为可以解决问题),这可能就是问题所在。
感谢 Ding Peng 现在我知道,我不需要服务主机,IIS Express 足以托管它。
感谢大家的帮助。
我正在开发由 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 和 phone 上的网络浏览器访问服务,地址为 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>
如果您在配置文件中配置了端点信息,则无需使用托管程序配置端点信息。而且,如果使用WCF服务应用模板创建的项目不需要程序托管,直接部署到IIS即可。
这个项目可以直接部署到IIS或者直接在VS中运行
其次,我建议您使用添加服务引用来生成客户端:
终于可以通过自动生成的代理直接调用服务了class。
好吧,因为我没有太多时间,所以我基本上放弃了这个,只去了我项目的前一个版本。现在我的文件看起来像这样:
这样连接:
{
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基本都是默认的
使用的端口不同,因为同时我尝试创建一个新服务(认为可以解决问题),这可能就是问题所在。 感谢 Ding Peng 现在我知道,我不需要服务主机,IIS Express 足以托管它。
感谢大家的帮助。