是否可以让 WCF 服务使用 JSON 作为请求和响应格式来支持会话?
我需要保护我的应用程序,该应用程序当前正在与 JSON 格式的 WCF 服务器通信并在每次调用时进行身份验证。
我只想在登录时进行身份验证,然后在会话中处理其余请求,但是当我尝试创建这样的服务时,我需要将绑定更改为 wsHttpBinding 或 netTCPBinding,而在我这样做的那一刻,服务器不再接受我的 JSON 请求。但是,它确实接受来自我用 C# 编写的测试客户端的请求,只需使用“添加服务引用”工具即可。
使用 fiddler,我发现 C# 客户端通过大量臃肿的 XML 与我的服务通信。
这是我的 web.config:
<?xml version="1.0"?>
<configuration>
<system.web>
<!--
Set compilation debug="true" to insert debugging
symbols into the compiled page. Because this
affects performance, set this value to true only
during development.
-->
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5cxxxxxxe089"/>
</assemblies>
</compilation>
<!--
The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
-->
<authentication mode="Windows"/>
<!--
The <customErrors> section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to configure html error pages
to be displayed in place of a error stack trace.
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
</system.web>
<!--
The system.webServer section is required for running ASP.NET AJAX under Internet
Information Services 7.0. It is not necessary for previous version of IIS.
-->
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="MobiServiceLibrary.MobiServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<!--<endpointBehaviors>
<behavior name="MobiServiceLibrary.MobiServiceBehavior">
<webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json" automaticFormatSelectionEnabled="true"/>
</behavior>
</endpointBehaviors>-->
</behaviors>
<services>
<service behaviorConfiguration="MobiServiceLibrary.MobiServiceBehavior" name="MobiServiceLibrary.MobiService">
<endpoint address="" binding="wsHttpBinding" contract="MobiServiceLibrary.IMobiService">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<!--<serviceHostingEnvironment multipleSiteBindingsEnabled="true" minFreeMemoryPercentageToActivateService="1">
</serviceHostingEnvironment>-->
</system.serviceModel>
</configuration>
我的登录“合同”:
[ServiceContract(SessionMode = SessionMode.Required)]
public partial interface IMobiService
{
[OperationContract(IsInitiating=true)]
[WebInvoke(Method = "POST", UriTemplate = "/Login", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat=WebMessageFormat.Json, RequestFormat=WebMessageFormat.Json)]
userData login(string pUserName, string pPassword, string pDeviceType);
}
我的登录“实施”:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession,AddressFilterMode=AddressFilterMode.Any)]
public partial class MobiService : IMobiService
{
public userData login(string pUserName, string pPassword, string pDeviceType)
{
//Do Login
}
}