我有一個WCF數據服務,我想在所有操作中默認返回JSON。有沒有我可以在配置/通過服務屬性中設置的地方?默認啓用WCF數據服務來接受/返回JSON
14
A
回答
0
您可以根據此下載添加擴展名。
http://archive.msdn.microsoft.com/DataServicesJSONP
你仍然需要定製的代碼檢查,看看你所要求的通過URL.e.g $格式= json的JSON格式。
7
爲了通過這樣的$格式標記,使JSON:
host:8038/YourService.svc/?$format=json
這個屬性添加到您的服務聲明:
[JSONPSupportBehavior]
public class Service : DataService<YourEntities>
添加爲一個類爲您服務:
using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.Text;
using System.Xml;
namespace YourNamespaceHere.Service
{
public class JSONPSupportInspector : IDispatchMessageInspector
{
// Assume utf-8, note that Data Services supports
// charset negotation, so this needs to be more
// sophisticated (and per-request) if clients will
// use multiple charsets
private static Encoding encoding = Encoding.UTF8;
#region IDispatchMessageInspector Members
public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel, InstanceContext instanceContext)
{
if (request.Properties.ContainsKey("UriTemplateMatchResults"))
{
HttpRequestMessageProperty httpmsg = (HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name];
UriTemplateMatch match = (UriTemplateMatch)request.Properties["UriTemplateMatchResults"];
string format = match.QueryParameters["$format"];
if ("json".Equals(format, StringComparison.InvariantCultureIgnoreCase))
{
// strip out $format from the query options to avoid an error
// due to use of a reserved option (starts with "$")
match.QueryParameters.Remove("$format");
// replace the Accept header so that the Data Services runtime
// assumes the client asked for a JSON representation
httpmsg.Headers["Accept"] = "application/json";
string callback = match.QueryParameters["$callback"];
if (!string.IsNullOrEmpty(callback))
{
match.QueryParameters.Remove("$callback");
return callback;
}
}
}
return null;
}
public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
{
if (correlationState != null && correlationState is string)
{
// if we have a JSONP callback then buffer the response, wrap it with the
// callback call and then re-create the response message
string callback = (string)correlationState;
XmlDictionaryReader reader = reply.GetReaderAtBodyContents();
reader.ReadStartElement();
string content = JSONPSupportInspector.encoding.GetString(reader.ReadContentAsBase64());
content = callback + "(" + content + ")";
Message newreply = Message.CreateMessage(MessageVersion.None, "", new Writer(content));
newreply.Properties.CopyProperties(reply.Properties);
reply = newreply;
}
}
#endregion
class Writer : BodyWriter
{
private string content;
public Writer(string content)
: base(false)
{
this.content = content;
}
protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
{
writer.WriteStartElement("Binary");
byte[] buffer = JSONPSupportInspector.encoding.GetBytes(this.content);
writer.WriteBase64(buffer, 0, buffer.Length);
writer.WriteEndElement();
}
}
}
// Simply apply this attribute to a DataService-derived class to get
// JSONP support in that service
[AttributeUsage(AttributeTargets.Class)]
public class JSONPSupportBehaviorAttribute : Attribute, IServiceBehavior
{
#region IServiceBehavior Members
void IServiceBehavior.AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
}
void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
foreach (ChannelDispatcher cd in serviceHostBase.ChannelDispatchers)
{
foreach (EndpointDispatcher ed in cd.Endpoints)
{
ed.DispatchRuntime.MessageInspectors.Add(new JSONPSupportInspector());
}
}
}
void IServiceBehavior.Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
}
#endregion
}
}
相關問題
- 1. WCF Rest服務接受Json數據
- 2. WCF服務 - JSON - 沒有數據返回
- 3. WCF服務不返回數據的JSON
- 4. 使一個WCF服務接受來自jQuery.AJAX的JSON數據()
- 5. 從wcf服務返回wcf/json結果
- 6. JSON使用WCF服務返回空值
- 7. WCF服務將JSON數據返回到Web應用程序
- 8. 使用netTcpBinding從WCF服務返回JSON數據
- 9. WCF服務返回另一個服務返回的JSON
- 10. 可以使用WCF數據服務來返回文件嗎?
- 11. WCF Json數據返回
- 12. WCF JSON返回XML數據!
- 13. 從WCF數據服務返回BSonDocuments
- 14. 從WCF服務返回數據表
- 15. WCF服務不返回數據
- 16. WCF數據服務認證
- 17. WCF 4服務與json自定義錯誤處理程序返回202接受
- 18. 指示WCF服務返回JSon
- 19. WCF服務不返回正確JSON
- 20. 如何從WCF服務返回Json?
- 21. 無法返回JSON數據,WCF Resful服務.NET 4.0
- 22. WCF數據服務是否返回無效的JSON?
- 23. jquery wcf數據服務返回原子+ xml而不是json
- 24. 在WCF REST服務中返回非JSON,非XML數據
- 25. 檢索WCF休息服務JSON數據返回
- 26. 如何處理從WCF數據服務(OData)返回的json DateTime
- 27. 由WCF數據服務(或IIS 7.5?)返回的JSON限制
- 28. WCF服務JSON數據
- 29. WCF 4:默認情況下,WCF服務
- 30. 如何通過AJAX調用Web服務來返回Json數據?
嗨,是否有可能在客戶端實現DataService的IDispatchMessageInspector?我創建了這個問題:http://stackoverflow.com/questions/18613078/c-sharp-implement-iclientmessageinspector-in-wcf-data-service – VAAA 2013-09-04 14:34:30
你可能想使用「application/json; odata = verbose」 – wilsjd 2013-10-04 20:41:50