1
votes

I have this interface:

  [ServiceContract]
    public interface ILocationService
    {
        [OperationContract]
        bool RegisterForNotification(string name, string uri);

        [OperationContract]
        bool UnRegisterForNotification(string name);
    }

and this service:

[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
    public class LocationBasedService : ILocationService
    {
        #region Registrations
        public bool RegisterForNotification(string name, string uri)
        {
            return true;
        }

        public bool UnRegisterForNotification(string name)
        {
            return true;
        }     
        #endregion
    }

and the following configuration:

<configuration>
  <system.serviceModel>
    <services>
      <service name="PushNotifications.Server.Service.LocationBasedService" >
        <endpoint address="http://localhost:8000/LocationBasedService"
                 binding="basicHttpBinding"                 
                 contract="Services.Interface.ILocationService"/>
      </service>      
    </services>
  </system.serviceModel>

Self hosted in a WPF application using ServiceHost. The code for this goes like this:

private void startSrv_Click(object sender, RoutedEventArgs e)
        {
            try 
            {
                host = new ServiceHost(typeof(LocationBasedService));
                host.Open();
                AddDiagnosticMessage("service successfully initialized");
                AddDiagnosticMessage(string.Format("{0} is up and running with these end points", host.Description.ServiceType));
                foreach (var se in host.Description.Endpoints)
                    AddDiagnosticMessage(se.Address.ToString());
            }
            catch (TimeoutException ex)
            {
                AddDiagnosticMessage(string.Format("The service operation timeod out. {0}", ex));
            }
            catch (CommunicationException ex)
            {
                AddDiagnosticMessage(string.Format("Could not start host service. {0}", ex));
            }
            catch (Exception ex)
            {
                AddDiagnosticMessage(ex.Message);
            }                  
        }

The service starts without exceptions. However, when I submit the url http://localhost:8000/LocationBasedService to my browser, I get HTTP 400 Bad Request. If I try to create a WCF client using Visual Studio's Add Service Reference, I get the following error:

'http://localhost:8000/LocationBasedService'. Content Type application/soap+xml; charset=utf-8 was not supported by service http://localhost:8000/LocationBasedService. The client and service bindings may be mismatched. The remote server returned an error: (415) Cannot process the message because the content type 'application/soap+xml; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'.. If the service is defined in the current solution, try building the solution and adding the service reference again.

IF I try to invoke the client, using the following code, I get a timeout exception.

 private void Button_Click(object sender, RoutedEventArgs e)
        {
            statusMessages.Add(GetFormattedMessage("initiailing client proxy"));
            var ep = new EndpointAddress("http://localhost:8000/LocationBasedService");
                     var proxy = ChannelFactory<ILocationService>.CreateChannel(new BasicHttpBinding(), ep);          
            var register = proxy.RegisterForNotification("name1", @"http://google.com");
            if (register)
            {  Console.Writeline(register.ToString()); }
        }

Could someone please give some insights onto what I missed. This was supposed to be an easy exercise :!

TIA.

1
I made some progress with viewing meta data by adding ServiceMetaData behavior but still cannot get service to respond to simple client request without timeouts. This is all with default BasicHttpBindings. - Klaus Nji

1 Answers

2
votes

The Bad HTTP request was solved by adding this code:

 host = new ServiceHost(typeof(LocationBasedService), new Uri("http://localhost:8000/LocationBasedService"));

  // Enable metadata publishing.
   ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
   smb.HttpGetEnabled = true;
   smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
   host.Description.Behaviors.Add(smb);

    host.Open();   

As for service timeouts, this was being caused by my personal firewall. I had to add Visual Studio to the list of allowed applications.

Back in business.