C#WCF HTTP服务控制台应用程序无法在辅助系统

时间:2017-10-26 16:07:12

标签: c# wcf

我有一个运行独立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错误,即使我在两个系统上都有相同的配置?

2 个答案:

答案 0 :(得分:1)

您可能在配置中有一些不适合新环境的内容。例如,您确定该端口尚未使用吗?

无论哪种方式,CommunicationObjectFaultedException只是另一个异常的症状,可以让您更好地了解失败的原因。第一个例外是触发using阻止尝试处置ServiceHost。由于主机从未启动过,因此处理会抛出一个新的异常来抑制旧的异常。

您可以尝试删除using阻止版,并使用传统的host.Open();host.Close();来电。这应该揭示根本原因。

答案 1 :(得分:0)

这可能对您有帮助,因为对我来说真正可疑的关键字是&#34;远程&#34;:https://support.microsoft.com/en-us/help/17463/windows-7-connect-to-another-computer-remote-desktop-connection

  

单击“开始”按钮“开始”按钮图标打开系统,   右键单击“计算机”,然后单击“属性”。单击远程   设置。需要管理员权限如果您提示输入   管理员密码或确认,输入密码或提供   确认。在“远程桌面”下,选择三个选项之一。   单击选择用户。

     

如果您是计算机管理员,则使用当前用户帐户   将自动添加到远程用户列表中,您可以   跳过接下来的两个步骤。在“远程桌面用户”对话框中,单击“是”   加。在“选择用户或组”对话框中,执行以下操作:收件人   指定搜索位置,单击“位置”,然后选择   您要搜索的位置。在输入要选择的对象名称中,键入   要添加的用户的名称,然后单击“确定”。名字   将显示在远程桌面用户的用户列表中   对话框。单击“确定”,然后再次单击“确定”。

您可以访问&#34;所有内容&#34;对于您正在开发的用户,您似乎也可以访问旧系统,但是在允许方面,新的远程服务器可能会很棘手。

相关问题