【发布时间】:2014-07-26 14:08:46
【问题描述】:
我正在尝试构建一个测试应用程序来测试 wcf 并了解更多信息:
我编写了一个 WCF 服务并将其托管在 Windows 服务中。这是我来自 Windows 服务的app.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="WCFLibrary.CalculatorService"
behaviorConfiguration="CalculatorServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/ServiceModelSamples/service"/>
</baseAddresses>
</host>
<!-- this endpoint is exposed at the base address provided by host: http://localhost:8000/ServiceModelSamples/service -->
<endpoint
address=""
binding="wsHttpBinding"
contract="WCFLibrary.ICalculator" />
<!-- the mex endpoint is exposed at http://localhost:8000/ServiceModelSamples/service/mex -->
<endpoint
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="CalculatorServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="False"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
现在我想构建一个客户端应用程序来使用 WCF 服务。但是当我转到Add Service Reference 时,它给了我一个错误,它无法到达端点。我可以看到从服务管理器运行的服务。我通过从 VS 进行调试来进行测试。在调试期间,客户端能够看到端点。但是如果我停止并 Debug 回到服务管理器,客户端就看不到端点。
可能出了什么问题?请问有什么线索吗?
EDIT1
Windows 服务 (Service1.cs)
namespace WindowsService
{
public partial class Service1 : ServiceBase
{
public ServiceHost serviceHost = null;
public Service1()
{
InitializeComponent();
}
public void onDebug()
{
OnStart(null);
}
protected override void OnStart(string[] args)
{
if (serviceHost != null)
{
serviceHost.Close();
}
// Create a ServiceHost for the CalculatorService type and
// provide the base address.
serviceHost = new ServiceHost(typeof(CalculatorService));
// Open the ServiceHostBase to create listeners and start
// listening for messages.
serviceHost.Open();
}
protected override void OnStop()
{
if (serviceHost != null)
{
serviceHost.Close();
serviceHost = null;
}
}
}
}
Program.cs:
namespace WindowsService
{
public class CalculatorWindowsService :ServiceBase
{
public ServiceHost serviceHost = null;
public CalculatorWindowsService()
{
// Name the Windows Service
ServiceName = "WCFWindowsServiceSample";
}
/// <summary>
/// The main entry point for the application.
/// </summary>
public static void Main()
{
//#if DEBUG
// Service1 myservice = new Service1();
// myservice.onDebug();
// System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
//#else
// CalculatorWindowsService calc = new CalculatorWindowsService();
ServiceBase.Run(new CalculatorWindowsService());
//#endif
}
}
}
【问题讨论】:
-
在本地网络浏览器中打开您的服务的 url 以查看任何错误消息;检查事件查看器以查看任何投诉;并检查您的防火墙入站和出站规则,以查看端口 8000 是否实际开放。
-
@AndyH:在 Internet Explorer 中,它给了我“无法显示页面”和“localhost:8000/ServiceModelSamples/service”
-
在事件查看器中显示服务已启动,如果我从服务管理器停止,则显示服务已成功停止
-
您可以使用 WCF 测试客户端连接到您的服务吗?您使用客户端的哪个 URL 连接到服务?如果您在浏览器中输入
http://servername:8000/ServiceModelSamples/service?wsdl- 您会看到服务的 WSDL 吗? -
@user726720 您会发现无法访问 WSDL(正如 marc 建议的那样)。你从这个事实中得出什么结论?