【发布时间】:2012-07-13 08:54:52
【问题描述】:
我已设法将复杂的 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;
}
}
}
【问题讨论】:
-
您是否尝试过为元数据交换添加“mex”端点?
-
您能否在实例化服务的位置也显示您的代码?
-
@Nightwish91 :我不确定我为什么需要墨西哥?在这种情况下,客户端不使用代理,而是使用 channelFactory。在没有 mex 的情况下,它在 IIS7 中确实可以正常工作?
-
@Monkieboy : 我已经更新了我的帖子。
-
好的,现在就按照说明运行。
标签: c# .net wcf windows-services self-hosting