WCF:自托管服务上的 HTTP 400 错误请求
我有这个接口:
[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要求。如果我尝试使用 Visual Studio 的“添加服务引用”创建 WCF 客户端,则会收到以下错误:
“http://localhost:8000/LocationBasedService”。 内容类型 application/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。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过添加以下代码解决了错误的 HTTP 请求:
至于服务超时,这是由我的个人防火墙引起的。我必须将 Visual Studio 添加到允许的应用程序列表中。
回来做生意。
The Bad HTTP request was solved by adding this code:
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.