Click here to Skip to main content
15,032,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am using this code for starting the wcf service .the code is runing fine but it is not starting the wcf service . there is no error or exception during the execution of the program

ServiceHost host;
      private void StartBtn_Click(object sender, EventArgs e)

          var baseaddress = new Uri[] { new Uri("") };
          using (host = new ServiceHost(typeof(WcfServiceLibrary1.Service1), new Uri[] { new Uri("") }))
              // Enable metadata publishing.
              ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
              smb.HttpGetEnabled = true;
              smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;

              // Open the ServiceHost to start listening for messages. Since
              // no endpoints are explicitly configured, the runtime will create
              // one endpoint per base address for each service contract implemented
              // by the service.
              host.AddServiceEndpoint(typeof(WcfServiceLibrary1.IService1), new WSHttpBinding(), "WcfServiceLibrary1.Service1");

              richTextBox1.AppendText("The service is ready at {0}"+ baseaddress.ToString());
              richTextBox1.AppendText("Press <stop service> to stop the service.");

              // Close the ServiceHost.
              // host.Close();
Updated 18-Sep-14 22:04pm
Herman<T>.Instance 19-Sep-14 4:22am
how have you set it up in IIS?
Adi5555 19-Sep-14 5:56am
i am using self hosting not IIS
johannesnestler 19-Sep-14 6:13am
hmm - code Looks ok - just checked with my selfhosting app - same (conceptual) code...
johannesnestler 19-Sep-14 6:14am
Did you try to call the Service with WCFTestClient or another application?
Adi5555 19-Sep-14 6:19am
the service is also working fine .... its even working in local area network...but i want to start is with the exe file not from visual studio execution
G3Coder 22-Sep-14 12:03pm
You are using the servicehost in a button click - won't it go out of scope?
Adi5555 23-Sep-14 0:44am
i dont care if it starts on the button click or on form load...jst wana start it by an exe file
G3Coder 23-Sep-14 5:10am
The host will be closed when the button click event is finished. You can test this by adding a Closing event to your service host instance. Try removing the using statement and let us know how it goes; though remember you are going to need to close it sometime.

1 solution

You can run a self-hosting WCF service as a console app, then just leave it to run in the background. To invoke it, build the console app into an exe and run it whenever you need it.

This is how I test my WCF services before I deploy them to IIS.

See my example WCF service that I have posted on Github which contains a self-hosted WCF service as a console app[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900