我正在尝试制作WCF RESTful网络服务,该服务将同时返回XML
和JSON
。问题是它有效但XML
序列化忽略了XML
属性。
正如您在端点配置中看到的,我有两个地址,如xml
和json
。
所以我像 -
那样访问网址 XML
-
http://localhost:59310/TestService.svc/xml/GetResponse
JSON
-
http://localhost:59310/TestService.svc/json/GetResponse
现在,我希望在访问DataContractFormat
网址时使用JSON
,并使用XmlSerializerFormat
获取XML
网址。我怎样才能做到这一点。请帮忙。
这是具有XML
属性的响应类。
namespace WCFMultiFormatTest
{
[XmlRoot(ElementName = "RESPONSE")]
public class Response
{
[XmlAttribute(AttributeName = "MESSAGE")]
public string Message { get; set; }
}
[XmlRoot(ElementName = "TEST")]
public class Root
{
[XmlElement(ElementName = "RESPONSE")]
public List<Response> Response { get; set; }
}
}
这是服务实现类(svc)。
namespace WCFMultiFormatTest
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class TestService : ITestService
{
public Root GetResponse()
{
Root r = new Root();
r.Response = new List<Response>();
Response r1 = new Response()
{
Message = "Hello"
};
Response r2 = new Response()
{
Message = "World"
};
r.Response.Add(r1);
r.Response.Add(r2);
return r;
}
}
}
这是服务合约界面。
namespace WCFMultiFormatTest
{
[ServiceContract]
public interface ITestService
{
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "/GetResponse")]
Root GetResponse();
}
}
这是web.config
中的服务配置。
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="WebHttpBinding_TestService" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
</security>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="JSONRestBehavior">
<webHttp defaultOutgoingResponseFormat="Json" />
</behavior>
<behavior name="XMLRestBehavior">
<webHttp defaultOutgoingResponseFormat="Xml" />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
<behavior name="TestServiceBehavior">
<serviceThrottling maxConcurrentCalls="300" maxConcurrentSessions="100" maxConcurrentInstances="2147483647" />
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="TestServiceBehavior" name="WCFMultiFormatTest.TestService">
<endpoint address="XML" binding="webHttpBinding" bindingConfiguration="WebHttpBinding_TestService" contract="WCFMultiFormatTest.ITestService" behaviorConfiguration="XMLRestBehavior"></endpoint>
<endpoint address="JSON" binding="webHttpBinding" bindingConfiguration="WebHttpBinding_TestService" contract="WCFMultiFormatTest.ITestService" behaviorConfiguration="JSONRestBehavior"></endpoint>
</service>
</services>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>