消费是在Windows服务从外部解决方案托管WCF服务 [英] Consuming a WCF Service that is hosted in a Windows Service from outside solution

查看:171
本文介绍了消费是在Windows服务从外部解决方案托管WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成立了一个WCF库托管在Windows服务使用以下演练:

I've set up a WCF Library hosted in a Windows Service using the following walk-through:

http://msdn.microsoft.com/en-us/library/ff649818.aspx

消费者的WinForms是在同一个解决方案,这是位于本地我的工作电脑公司的C系列商品:驱动器

The consumer winforms is in the same solution, which is located locally on my work PC's C: drive.

步行通过的作品即WinForms的按钮给我正确的答案。

The walk-through works i.e. the winforms button gives me the correct answer.

如果我创建了C-驱动器与一个Windows新的解决方案窗体项目在这我不能成功添加服务引用来运行这个服务,我得到以下信息:

If I create a new Solution on the C-Drive with a single Windows Forms project in it I cannot successfully add a service reference to this running service, i get the following message:

详细的消息称以下内容:

The detailed message says the following:

的URI前缀无法识别。元数据包含
的参考无法解析:'的net.tcp://本地主机:8526 /服务1。无法
连接到的net.tcp://本地主机:8526 / Service1的。连接尝试
历时00一个时间跨度:00:02.0020000。 TCP错误代码10061:没有
连接可以作出,因为目标机器积极拒绝
它127.0.0.1:8526。无连接可以作出,因为目标
机器积极地拒绝它127.0.0.1:8526如果该服务在当前的解决方案定义
,尝试构建解决方案,并再次添加
服务的参考。

The URI prefix is not recognized. Metadata contains a reference that cannot be resolved: 'net.tcp://localhost:8526/Service1'. Could not connect to net.tcp://localhost:8526/Service1. The connection attempt lasted for a time span of 00:00:02.0020000. TCP error code 10061: No connection could be made because the target machine actively refused it 127.0.0.1:8526. No connection could be made because the target machine actively refused it 127.0.0.1:8526 If the service is defined in the current solution, try building the solution and adding the service reference again.

为什么我可以在这个服务参考确定添加到项目相同的解决方案的服务中而不是从一个项目在不同的解决方案?

Why can I add this Service Reference ok to a project within the same Solution as the Service but not from a project in a different solution?

修改

我的同事在MSDN文章中发现了一个错误 - 我已经详细阐述了他发现的这里

My colleague found an error in the MSDN article - I have detailed his find HERE

推荐答案

借助一步一步的演练文章 在MSDN 不幸的是在这里结束它变得有趣,所以让我们继续在这里。因为有很多这可能导致错误的可能性,我描述了几个选项(= scencarios这可能会导致问题)之下,这应有助于排除故障:

The step by step walkthrough article at MSDN ends unfortunately where it gets interesting, so let's continue here. Because there are many possibilities which may cause the error, I've described several options (= scencarios which may cause the issue) below, which should help troubleshooting:

第1个选项:尝试指定

  net.tcp://localhost:8526/Service1/mex

当您添加服务引用新的客户 - 确保已安装的服务和运行的的你做到这一点。

when you add the service reference to your new client - ensure that the service is installed and running before you do that.

说明:后缀MEX代表元数据交换,并允许Visual Studio的下载细节的WCF合同。此后缀还用于步行通过的例子,它被自动添加(你会看到它的地址栏中,如果重新打开添加引用服务通过右键 。点击配置服务引用...)

Explanation: The suffix "mex" stands for "metadata exchange" and allows Visual Studio to download details of the WCF contract. This suffix is also used in the walk-through example, it was added automatically (you will see it in the Address field if re-open the added service reference by right-clicking on "Configure Service-Reference...").

第二个选项:什么当我测试了步行通过的是,它有时可以帮助在服务引用单击鼠标右键,在客户服务联系菜单中的更新服务引用选择我注意到了。

2nd option: What I noticed when I tested the walk-through is that it helps sometimes to right-click on the service reference and select in the contect menu "Update Service-Reference".

过了一会在系统托盘您可以看到气球消息您的服务(S)已经被托管。 ,之后您可以在同一解决方案中启动客户端。在这种情况下,该服务已被暂时创建,但不会永久部署 - 这意味着,如果停止调试,它被移除。其结果是,你不能从远程PC使用这项服务,它是在Visual Studio中的解决方案中依稀可见。 Visual Studio的内部调用工具

