我已经设法将我复杂的 WCF 服务移动到 Windows 服务中。绑定看起来像这样:
<service behaviorConfiguration="MyAppClientService.CustomValidator_Behavior" name="MyApp.ServiceImplementation.MyAppClientService">
<endpoint binding="netTcpBinding" bindingConfiguration="netTcpRegular" address="Regular" bindingNamespace="http://MyApp.ServiceContracts/2007/11" contract="MyApp.ServiceContracts.IMyAppClientService"/>
<endpoint binding="netTcpBinding" bindingConfiguration="netTcpWindowMessageSecurity" address="Windows" bindingNamespace="http://MyApp.ServiceContracts/2007/11" contract="MyApp.ServiceContracts.IMyAppClientService"/>
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8001/MyAppService/Client"/>
<add baseAddress="http://localhost:8002/MyAppService/Client"/>
</baseAddresses>
</host>
</service>
当服务启动时,我浏览:http://localhost:8002/MyAppService/Client
这工作正常,我还可以看到 WSDL。
但是当我尝试使用我的 Winform 客户端连接到服务时它找不到服务,这就是地址在客户端中的样子:
<client>
<endpoint address="net.tcp://localhost:8001/MyAppService/Client/MyAppClientService.svc/Regular" behaviorConfiguration="BasicBehavior" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IMyAppClientServiceRegular" contract="MyApp.ServiceContracts.IMyAppClientService" name="MyTest_RegularLogin"/>
</client>
浏览时http://localhost:8001/MyAppService/Client
我会得到一个丢失的页面,我想这是正确的,因为它托管在 tcp 而不是 http?
当服务托管在 IIS7(WAS) 中时,它工作得很好,但后来我在客户端使用了一个看起来像这样的端点:
<endpoint address="net.tcp://localhost/MyAppDev/MyAppClientService.svc/Regular" behaviorConfiguration="BasicBehavior" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IMyAppClientServiceRegular" contract="MyApp.ServiceContracts.IMyAppClientService" name="MyApp_RegularLogin"/>
注意:定期统计这是客户端提供用户名和密码的常规登录(无 Windows 登录)
编辑 :
我关注了这篇文章:http: //msdn.microsoft.com/en-us/library/ms733069.aspx
这就是 Windows 服务类的样子
public class MyAppWindowsService : ServiceBase
{
public ServiceHost _serviceHost = null;
public MyAppWindowsService()
{
// Name the Windows Service
ServiceName = "MyAppWindowsService";
}
public static void Main()
{
ServiceBase.Run(new MyAppWindowsService());
}
// Start the Windows service.
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(MyApp.ServiceImplementation.MyAppClientService));
// Open the ServiceHostBase to create listeners and start
// listening for messages.
_serviceHost.Open();
}
protected override void OnStop()
{
if (_serviceHost != null)
{
_serviceHost.Close();
_serviceHost = null;
}
}
}