![](/img/trans.png)
[英]C# : .Net : Code working on Console application . Same code is failing when running on WCF service
[英]C# WCF HTTP Service Console Application Not Working on Secondary System
我有一个运行独立WCF服务的C#控制台应用程序。 该应用程序在我的开发PC和已经运行了一段时间的Windows 7生产PC上都可以正常运行。 我试图使其在辅助远程系统上工作,但是在第二个系统上运行程序时由于以下错误而继续收到System.ServiceModel.CommunicationObjectFaultedException
: The communication object, System.ServiceModel.ServiceHost, cannot be used for communication because it is in the Faulted state.
我正在3个系统中使用完全相同的可执行文件。 所有系统都是带有.NET Framework 4的Windows 7,我以管理员身份运行该可执行文件(我看到了其他提示此问题的线程)。 我已经在相应系统的app.config
更改了客户端端点地址的IP地址。
这是我的Program.cs
代码:
static void Main(string[] args)
{
initService();
}
static void initService()
{
using (ServiceHost host = new ServiceHost(typeof(MyService)))
{
host.Open(); // Throws System.ServiceModel.CommunicationObjectFaultedException
Console.ReadLine();
host.Close();
}
}
以及使用Microsoft服务配置编辑器配置的我的app.config
文件:
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<bindings/>
<client>
<endpoint address="http://000.000.000:9000" binding="basicHttpBinding"
bindingConfiguration="" contract="TestService.IService"
name="ServiceEndPoint" kind="" endpointConfiguration="" />
</client>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
<useRequestHeadersForMetadataAddress/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="TestService.MyService">
<endpoint address="http://localhost:9000" behaviorConfiguration="" binding="basicHttpBinding" bindingConfiguration=""
name="MyServiceEndPoint" contract="MyService.IService"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:9000"/>
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
即使在两个系统上都具有相同的配置,为什么在辅助系统上仍收到System.ServiceModel.CommunicationObjectFaultedException
错误?
您的配置中可能包含一些不适用于新环境的内容。 例如,您确定端口尚未使用吗?
无论哪种方式, CommunicationObjectFaultedException
只是另一个异常的征兆,它将使您更好地了解失败的原因。 第一个异常触发您的using
块尝试处置ServiceHost
。 由于主机从未启动,因此处置会抛出新的异常,从而抑制旧的异常。
您可以尝试摆脱using
块,并使用传统的host.Open();
和host.Close();
调用。 这应该揭示根本原因。
这可能会对您有所帮助,因为对我来说真的很可疑的关键字是“远程”: https : //support.microsoft.com/zh-cn/help/17463/windows-7-connect-to-another-computer-远程桌面连接
单击“开始”按钮“开始”按钮图标,右键单击“计算机”,然后单击“属性”,打开“系统”。 单击远程设置。 需要管理员权限如果系统提示您输入管理员密码或确认,请输入密码或提供确认。 在“远程桌面”下,选择三个选项之一。 单击选择用户。
如果您是计算机的管理员,那么您当前的用户帐户将自动添加到远程用户列表中,您可以跳过接下来的两个步骤。 在“远程桌面用户”对话框中,单击“添加”。 在“选择用户或组”对话框中,执行以下操作:要指定搜索位置,请单击“位置”,然后选择要搜索的位置。 在“输入要选择的对象名称”中,键入要添加的用户的名称,然后单击“确定”。 该名称将显示在“远程桌面用户”对话框的用户列表中。 单击确定,然后再次单击确定。
您可以与正在开发的用户一起访问“所有内容”,您似乎也对旧系统具有访问权限,但是就权限而言,新的远程服务器可能很棘手。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.