Results 1 to 8 of 8

Thread: WCF example: Self host service is not working

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Posts
    470

    WCF example: Self host service is not working

    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:8080/ServiceModelSamples/Service

    I am using IE 8 and Windows XP Pro.
    Could you please advise me?
    Thanks.
    Code:
    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

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: WCF example: Self host service is not working

    Are you admin on your machine?

    You said it works from within Visual Studio - what do you mean by that. Are you saying that you can browse to the URL while it's self hosted and running from Visual Studio? Or are you saying that the URL in the browser just never works?

  3. #3
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: WCF example: Self host service is not working

    Yeah you need to explain a little better... but from your code I cant see any reason why anything wouldnt be working, all looks as it should.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Posts
    470

    Re: WCF example: Self host service is not working

    Quote Originally Posted by mendhak View Post
    Are you admin on your machine?

    You said it works from within Visual Studio - what do you mean by that. Are you saying that you can browse to the URL while it's self hosted and running from Visual Studio? Or are you saying that the URL in the browser just never works?
    I mean if I run the code by pressing F5 then a dos window pops up.
    However if I paste the ip address to the browser then an error occuried.
    Code:
    This program cannot display the webpage
    And I am the computer adminstrator.

  5. #5
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: WCF example: Self host service is not working

    You do have your program running when you try and access it via the browser dont you?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Posts
    470

    Re: WCF example: Self host service is not working

    Well, I stopped running the program then access it via the browser.

  7. #7
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: WCF example: Self host service is not working

    Thats why it isnt working then. The web browser is just a way of connecting to your program, if your program isnt running then there is nothing listening on the port you specified (8080 in your case) so the web browser cant connect to anything. Start your program, then try the web browser again while your program is still open and you should see what you expected to see
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  8. #8
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: WCF example: Self host service is not working

    Lol... yes, the service is self-hosted, and is self-hosted as long as the app is running, because it is the host.

    Later, you'll probably end up doing these services in IIS, so you don't have to worry too much about the host, but this is to give you an idea of what's going on behind the scenes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width