简体   繁体   中英

WPF - Self hosting WCF

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

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.

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