Вернуть JSON ответ в WCF REST-сервисов
Я написал службу WCF для возврата ответа JSON.Однако я не мог его получить ?
Вот мой код:
IService.в CS:
[OperationContract] [WebInvoke(UriTemplate = "GetAppointments/?companyId={companyId}&appointmentDate={appointmentDate}", Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)] GetAppointmentList GetAppointment(Guid companyId, string appointmentDate);
Файл service1.в CS :
public GetAppointmentList GetAppointment(Guid companyId, string appointmentDate) { SQLDataContext context = new SQLDataContext(); var getappointment = context.GetAppointmentTimings(companyId,appointmentDate); GetAppointmentList getappointmentlist = new GetAppointmentList(); getappointmentlist.AppointmentList = new List<company>().ToList(); foreach (var r in getappointment.ToList()) { Company company = new Company(); company.CompanyId = r.CompanyId; company.CompanyName = r.CompanyName; company.BreakStartTime = r.BreakStartTime + ":" + "00"; company.BreakEndTime = r.BreakEndTime + ":" + "00"; company.Interval = r.Interval; company.AppointmentStartTime = r.AppointmentStartTime; company.AppointmentEndTime = r.AppointmentEndTime; getappointmentlist.AppointmentList.Add(company); } return getappointmentlist; }
файл web.config:
<system.serviceModel> <services> <service name="Appointments.CompanyService" behaviorConfiguration="SampleCompSvcBehavior"> <host> <baseAddresses> <add baseAddress="http://localhost:8732/SampleCompService"/> </baseAddresses> </host> <endpoint address="http://localhost:8732/SampleCompService/json" binding="webHttpBinding" contract="Appointments.ICompanyService" behaviorConfiguration="SampleCompSvcWebBehavior" bindingConfiguration="hk"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service> </services> <bindings> <webHttpBinding> <binding name="hk"> <security mode="None"></security> </binding> </webHttpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior name="SampleCompSvcBehavior"> <serviceMetadata httpGetEnabled="True"/> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> <endpointBehaviors> <behavior name="SampleCompSvcWebBehavior"> <webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json" automaticFormatSelectionEnabled="true" /> </behavior> </endpointBehaviors> </behaviors> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/> </system.serviceModel>
Где я ошибаюсь ?
Member 10656177
Вы нашли решение этой проблемы? У меня та же проблема. Интересно, связано ли это с использованием файла класса (*.cs), а не служебного файла (*.svc)