none
WCF (Add Service Reference) RRS feed

  • Question

  • I am reading book of Andrew Troelsen "PRO C# 5.0".

    There is a sample in this book according to WCF services.

    I have an class library called MagicEightBallServiceLib:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.ServiceModel;
    
    namespace MagicEightBallServiceLib
    {
        public class MagicEightBallService : IEightBall
        {
            public MagicEightBallService()
            {
                Console.WriteLine("The 8-ball awaits your question...");
            }
    
            public string ObtainAnswerToQuestion(string userQuestion)
            {
                string[] answers = {"Future Uncertain", "Yes", "No", "Hazy", "Ask again later", "Definitely"};
                Random r = new Random();
                return answers[r.Next(answers.Length)];
            }
        }
    
        [ServiceContract(Namespace = "http://MyCompany.com")]
        public interface IEightBall
        {
            [OperationContract]
            string ObtainAnswerToQuestion(string userQuestion);
        }
    }
    

    Then i created Console Application project called MagicEightBallServiceHost.

    In this application i have app.config file with such code:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <system.serviceModel>
        <services>
          <service name="MagicEightBallServiceLib.MagicEightBallService" behaviorConfiguration="EightBallServiceMEXBehavior">
            <endpoint address="" binding="basicHttpBinding" contract="MagicEightBallServiceLib.IEightBall">
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange">
            </endpoint>
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:8080/MagicEightBallService" />
              </baseAddresses>
            </host>
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior name="EightBallServiceMEXBehavior">
              <serviceMetadata httpGetEnabled="true" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>
    </configuration>

    and my static void Main:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.ServiceModel;
    using MagicEightBallServiceLib;
    
    namespace MagicEightBallServiceHost
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("********* Console based WCF Host ***********");
                using (ServiceHost serviceHost = new ServiceHost(typeof(MagicEightBallService)))
                {
                    serviceHost.Open();
                    DisplayHostInfo(serviceHost);
                    Console.WriteLine("The service is ready.");
                    Console.WriteLine("Please, enter key to terminate service.");
                    Console.ReadLine();
                }
            }
    
            static void DisplayHostInfo(ServiceHost host)
            {
                Console.WriteLine();
                Console.WriteLine("HOST INFO!");
                foreach (var obj in host.Description.Endpoints)
                {
                    Console.WriteLine("Adress: {0}", obj.Address);
                    Console.WriteLine("Binding: {0}", obj.Binding.Name);
                    Console.WriteLine("Contract: {0}", obj.Contract.Name);
                    Console.WriteLine();
                }
                Console.WriteLine("*****************");
            }
        }
    }

    I want to add Service Reference:

    When i write the url "http://localhost:8080/MagicEightBallService" i have got an error "an error occured while attempting to find services at http://localhost:8080/MagicEightBallService"
    When I press 'Discover' i haven't got any services too :(

    Can you help me and say what I doing wrong? (Visual Studio running as Administrator)

    Sorry for my English.

    Sunday, June 30, 2013 12:55 PM

Answers