C# 尝试使用WCF服务时System.Net.WebException
我正在开发由WCF服务和Xamarin.Forms客户端应用程序组成的系统。看起来我的应用程序与服务器的连接很好(当我在调用任何方法之前进行检查时,客户端的状态为Open),但在我尝试调用服务方法后,我得到System.Net.WebException:C# 尝试使用WCF服务时System.Net.WebException,c#,wcf,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
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 或者,当我试图从我的应用程序调用任何方法时,我会遇到异常
我的客户端应用程序正在连接到主机并调用以下方法:
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();
}
{
dbClient = new DatabaseServiceClient(new BasicHttpBinding(), new EndpointAddress("http://192.168.8.106:13409/DatabaseService.svc"));
}
我的服务由以下简单程序托管:
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();
}
}
}
类程序
{
静态void Main()
{
Uri baseAddress=新Uri(“http://192.168.8.106:4408/DatabaseService.svc");
ServiceHost selfHost=新的ServiceHost(typeof(DatabaseService.DatabaseService),baseAddress);
尝试
{
selfHost.AddServiceEndpoint(typeof(DatabaseService.idatabbaseservice),新的WSHttpBinding(),“DatabaseService”);
ServiceMetadataBehavior smb=新ServiceMetadataBehavior
{
HttpGetEnabled=true
};
selfHost.Description.Behaviors.Add(smb);
selfHost.Open();
Console.WriteLine(“主机状态:+selfHost.State”);
Console.WriteLine(“主机地址:+selfHost.BaseAddresses.Count”);
foreach(selfHost.BaseAddresses中的变量x)
Console.WriteLine(“-”+x.AbsoluteUri);
控制台。写线(“”);
string命令=”;
while(命令!=“q”)
command=Console.ReadLine();
selfHost.Close();
Console.WriteLine(“主机关闭”);
Console.ReadLine();
}
捕获(例外e)
{
WriteLine(“引发异常:\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>
我还应该提到,当通过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中运行
其次,我建议您使用添加服务引用来生成客户端:
最后,您可以通过自动生成的代理类直接调用该服务。好的,因为我没有太多时间,所以我基本上放弃了这一点,只是转到了我项目的前一个版本。现在,我的文件如下所示:
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();
}
{
dbClient = new DatabaseServiceClient(new BasicHttpBinding(), new EndpointAddress("http://192.168.8.106:13409/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();
}
{
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基本上是默认的
使用的端口不同,因为同时我尝试创建一个新的服务(认为可以解决问题),这可能就是问题所在。
感谢丁鹏,现在我知道了,我不需要服务主机,iisexpress就可以托管它了
谢谢大家的帮助。您在servicehost上执行了新建WSHttpBinding(),
,这与您在客户端上使用的BasicHttpBinding不同。除了将WSHttpBinding更改为BasicHttpBinding之外,我还需要做些什么吗?我不知道,这是显而易见的最简单的检查。如果幸运的话,它能解决你的问题。如果不是的话,我们今天会很糟糕。。。