2011-09-28 239 views
0

我有一個返回JSON數據返回給應用程序(不是ASP.NET Web應用程序)一個WCF Web服務在這裏返回*裸* JSON是代碼:從WCF Web服務

[ServiceContract(Namespace = "Navtrak.Services.WCF.MobileAPI")] 
    public interface IJobServiceJSON 
    { 
     [OperationContract] 
     [ServiceKnownType(typeof(Job))] 
     [WebInvoke(
      Method = "GET", 
      BodyStyle = WebMessageBodyStyle.WrappedRequest, 
      ResponseFormat = WebMessageFormat.Json)] 
     PagedResult<Job> ReadJobs(int userId, int? startIndex, int? maxResults, string searchTerm, string sortBy); 
    } 

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
    public class JobServiceJSON : IJobServiceJSON 
    { 
     public PagedResult<Job> ReadJobs(int userId, int? startIndex, int? maxResults, string searchTerm, string sortBy) 
     { 
      var service = new JobService(); 
      return service.ReadJobs(userId, startIndex, maxResults, searchTerm, sortBy); 
     } 
    } 

<%@ ServiceHost Language="C#" Debug="true" Service="Navtrak.Services.WCF.MobileAPI.JobServiceJSON" CodeBehind="JobServiceJSON.svc.cs" Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory" %> 

這裏是web.config設置:

<services> 
     <service name="JobServiceJSON" behaviorConfiguration="HttpGetMetadata"> 
     <endpoint contract="Navtrak.Services.WCF.MobileAPI.Interfaces.IJobServiceJSON" binding="basicHttpBinding" behaviorConfiguration="AjaxBehavior" /> 
     </service> 
    </services> 

    <behaviors> 
     <serviceBehaviors> 
     <behavior name="HttpGetMetadata"> 
      <serviceMetadata httpGetEnabled="true"/> 
      <serviceDebug includeExceptionDetailInFaults="true"/> 
     </behavior> 
     </serviceBehaviors> 
     <endpointBehaviors> 
     <behavior name="AjaxBehavior"> 
      <!--<enableWebScript />--> 
      <webHttp defaultOutgoingResponseFormat="Json"/> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 

這種方法的問題是,它返回的JSON包裹在一個「d」的對象,這並不可怕,但它也返回了「___type」屬性每個對象都帶有該類的名稱空間。這確實漲大了JSON的大小,因爲在這裏看到:

