मेरे पास एक डब्ल्यूसीएफ डेटा सेवा है जो मैं सभी परिचालनों के लिए डिफ़ॉल्ट रूप से जेएसओएन वापस करना चाहता हूं। क्या कोई ऐसी जगह है जिसे मैं कॉन्फ़िगरेशन/सेवा विशेषताओं के माध्यम से सेट कर सकता हूं?डिफ़ॉल्ट रूप से JSON को स्वीकार/वापस करने के लिए WCF डेटा सेवा सक्षम करें
14
A
उत्तर
0
आप इस डाउनलोड के अनुसार एक एक्सटेंशन जोड़ सकते हैं।
http://archive.msdn.microsoft.com/DataServicesJSONP
तुम अब भी रूप में कोड यदि आप URL.e.g $ प्रारूप = json के माध्यम से JSON प्रारूपण के लिए पूछ रहे हैं देखने के लिए जाँच कर रहा है यह अनुकूलित करने के लिए की आवश्यकता होगी।
7
इस तरह $ प्रारूप टैग के माध्यम से json सक्षम करने के लिए आदेश में:
[JSONPSupportBehavior]
public class Service : DataService<YourEntities>
आपकी सेवा के लिए एक वर्ग के रूप में इस जोड़ें::
host:8038/YourService.svc/?$format=json
आपकी सेवा घोषणा करने के लिए इस विशेषता जोड़ें
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. अमान्य JSON को WCF
- 2. WCF सेवा
- 3. डिफ़ॉल्ट कैसे करें svn: सक्षम करने के लिए कीवर्ड?
- 4. WCF XML serialization को अनुकूलित करने के लिए कैसे करें
- 5. WCF सेवा
- 6. सेवा और डेटा अनुबंधों के लिए सर्वश्रेष्ठ अभ्यास - WCF
- 7. मैं एक wcf सेवा
- 8. WCF सेवा मूलभूत मूल्यों
- 9. WCF वेब सेवा विफल पर बड़ा डेटा
- 10. क्या यह डिफ़ॉल्ट रूप से UnsafeHeaderParsing को सक्षम करने के लिए एक स्वीकार्य अभ्यास है?
- 11. WCF, LINQ, JSON
- 12. WCF सेवा
- 13. WCF सेवा
- 14. WCF बाकी सेवा प्रतिक्रियाओं
- 15. WCF सेवा कनेक्शन आईआईएस
- 16. wcf सेवा से वापसी एचटीएमएल
- 17. WCF सेवा
- 18. सरल WCF सेवा, सेवा
- 19. JSON अनुरोध द्वारा fiddler से wcf restful सेवा को कैसे कॉल करें?
- 20. एक WCF सेवा कार्यान्वयन
- 21. WCF सेवा
- 22. एंड्रॉइड से फ़ाइलों को अपलोड करें wcf
- 23. कॉलिंग WCF सेवा POST पद्धति
- 24. WCF, पोस्टिंग JSONized डेटा
- 25. एक async WCF सेवा
- 26. WCF 4 साबुन और JSON Endpoint DotNetOpenAuth
- 27. एक RESTful WCF सेवा
- 28. WCF सेवा शुरू करने के लिए जब एक से अधिक सेवा उपलब्ध
- 29. प्रगति स्पष्ट रूप से एआरसी सक्षम करने के लिए?
- 30. गतिशील रूप से विजेट को सक्षम या अक्षम करने से
हाय क्या क्लाइंट पक्ष पर IDSpatchMessageInspector को डेटा सेवा के लिए लागू करना संभव है? मैं यह प्रश्न बनाता हूं: http://stackoverflow.com/questions/18613078/c-sharp-implement-iclientmessageinspector-in-wcf-data- सेवा – VAAA
आप "एप्लिकेशन/जेसन; odata = verbose" का उपयोग करना चाह सकते हैं – wilsjd