0

I am using this example to create a Self hosted WCF application (.NET 4.5): https://msdn.microsoft.com/en-us/library/ms731758%28v=vs.110%29.aspx

I have successfully used it to create a console application.

But I am now trying to use the same code, in a brand new WPF application.

The only thing I have changed, is to remove:

host.Close();

So the app runs with the connection open as it should.

But when I test the WPF app using the WCF test client, it returns the error:

Error: Cannot obtain Metadata from http://localhost:8080/hello If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address. For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange Error URI: http://localhost:8080/hello Metadata contains a reference that cannot be resolved: 'http://localhost:8080/hello'. There was no endpoint listening at http://localhost:8080/hello that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details. Unable to connect to the remote server No connection could be made because the target machine actively refused it 127.0.0.1:8080HTTP GET Error URI: http://localhost:8080/hello There was an error downloading 'http://localhost:8080/hello'. Unable to connect to the remote server No connection could be made because the target machine actively refused it 127.0.0.1:8080

If I set a breakpoint on:

host.Open(); 

it is run, so should be running the service ok?

What would be the difference in my WPF app, vs the console application to cause this. Visual studio is run under administrator in both cases.

The main code:

 using (ServiceHost host = new ServiceHost(typeof(HelloWorldService), baseAddress))
        {
            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
            host.Description.Behaviors.Add(smb);

            host.Open();

            //Console.WriteLine("The service is ready at {0}", baseAddress);
            //Console.WriteLine("Press <Enter> to stop the service.");
            //Console.ReadLine();

            // Close the ServiceHost.
            //host.Close();
        }
5
  • possible duplicate of Error: Cannot obtain Metadata from WCF service Commented Feb 1, 2015 at 19:35
  • I disagree. The question is more asking why difference between Console application and a WPF application Commented Feb 1, 2015 at 19:50
  • 1
    When you say the only thing you've done is to remove the host.Close() call, I'm assuming you're referring to step 8 from your MSDN link. But, given that you're now hosting the service in a WPF application, I also assume you've removed the Console.ReadLine() call from the example, which is what prevents that code from exiting the using block and disposing the instance of ServiceHost. Can you include your code for starting the ServiceHost from your WPF app instead of linking out to MSDN? Commented Feb 1, 2015 at 20:40
  • Edited, is it because its existing the using statement?? If so, put that in an answer and I will mark it as answered. Many thanks! Commented Feb 1, 2015 at 21:10
  • Ahh, I removed the using statement and it worked. How silly of me. Im fairly new to using statements. Commented Feb 1, 2015 at 21:11

1 Answer 1

1

The code from the MSDN example contains a call to Console.ReadLine(), which prevents the application from exiting the using block where the ServiceHost instance is instantiated and opened.

When you moved the code into your WPF app, by keeping the using block, but not blocking the execution of your code from exiting the block (which you wouldn't want to do, since it is a GUI) you're effectively creating a service host which is then immediately disposed.

You will need to bootstrap your ServiceHost outside of a using block, and dispose of it manually when appropriate for your application.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.