After a while in the systray you can see the balloon message "Your service(s) have been hosted.", after which you can start the client within the same solution. In this case, the service has been temporarily created but is not deployed permanently - which means, if you stop debugging, it is removed. As a result, you can't use this service from a remote PC, it is just visible within the solution in Visual Studio. Visual Studio internally invokes the tool

WcfSvcHost.Exe /Service:<Service1Binary> /Configuration:<Service1Config> 



与正确的参数支持它正常注册服务(你可以找到在Visual Studio的这个工具 Common7\IDE 子目录,也有 WcfTestClient.Exe 可用 - 一个工具,它作为客户端,非常实用调试WCF)。

supporting it with the right parameters to register the service properly (you can find this tool in Visual Studio's Common7\IDE subdirectory, and there is also WcfTestClient.Exe available - a tool which acts as a client, very useful to debug WCF).

举例来说,如果你已经停止调试,以及从Windows资源管理器启动client.exe的Visual Studio之外,再没有找到服务,而你恰好有你在你的问题中描述的错误信息

For instance, if you have stopped debugging, and launch the client.exe from Windows Explorer outside of Visual Studio, then it does not find the service and you're getting exactly the error message you have described in your question.

有关于此事的微软两个有趣的链接:
Problem与元数据交换并的Publishing元数据

There are two interesting links regarding this matter at Microsoft: Problem with Metadata Exchange and Publishing Metadata

注意此从部署它作为在第三个选项描述的不同。

第三个选项您是否使用 InstallUtil 部署服务?在这种情况下,它可能发生,你不小心删除了 [...] /斌/调试子目录和服务无法启动​​,因为 .EXE 文件丢失。

3rd option: Have you used InstallUtil to deploy the service? In this case it can happen that you have accidently removed the [...]/bin/Debug subdirectory and the service fails to start, because the .EXE file is missing.

注意:这可以,如果你使用的的ServiceInstaller 避免项目,其中复制服务之前二进制文件被注册。或者 - 如果你想使用 InstallUtil 作为简单 - 你可以在服务二进制文件复制到目标目录(包括.config文件和.dll文件)在注册之前。

Note: This can be avoided if you're using a ServiceInstaller project, which copies the binaries before the service is registered. Or - if you want to use InstallUtil for simplicity - you can copy the service binaries to a target directory (including the .config files and .dlls) before you register it.

第四选项:如果您运行的服务的远程电脑后,您需要指定的适当的主机名或IP主机,而不是的本地主机的地址,然后你需要确保< STRONG>的个人防火墙 (Windows防火墙或第三方) 不阻止端口8526 (端口号这是用来EM中的例子)。 。指定一个例外,允许该端口的传入和传出的流量

4th option: If you run the service on a remote computer, you need to specify the proper host name or IP address of the host instead of localhost, and you need to ensure that the personal firewall (windows firewall or 3rd party) doesn't block the port 8526 (the port number which was used in the example). Specify an exception to allow this port for incoming and outgoing traffic.

5次和最后一个选项(更新): 命名冲突 - 服务1是服务,而且在WCF库中的类名。要么完全限定你从 WCF库在服务上,即 WcfServiceLibrary1.Service1 使用类名或重命名的类。 Whytheq发现它自己与同事和贴吧的这里

5th and final option (UPDATE): Naming conflict - Service1 is the service but also the class name in the Wcf library. Either fully qualify the class name you're using from the WCF library in the service, i.e. WcfServiceLibrary1.Service1 or rename the class. Whytheq has found it himself with a colleague and as posted it here.

更多阅读: 看看这篇文章,我最近发现: WCF:几个小技巧。它解释了很好的故障排除WCF。我想唯一的变化在控制台取得托管的例子是为替换使用语句

More reading: Check out this article, which I've found recently: "WCF: a few tips". It explains very well troubleshooting WCF. The only change I would made to the console hosting example is to replace the using statement by a

ServiceHost host = new ServiceHost(typeof(Service));
try
{
    host.Open();

    Console.WriteLine("WCF Service is ready for requests." +  
    "Press any key to close the service.");
    Console.WriteLine();
    Console.Read();

    Console.WriteLine("Closing service...");
}
finally
{
    if (host!=null) {
            host.Close();
            host=null;
    }
}

如果您想了解更多关于原因,看看这篇文章:代理启闭

If you want to know more about the reason why, check out this article: "Proxy open and close".

这篇关于消费是在Windows服务从外部解决方案托管WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