简体   繁体   中英

C# WCF HTTP Service Console Application Not Working on Secondary System

I have a C# console application that runs a standalone WCF service. The application works fine on my development PC and on a production Windows 7 PC where it's been running for some time. I'm trying to get it to work on a secondary remote system, but keep receiving a System.ServiceModel.CommunicationObjectFaultedException when running the program on the second system with the following error: The communication object, System.ServiceModel.ServiceHost, cannot be used for communication because it is in the Faulted state.

I am using the exact same executable across the 3 systems. All systems are Windows 7 with .NET Framework 4, and I run the executable as an administrator (I saw other threads that suggested this). I've changed the IP address of the client endpoint address in app.config for the respective system.

Here's my Program.cs code:

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();
    }
}

And my app.config file that was configured using Microsoft Service Configuration Editor:

<?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>

Why am I receiving the System.ServiceModel.CommunicationObjectFaultedException error on the secondary system even though I have the same configuration on both systems?

You might have something in the config that isn't suitable for the new environment. For example, are you sure the port isn't already in use?

Either way, the CommunicationObjectFaultedException is a just a symptom of another exception that will give you a better picture of the reason for the failure. That first exception is triggering your using block to attempt to dispose the ServiceHost . Since the host never started, disposal throws a new exception suppressing the old one.

You could try getting rid of your using block and going with traditional host.Open(); and host.Close(); calls. This should reveal the underlying cause.

This may help you out, cause the keyword that is really suspicious to me is "remote": https://support.microsoft.com/en-us/help/17463/windows-7-connect-to-another-computer-remote-desktop-connection

Open System by clicking the Start button Start button icon, right-clicking Computer, and then clicking Properties. Click Remote settings. Administrator permission required If you're prompted for an administrator password or confirmation, type the password or provide confirmation. Under Remote Desktop, select one of the three options. Click Select Users.

If you're an administrator on the computer, your current user account will automatically be added to the list of remote users and you can skip the next two steps. In the Remote Desktop Users dialog box, click Add. In the Select Users or Groups dialog box, do the following: To specify the search location, click Locations, and then select the location you want to search. In Enter the object names to select, type the name of the user that you want to add, and then click OK. The name will be displayed in the list of users in the Remote Desktop Users dialog box. Click OK, and then click OK again.

You can acces "everything" with the user you are developping, you seem to have acces on the old system too, but a fresh remote server can be tricky in terms of permission.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM