是否有可能使用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