【发布时间】:2015-01-20 00:13:57
【问题描述】:
我有一个名为AuthenticationService 的服务的以下代码:
IAuthenticationService.cs
[ServiceContract]
public interface IAuthenticationService
{
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
bool Login(string username, string password, string applicationName);
}
AuthenticationService.svc.cs
public sealed class AuthenticationService : IAuthenticationService
{
public bool Login(string username, string password, string applicationName)
{
// TODO: add the logic to authenticate the user
return true;
}
}
Web.config
<system.serviceModel>
<!-- START: to return JSON -->
<services>
<service name="ImageReviewPoc.Service.AuthenticationService">
<endpoint contract="ImageReviewPoc.Service.Contracts.IAuthenticationService" binding="webHttpBinding" behaviorConfiguration="jsonBehavior"/>
</service>
</services>
<!-- END: to return JSON -->
<behaviors>
<!-- START: to return JSON -->
<endpointBehaviors>
<behavior name="jsonBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
<!-- END: to return JSON -->
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="false"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add scheme="http" binding="webHttpBinding"/>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
我有以下使用该服务的控制台应用程序:
Program.cs
internal class Program
{
private static void Main(string[] args)
{
Login().Wait();
}
private static async Task Login()
{
var client = new AuthenticationServiceClient();
var ok = await client.LoginAsync("User", "Password!", "Console Application");
client.Close();
Console.WriteLine(ok);
}
}
App.config
<system.serviceModel>
<client>
<endpoint address="http://localhost:62085/AuthenticationService.svc/"
binding="webHttpBinding"
contract="AuthenticationServiceReference.IAuthenticationService"
kind="webHttpEndpoint" />
</client>
</system.serviceModel>
我使用Postman 来测试发送以下 JSON 数据的服务并且它工作正常:
{"username": "reviewer", "password": "456", "applicationName": "123"}
但是,当我使用控制台应用程序测试服务时,我得到了一个
System.InvalidOperationException:合约“IAuthenticationService”的操作“登录”指定要序列化的多个请求主体参数,而无需任何包装器元素。最多一个 body 参数可以在没有包装元素的情况下被序列化。删除额外的正文参数或将 WebGetAttribute/WebInvokeAttribute 上的 BodyStyle 属性设置为 Wrapped。
从IAuthenticationService.cs 代码可以看出,我已经将BodyStyle 设置为Wrapped。有人可以指导我在这里做错了什么吗?
注意以下几点:
- 我已经在 Stackoverflow 和互联网上搜索解决方案。几乎所有的解决方案都是关于设置
BodyStyle。它可能帮助了其他人,但对我没有多大帮助。 -
Login和LoginAsync都试过了;结果是一样的 - 我通过 Visual Studio 2013(终极版,如果您想知道的话)中的“添加服务引用”添加它来引用该服务
- 我知道我可以使用其他方式调用该服务;例如,HttpClient,但我想知道的是为什么自动生成的客户端不起作用
【问题讨论】:
-
在更改为
BodyStyle后,您是否已经在客户端更新了您的服务参考? -
我认为,进入的 json 请求不正确。你能和提琴手核对一下,看看请求是什么样的。可能是请求格式不正确...
-
@khlr,是的,我更改了服务并更新了参考至少 739,000 次 :)
-
@Saravanan,我会检查一下。我不确定它是否能到达服务器。当客户端(控制台应用程序)尝试调用该方法时,会在客户端(控制台应用程序)中引发异常。
-
@Saravanan,正如我所怀疑的,它甚至没有到达服务器。