【发布时间】:2012-01-14 13:44:51
【问题描述】:
我有这个界面:
[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'。 内容类型应用程序/soap+xml;服务 http://localhost:8000/LocationBasedService 不支持 charset=utf-8。客户端和服务绑定可能不匹配。 远程服务器返回错误:(415)无法处理消息,因为内容类型'application/soap+xml; charset=utf-8' 不是预期的类型 'text/xml;字符集=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。
【问题讨论】:
-
我通过添加 ServiceMetaData 行为在查看元数据方面取得了一些进展,但仍然无法让服务在没有超时的情况下响应简单的客户端请求。这都是默认的 BasicHttpBindings。
标签: wcf servicehost bad-request