Как вернуть JsonP из запроса WCP?
Я хочу создать службу WCF, которая будет возвращать запрос JsonP. Я хочу вернуть следующее,
jsonpCallback ({"fileNames": "IDR023.T.201705201412.png, IDR023.T.201705201418.png"});
Поэтому я создал следующий сервис WCF
IBOM.cs
[ServiceContract]
public interface IBOM
{
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json)]
string GetData();
}
BOM.cs
public class BOM : IBOM
{
public string GetData()
{
return "jsonpCallback({\"fileNames\":\"IDR023.T.201705201412.png, IDR023.T.201705201418.png\"});";
}
}
WebConfig
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="BomService.BOM" behaviorConfiguration="ServiceBehaviour">
<!-- Service Endpoints -->
<!-- Unless fully qualified, address is relative to base address supplied above -->
<endpoint address ="" binding="webHttpBinding" contract="BomService.IBOM" behaviorConfiguration="web">
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
Но я возвращаюсь
"jsonpCallback({\"fileNames\":\"IDR023.T.201705201412.png, IDR023.T.201705201418.png\"});"
Я возвращаю строку и пытаюсь настроить ее так, чтобы она возвращала то, что мне нужно. Это лучший способ для JSONP? или есть лучший способ добиться того, что мне нужно
Спасибо
1 ответ
Решение
Для тех, кто заинтересован, я разработал это. Вот код
Ibom
[ServiceContract] открытый интерфейс IBOM {
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json)]
Stream GetData();
}
BOM
public class BOM : IBOM
{
public Stream GetData()
{
string jsCode = "jsonpCallback" + "({\Test:\"" + fileNames + "\"});";
WebOperationContext.Current.OutgoingResponse.ContentType = "application/javascript";
return new MemoryStream(Encoding.UTF8.GetBytes(jsCode));
}