Answered by:
Self host service is not working

Question
-
Hello,
I followed the example in MSDN http://msdn.microsoft.com/en-us/library/ms730935.aspx
When I verify if the service is working or not, the result is that it works from visual studio(service.exe) but failed in browsing to the service's debug page at http://localhost:8000/ServiceModelSamples/Service
Could you please advise me?
Thanks.
Imports System Imports System.ServiceModel Imports System.ServiceModel.Description Namespace Microsoft.ServiceModel.Samples Module Service ' Define a service contract. <ServiceContract([Namespace]:="http://Microsoft.ServiceModel.Samples")> _ Public Interface ICalculator <OperationContract()> _ Function Add(ByVal n1 As Double, ByVal n2 As Double) As Double <OperationContract()> _ Function Subtract(ByVal n1 As Double, ByVal n2 As Double) As Double <OperationContract()> _ Function Multiply(ByVal n1 As Double, ByVal n2 As Double) As Double <OperationContract()> _ Function Divide(ByVal n1 As Double, ByVal n2 As Double) As Double End Interface ' Step 1: Create service class that implements the service contract. Public Class CalculatorService Implements ICalculator ' Step 2: Implement functionality for the service operations. Public Function Add(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Add Dim result As Double = n1 + n2 ' Code added to write output to the console window. Console.WriteLine("Received Add({0},{1})", n1, n2) Console.WriteLine("Return: {0}", result) Return result End Function Public Function Subtract(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Subtract Dim result As Double = n1 - n2 Console.WriteLine("Received Subtract({0},{1})", n1, n2) Console.WriteLine("Return: {0}", result) Return result End Function Public Function Multiply(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Multiply Dim result As Double = n1 * n2 Console.WriteLine("Received Multiply({0},{1})", n1, n2) Console.WriteLine("Return: {0}", result) Return result End Function Public Function Divide(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Divide Dim result As Double = n1 / n2 Console.WriteLine("Received Divide({0},{1})", n1, n2) Console.WriteLine("Return: {0}", result) Return result End Function End Class Class Program Shared Sub Main() Dim baseAddress As New Uri("http://localhost:8080/ServiceModelSamples/Service") Dim selfHost As New ServiceHost(GetType(CalculatorService), baseAddress) Try selfHost.AddServiceEndpoint( _ GetType(ICalculator), _ New WSHttpBinding(), _ "CalculatorService") Dim smb As New ServiceMetadataBehavior() smb.HttpGetEnabled = True selfHost.Description.Behaviors.Add(smb) selfHost.Open() Console.WriteLine("The service is ready.") Console.WriteLine("Press <ENTER> to terminate service.") Console.WriteLine() Console.ReadLine() selfHost.Close() Catch ce As CommunicationException Console.WriteLine("An exception occurred: {0}", ce.Message) selfHost.Abort() End Try End Sub End Class End Module End Namespace
Wednesday, July 22, 2009 4:07 PM
Answers
-
Yes, when you close the program the service will be stopped. The service will only be running while the program is running as well.
- Proposed as answer by Riquel_Dong Monday, July 27, 2009 3:03 AM
- Marked as answer by Riquel_Dong Wednesday, July 29, 2009 2:17 AM
Thursday, July 23, 2009 3:28 PM
All replies
-
Your service base address is at http://localhost:8080/ServiceModelSamples/Service, not at port 8000. I just tried your code and I was able to browse to http://localhost:8080/ServiceModelSamples/Service just fine.Wednesday, July 22, 2009 4:40 PM
-
Still failed.
Internet Explorer cannot display the webpageWednesday, July 22, 2009 5:39 PM -
Replace your Main sub with the code below. That will show exactly where the server is listening to (the second one should be the address you can try on Internet Explorer). It will also attempt to send a request to the help page (the HttpWebRequest code) to see if the program can access it. Can you try it and let me know the result? Notice that you'll need to add Import statements for System.IO, System.Net and System.ServiceModel.Dispatcher as well.
Dim baseAddress As New Uri("http://localhost:8080/ServiceModelSamples/Service") Dim selfHost As New ServiceHost(GetType(CalculatorService), baseAddress) Try selfHost.AddServiceEndpoint( _ GetType(ICalculator), _ New WSHttpBinding(), _ "CalculatorService") Dim smb As New ServiceMetadataBehavior() smb.HttpGetEnabled = True selfHost.Description.Behaviors.Add(smb) selfHost.Open() For Each channelDispatcher As ChannelDispatcher In selfHost.ChannelDispatchers For Each endpointDispatcher As EndpointDispatcher In channelDispatcher.Endpoints Console.WriteLine("Listening at {0}", endpointDispatcher.EndpointAddress.Uri) Next Next Console.WriteLine("The service is ready.") Console.WriteLine("Now sending a request to the help page") Dim req As HttpWebRequest = HttpWebRequest.Create(baseAddress) req.Method = "GET" Dim resp As HttpWebResponse = req.GetResponse Console.WriteLine("Response:") Console.WriteLine("HTTP/{0} {1} {2}", resp.ProtocolVersion, CInt(resp.StatusCode), resp.StatusDescription) If resp.ContentLength > 0 Then Console.WriteLine(New StreamReader(resp.GetResponseStream).ReadToEnd) End If Console.WriteLine() Console.WriteLine("Press <ENTER> to terminate service.") Console.WriteLine() Console.ReadLine() selfHost.Close() Catch ce As CommunicationException Console.WriteLine("An exception occurred: {0}", ce.Message) selfHost.Abort() End Try
- Proposed as answer by Carlos Figueira Thursday, July 23, 2009 3:28 PM
Wednesday, July 22, 2009 5:55 PM -
Still not working.
I am using visual studio 2008 professional trial.
OS/Win XP Pro, IE 8.0.Wednesday, July 22, 2009 6:12 PM -
What is the output of the program, with all the new WriteLine's?Wednesday, July 22, 2009 6:29 PM
-
It only works in Visual Studio. I copied the result from dos window.
Listening at http://localhost:8080/ServiceModelSamples/Service/CalculatorService Listening at http://localhost:8080/ServiceModelSamples/Service The service is ready. Now sending a request to the help page Response: HTTP/1.1 200 OK <HTML><HEAD><link rel="alternate" type="text/xml" href="http://localhost:8080/Se rviceModelSamples/Service?disco"/><STYLE type="text/css">#content{ FONT-SIZE: 0. 7em; PADDING-BOTTOM: 2em; MARGIN-LEFT: 30px}BODY{MARGIN-TOP: 0px; MARGIN-LEFT: 0 px; COLOR: #000000; FONT-FAMILY: Verdana; BACKGROUND-COLOR: white}P{MARGIN-TOP: 0px; MARGIN-BOTTOM: 12px; COLOR: #000000; FONT-FAMILY: Verdana}PRE{BORDER-RIGHT: #f0f0e0 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #f0f0e0 1px solid; MARGIN-TO P: -5px; PADDING-LEFT: 5px; FONT-SIZE: 1.2em; PADDING-BOTTOM: 5px; BORDER-LEFT: #f0f0e0 1px solid; PADDING-TOP: 5px; BORDER-BOTTOM: #f0f0e0 1px solid; FONT-FAMI LY: Courier New; BACKGROUND-COLOR: #e5e5cc}.heading1{MARGIN-TOP: 0px; PADDING-LE FT: 15px; FONT-WEIGHT: normal; FONT-SIZE: 26px; MARGIN-BOTTOM: 0px; PADDING-BOTT OM: 3px; MARGIN-LEFT: -30px; WIDTH: 100%; COLOR: #ffffff; PADDING-TOP: 10px; FON T-FAMILY: Tahoma; BACKGROUND-COLOR: #003366}.intro{MARGIN-LEFT: -15px}</STYLE><T ITLE>CalculatorService Service</TITLE></HEAD><BODY><DIV id="content"><P class="h eading1">CalculatorService Service</P><BR/><P class="intro">You have created a s ervice.<P class='intro'>To test this service, you will need to create a client a nd use it to call the service. You can do this using the svcutil.exe tool from t he command line with the following syntax:</P> <BR/><PRE>svcutil.exe <A HREF="ht tp://localhost:8080/ServiceModelSamples/Service?wsdl">http://localhost:8080/Serv iceModelSamples/Service?wsdl</A></PRE></P><P class="intro"/>This will generate a configuration file and a code file that contains the client class. Add the two files to your client application and use the generated client class to call the Service. For example:<BR/><P class='intro'><B>C#</B></P><PRE><font color="blue"> class </font><font color="teal">Test </font>{ <font color="blue"> static void </font>Main() { <font color="teal">CalculatorClient</font> client = <font color="blue">n ew </font><font color="teal">CalculatorClient</font>(); <font color="green"> // Use the 'client' variable to call operations on t he service. </font><font color="green"> // Always close the client. </font> client.Close(); } } </PRE><BR/><P class='intro'><B>Visual Basic</B></P><PRE><font color="blue">Class </font><font color="teal">Test </font><font color="blue"> Shared Sub </font>Main() <font color="blue"> Dim </font>client As <font color="teal">CalculatorCli ent</font> = <font color="blue">New </font><font color="teal">CalculatorClient</ font>() <font color="green"> ' Use the 'client' variable to call operations on th e service. </font><font color="green"> ' Always close the client. </font> client.Close() <font color="blue"> End Sub </font><font color="blue">End Class</font></PRE></DIV></BODY></HTML> Press <ENTER> to terminate service.
Wednesday, July 22, 2009 6:31 PM -
Ok, so the server is launched, and it is serving the help page correctly, so the issue must be on Internet Explorer. You're launching IE from the same computer as the process is running, right? Also, are there any proxy settings on IE (Tools -> Internet Options -> Connections -> LAN Settings) which may be directing the request somewhere else?Wednesday, July 22, 2009 6:48 PM
-
I am using wireless. Is the reason to cause failure?Wednesday, July 22, 2009 6:57 PM
-
It shouldn't - localhost (i.e., your own machine) should work even if you don't have any internet connection.Wednesday, July 22, 2009 7:07 PM
-
There is no any proxy settings on IE.Wednesday, July 22, 2009 7:09 PM
-
One more attempt: can you replace the lines:
If resp.ContentLength > 0 Then Console.WriteLine(New StreamReader(resp.GetResponseStream).ReadToEnd) End If Console.WriteLine() Console.WriteLine("Press <ENTER> to terminate service.") Console.WriteLine() Console.ReadLine()
with
If resp.ContentLength > 0 Then Console.WriteLine(New StreamReader(resp.GetResponseStream).ReadToEnd) End If Console.WriteLine() Console.WriteLine("Trying to start IE from the program...") Dim process As System.Diagnostics.Process = System.Diagnostics.Process.Start("C:\\Program Files\\Internet Explorer\\iexplore.exe", baseAddress.ToString()) Console.WriteLine("Press <ENTER> to terminate service.") Console.WriteLine() Console.ReadLine()
this will try to launch IE from the program itself (change the location of iexplore.exe, if it's different than what is in the example above), to see if this will make a difference.- Edited by Carlos Figueira Thursday, July 23, 2009 12:07 AM .
Thursday, July 23, 2009 12:06 AM -
Perhaps it is my fault. I stopped the program then launch the IE.
I mean I closed the dos window then paste the ip address to launch it.Thursday, July 23, 2009 1:34 PM -
Yes, when you close the program the service will be stopped. The service will only be running while the program is running as well.
- Proposed as answer by Riquel_Dong Monday, July 27, 2009 3:03 AM
- Marked as answer by Riquel_Dong Wednesday, July 29, 2009 2:17 AM
Thursday, July 23, 2009 3:28 PM