我创建了WCF Self Hosted服务,我在C#控制台应用程序中托管它并且它运行良好但是问题是当我将localhost URL放在浏览器中然后它不会浏览+我无法添加它对Webforms客户端应用程序的引用,它会引发错误:
There was an error downloading 'http://localhost:8084/_vti_bin/ListData.svc/$metadata'.
Unable to connect to the remote server
No connection could be made because the target machine actively refused it 127.0.0.1:8084
Metadata contains a reference that cannot be resolved: 'http://localhost:8084/'.
There was no endpoint listening at http://localhost:8084/ that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
Unable to connect to the remote server
No connection could be made because the target machine actively refused it 127.0.0.1:8084
If the service is defined in the current solution, try building the solution and adding the service reference again.
我在VS 2013的单独实例中运行托管,在另一个托管中运行客户端作为管理员,但它不起作用,为什么?
CODE:
托管申请:
namespace HellloServiceHost
{
class Program
{
static void Main(string[] args)
{
using(ServiceHost sh = new ServiceHost(typeof(HellloService.HelloService)))
{
sh.Open();
Console.WriteLine("Host Started @"+ System.DateTime.Now.ToShortDateString());
Console.ReadLine();
}
}
}
}
的app.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="HellloService.HelloService" behaviorConfiguration="MexBehaviour" >
<endpoint address="HelloService" binding="basicHttpBinding" contract="HellloService.IHelloService"></endpoint>
<endpoint address="HelloService" binding="netTcpBinding" contract="HellloService.IHelloService"></endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8084/"/>
<add baseAddress="net.tcp://localhost:8085/"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors >
<behavior name="MexBehaviour">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
答案 0 :(得分:0)
您的配置文件建议服务地址为:
http://localhost:8084/HelloService
尝试使用该URL获取服务引用。如果仍然无法正常工作,请尝试在服务启动时打印端点地址。
答案 1 :(得分:0)
每当我在控制台中托管服务时,我都会在sh.Open()方法之后将ServiceHost的所有端点地址转储到控制台:
foreach (var endpoint in sh.Description.Endpoints)
{
Console.WriteLine(endpoint.Address.Uri);
}
这适用于通过.config文件配置的端点,或者如果您在代码中创建它们。
答案 2 :(得分:0)
在配置(App.config)中添加此终结点
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
错误将得到解决。
答案 3 :(得分:0)
您还可以尝试在Visual Studio中启动WCF服务,然后从“调试”菜单中选择“全部分离”。这样可以使服务继续在后台运行,但是打开Visual Studio可以添加服务引用。