是否有可能使用JSON作为请求和响应格式来支持会话的WCF服务?同时使用JSON和会话的WCF服务?
我需要保护我的应用程序,该应用程序当前正与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
}
}
如何将服务标记为scriptService?通过WebConfig或合同属性? 我在IIS8上托管,但II7也没有工作。我也直接从Visual Studio在我的本地PC上运行它进行调试。 当我使用fiddler调用登录时,在本地运行的服务上,返回错误:** HTTP/1.1 415 Unsupported Media Type **。 在IIS8上,完全一样,我得到这个错误:** HTTP/1.1 415由于内容类型'application/json'不是预期类型'application/soap + xml;字符集= UTF-8' **。 – 2013-03-18 07:57:50
我编辑了我的问题以包含代码。 – 2013-03-18 08:15:37
这意味着您的登录服务不期待json。它正在寻找肥皂。所以你必须向登录服务发出肥皂请求。 – Siby 2013-03-25 13:36:37