WCF:HTTP自我托管服务

问题描述:

400错误的请求我有这样的接口:WCF:HTTP自我托管服务

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

     [OperationContract] 
     bool UnRegisterForNotification(string name); 
    } 

这个服务:

[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 
    } 

和以下配置:

<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> 

自托管在使用ServiceHost的WPF应用程序中。代码如下所示:

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); 
      }     
     } 

服务开始无例外。但是,当我将URL http://localhost:8000/LocationBasedService提交给我的浏览器时,我收到HTTP 400 Bad Request。如果我尝试使用Visual Studio的添加服务参考创建WCF客户端,我会收到以下错误:

'http:// localhost:8000/LocationBasedService'。 内容类型application/soap + xml; charset = utf-8不支持服务http://localhost:8000/LocationBasedService。客户端和服务绑定可能不匹配。 远程服务器返回错误:(415)由于内容类型'application/soap + xml; charset = utf-8'不是预期的类型'text/xml; charset = utf-8'.. 如果服务在当前解决方案中定义,请尝试构建解决方案并再次添加服务引用。

如果我尝试调用客户端,使用下面的代码,我得到一个超时异常。

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()); } 
     } 

有人请给我一些见解我错过了。这应该是一个简单的练习:!

TIA。

+0

我通过添加ServiceMetaData行为在查看元数据方面取得了一些进展,但仍无法获取服务以响应简单的客户端请求而无需超时。这是所有默认的BasicHttpBindings。 –

坏HTTP请求得到解决通过添加以下代码:

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(); 

至于服务超时,这是由我的个人防火墙引起的。我必须将Visual Studio添加到允许的应用程序列表中。

重新营业。

+0

辐条太快。测试WCF客户端工作,但通过代码,我无法超过超时。 –

+0

但是,如果我从另一个解决方案(Visual Studio Express的另一个实例)运行我的客户端,它工作正常。之前,我在同一个WPF应用程序中的客户端测试代码中使用了相同的app.config。 –