{"d":{"__type":"PagedResultOfJobPUkCTgiD:#Navtrak.Business.Schemas.CommonSchemas.Schemas","Results":[{"__type":"Job:#Navtrak.Business.Schemas.CommonSchemas.Schemas.Job","Address":"1011 Granite Ct, Salisbury, MD 21804-8609,","Contact":null,"CreateDate":"\/Date(1313514808000-0400)\/","DstActive":0,"Id":18416,"JobAttributes":[{"__type":"JobAttribute:#Navtrak.Business.Schemas.CommonSchemas.Schemas.Job","Id":0,"JobAttributeId":0,"JobId":0,"MobileDisplay":1,"Name":"","Sequence":0,"Value":"1011 Granite Ct, Salisbury, MD 21804-8609"},{"__type":"JobAttribute:#Navtrak.Business.Schemas.CommonSchemas.Schemas.Job","Id":0,"JobAttributeId":0,"JobId":0,"MobileDisplay":1,"Name":"","Sequence":1,"Value":"1011 Granite Ct, Salisbury, MD 21804-8609,"},{"__type":"JobAttribute:#Navtrak.Business.Schemas.CommonSchemas.Schemas.Job","Id":0,"JobAttributeId":0,"JobId":0,"MobileDisplay":0,"Name":"","Sequence":4,"Value":"2:00p ET"},{"__type":"JobAttribute:#Navtrak.Business.Schemas.CommonSchemas.Schemas.Job","Id":0,"JobAttributeId":0,"JobId":0,"MobileDisplay":1,"Name":"","Sequence":5,"Value":"test job!"}],"JobTextForMobileDisplay":null,"Latitude":38352974,"Longitude":-75563528,"Name":"1011 Granite Ct, Salisbury, MD 21804-8609","Notes":"test job!","Phone":null,"PromisedDate":"\/Date(1313604000000-0400)\/","ScheduledDay":"\/Date(1313553600000-0400)\/","ScheduledTimeFrom":0,"ScheduledTimeTo":0,"Status":10,"TimeZoneOffset":0},{"__type":"Job:#Navtrak.Business.Schemas.CommonSchemas.Schemas.Job","Address":", , ,","Contact":null,"CreateDate":"\/Date(1313515111000-0400)\/","DstActive":0,"Id":18419,"JobAttributes":[{"__type":"JobAttribute:#Navtrak.Business.Schemas.CommonSchemas.Schemas.Job","Id":0,"JobAttributeId":0,"JobId":0,"MobileDisplay":1,"Name":"","Sequence":1,"Value":", , ,"},{"__type":"JobAttribute:#Navtrak.Business.Schemas.CommonSchemas.Schemas.Job","Id":0,"JobAttributeId":0,"JobId":0,"MobileDisplay":1,"Name":"","Sequence":0,"Value":"1 test place jul 13"},{"__type":"JobAttribute:#Navtrak.Business.Schemas.CommonSchemas.Schemas.Job","Id":0,"JobAttributeId":0,"JobId":0,"MobileDisplay":0,"Name":"","Sequence":4,"Value":"2:00p ET"},{"__type":"JobAttribute:#Navtrak.Business.Schemas.CommonSchemas.Schemas.Job","Id":0,"JobAttributeId":0,"JobId":0,"MobileDisplay":1,"Name":"","Sequence":5,"Value":"test"}],"JobTextForMobileDisplay":null,"Latitude":38774850,"Longitude":-96554630,"Name":"1 test place jul 13","Notes":"test","Phone":null,"PromisedDate":"\/Date(1313604000000-0400)\/","ScheduledDay":"\/Date(1313553600000-0400)\/","ScheduledTimeFrom":0,"ScheduledTimeTo":0,"Status":10,"TimeZoneOffset":0},{"__type":"Job:#Navtrak.Business.Schemas.CommonSchemas.Schemas.Job","Address":"1011 Granite Ct, Salisbury, MD 21804-8609,","Contact":null,"CreateDate":"\/Date(1313515357000-0400)\/","DstActive":0,"Id":18420,"JobAttributes":[{"__type":"JobAttribute:#Navtrak.Business.Schemas.CommonSchemas.Schemas.Job","Id":0,"JobAttributeId":0,"JobId":0,"MobileDisplay":1,"Name":"","Sequence":0,"Value":"1011 Granite Ct, Salisbury, MD 21804-8609"},{"__type":"JobAttribute:#Navtrak.Business.Schemas.CommonSchemas.Schemas.Job","Id":0,"JobAttributeId":0,"JobId":0,"MobileDisplay":1,"Name":"","Sequence":1,"Value":"1011 Granite Ct, Salisbury, MD 21804-8609,"},{"__type":"JobAttribute:#Navtrak.Business.Schemas.CommonSchemas.Schemas.Job","Id":0,"JobAttributeId":0,"JobId":0,"MobileDisplay":0,"Name":"","Sequence":4,"Value":"2:00p ET"},{"__type":"JobAttribute:#Navtrak.Business.Schemas.CommonSchemas.Schemas.Job","Id":0,"JobAttributeId":0,"JobId":0,"MobileDisplay":1,"Name":"","Sequence":5,"Value":"test"}],"JobTextForMobileDisplay":null,"Latitude":38352974,"Longitude":-75563528,"Name":"1011 Granite Ct, Salisbury, MD 21804-8609","Notes":"test","Phone":null,"PromisedDate":"\/Date(1313604000000-0400)\/","ScheduledDay":"\/Date(1313553600000-0400)\/","ScheduledTimeFrom":0,"ScheduledTimeTo":0,"Status":10,"TimeZoneOffset":0}],"TotalRecords":4}} 

我一直在試圖返回BARE JSON,但一直沒能弄明白。如果我將BodyStyle更改爲WebMessageBodyStyle.Bare,那麼它會給我一個錯誤,即除WrappedRequest之外的任何類型都與Web腳本行爲不兼容。如果我從svc文件中刪除Factory =「System.ServiceModel.Activation.WebScriptServiceHostFactory」,那麼如果我嘗試從Web瀏覽器調用json服務/方法,它將返回400錯誤的請求錯誤。

另一個與此相關的網站說明,我認爲在AjaxBehavior部分中設置「enableWebScript」將與svc文件中的Factory =設置相同,但它沒有做任何事情。我必須設置Factory =以便它正確地返回JSON並且不會給出400錯誤。

那麼,關於如何讓這個工作的任何建議?這個JSON服務被移動應用程序調用,所以我真的不想返回所有的「___type」屬性。

回答

1

如果您使用WebScriptServiceHostFactory,則根本不需要使用config(並且由於<service>元素中的「name」屬性沒有名稱空間,所以我認爲它無論如何都被忽略)。 WebScriptServiceHostFactory旨在與ASP.NET AJAX框架一起使用,而不是一般用法。如果將其替換爲WebServiceHostFactory,則應該看到對象的{"d":環繞和__type提示。

+0

真棒,這工作得很好!其中一個問題是服務方法中的可空參數停止工作,但是您可以擴展WCF來處理它們,或者停止使用可爲空的參數。我想知道是否有一種方法可以在web.config中將工廠設置爲WebServiceHostFactory,而不是在.svc中,我討厭在那裏設置內容,因爲沒有人會注意到它。 – Justin