我最近开始使用WCF,我遇到了一个问题,我只是没有线索如何解决。 我使用服务主机启动WCF服务,但是当我在浏览器中使用URI时,它不显示服务的合同,当我尝试使用ChannelFactory连接到它时,它会发出异常。WCF错误 - 无法获取服务端点
我在Visual Studio 2017中创建了项目,并没有对配置文件做任何事情,excpet改变了基地址。服务接口和实现都在根项目“文件夹”中,我试过禁用防火墙甚至是防病毒,但似乎没有任何效果。
的App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="TaskExecutor.Exec">
<endpoint address="" binding="basicHttpBinding" contract="TaskExecutor.IExec">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8001/TaskExecutor/Exec/" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
服务接口:
namespace TaskExecutor
{
[ServiceContract]
public interface IExec
{
[OperationContract]
void DoWork();
}
}
服务实现:
namespace TaskExecutor
{
public class Exec : IExec
{
public void DoWork()
{
Console.WriteLine("Doing work.");
}
}
}
计划推出的服务:
using (ServiceHost host = new ServiceHost(typeof(Exec)))
{
host.Open();
Console.WriteLine("exec service started at {0}", host.BaseAddresses[0]);
}
Console.WriteLine("Press any key to end...");
Console.ReadLine();
启动程序显示的消息后:
exec service started at http://localhost:8001/TaskExecutor/Exec/
Press any key to end...
服务客户端代码如下:
EndpointAddress endpoint = new EndpointAddress("http://localhost:8001/TaskExecutor/Exec/");
BasicHttpBinding binding = new BasicHttpBinding();
ChannelFactory<IExec> channelFactory = new ChannelFactory<IExec>(binding, endpoint);
IExec proxy = channelFactory.CreateChannel();
proxy.DoWork();
而且它提供了异常:
System.ServiceModel.EndpointNotFoundException occurred
HResult=0x80131501
Message=There was no endpoint listening at http://localhost:8001/TaskExecutor/Exec/ that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
Inner Exception 1:
WebException: Unable to connect to the remote server
Inner Exception 2:
SocketException: No connection could be made because the target machine actively refused it
我认真地穿上”不知道该怎么做,任何帮助都会很棒。
非常感谢!
去这个网站一个简单的教程一步一步在WCF - https://www.tutorialspoint.com/wcf/wcf_creating_service.htm为输入 – MethodMan
谢谢,但它没有太大的帮助,我一直在做的东西大部分是那个教程 – gqmartins