Вот что у меня работает:
Создание проекта библиотеки классов WCF в VS2012.
Схема Xsd для типов данных:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns="urn:wcf:hydra:datatypes.xsd:1.5" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:wcf:hydra:datatypes.xsd:1.5" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="IncomingDocument">
<xs:complexType>
<xs:sequence>
<xs:element name="Firstname" type="xs:string"/>
<xs:element name="Surname" type="xs:string"/>
<xs:element name="ChildrensNames" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="ChildsName" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Gender" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Male"/>
<xs:enumeration value="Female"/>
<xs:enumeration value="Other"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
<xs:attribute name="Version" type="xs:string" use="required" fixed="1.5"/>
</xs:complexType>
</xs:element>
<xs:element name="ResponseDocument">
<xs:complexType>
<xs:sequence>
<xs:element name="NumberOfChildren" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="DayDetails">
<xs:complexType>
<xs:sequence>
<xs:element name="CurrentDateTime" type="xs:dateTime"/>
<xs:element name="WeatherDetails" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Два ServiceContractsОбратите внимание, что каждый добавляет волшебное слово, [XmlSerializserFormat]
[ServiceContract]
public interface ITestMutliHeadService
{
[OperationContract]
[WebInvoke(UriTemplate = "loadchildren")]
ResponseDocument LoadChildren(IncomingDocument request);
[OperationContract]
[WebGet(UriTemplate = "daydetails")]
DayDetails DayDetails();
[OperationContract]
[WebGet(UriTemplate = "test/{input}")]
string Test(string input);
}
[ServiceContract]
[XmlSerializerFormat]
public interface ITestMutliHeadServiceSchemaCompliant : ITestMutliHeadService
{
}
Реализуйте ваш конкретный класс с двумя интерфейсами:
public sealed class TestMutliHeadService : ITestMutliHeadService, ITestMutliHeadServiceSchemaCompliant
{
public ResponseDocument LoadChildren(IncomingDocument request)
{
int count = 0;
if (request.ChildrensNames != null)
{
count = request.ChildrensNames.Count();
}
return new ResponseDocument()
{
NumberOfChildren = count,
};
}
public DayDetails DayDetails()
{
return new DayDetails()
{
CurrentDateTime = DateTime.Now,
WeatherDetails = "It's rather nice, really.",
};
}
public string Test(string input)
{
return input;
}
}
Теперь создайте пустой проект ASP.NET и добавьте web.config
и Global.asax
и ссылка на System.ServiceModel.Activation
Global.asax.cs:
public class Global : HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes();
}
private void RegisterRoutes()
{
var factory = new ServiceHostFactory();
RouteTable.Routes.Add(new ServiceRoute("", factory, typeof(TestMutliHeadService)));
}
}
Вот волшебный монстр, system.serviceModel в web.config:
<system.serviceModel>
<!-- Services -->
<services>
<service name="WCF.Hydra.BL.TestMutliHeadService">
<endpoint name="restJson" address="rest/json" behaviorConfiguration="restJsonEndpointBehaviour" binding="webHttpBinding" bindingConfiguration="restBindingConfig" contract="WCF.Hydra.BL.ITestMutliHeadService"/>
<endpoint name="restJsonSecure" address="rest/json" behaviorConfiguration="restJsonEndpointBehaviour" binding="webHttpBinding" bindingConfiguration="restSecureBindingConfig" contract="WCF.Hydra.BL.ITestMutliHeadService"/>
<endpoint name="restXml" address="rest/xml" behaviorConfiguration="restXmlEndpointBehaviour" binding="webHttpBinding" bindingConfiguration="restBindingConfig" contract="WCF.Hydra.BL.ITestMutliHeadServiceSchemaCompliant"/>
<endpoint name="restXmlSecure" address="rest/xml" behaviorConfiguration="restXmlEndpointBehaviour" binding="webHttpBinding" bindingConfiguration="restSecureBindingConfig" contract="WCF.Hydra.BL.ITestMutliHeadServiceSchemaCompliant"/>
<endpoint name="mex" address="mex" behaviorConfiguration="" binding="mexHttpBinding" bindingConfiguration="mexBindingConfig" contract="WCF.Hydra.BL.ITestMutliHeadService"/>
<endpoint name="mexSecure" address="mex" behaviorConfiguration="" binding="mexHttpsBinding" bindingConfiguration="mexSecureBindingConfig" contract="WCF.Hydra.BL.ITestMutliHeadService"/>
<endpoint name="soapXsd" address="soap" behaviorConfiguration="" binding="basicHttpBinding" bindingConfiguration="soapBindingConfig" contract="WCF.Hydra.BL.ITestMutliHeadService"/>
<endpoint name="soapXsdSecure" address="soap" behaviorConfiguration="" binding="basicHttpsBinding" bindingConfiguration="soapSecureBindingConfig" contract="WCF.Hydra.BL.ITestMutliHeadService"/>
</service>
</services>
<!-- Binding Configurations -->
<bindings>
<webHttpBinding>
<binding name="restBindingConfig">
<security mode="None"/>
</binding>
<binding name="restSecureBindingConfig">
<security mode="Transport"/>
</binding>
</webHttpBinding>
<mexHttpBinding>
<binding name="mexBindingConfig"/>
</mexHttpBinding>
<mexHttpsBinding>
<binding name="mexSecureBindingConfig"/>
</mexHttpsBinding>
<basicHttpsBinding>
<binding name="soapSecureBindingConfig">
<security mode="Transport"/>
</binding>
</basicHttpsBinding>
<basicHttpBinding>
<binding name="soapBindingConfig">
<security mode="None"/>
</binding>
</basicHttpBinding>
</bindings>
<!-- Behaviour Configurations -->
<behaviors>
<endpointBehaviors>
<behavior name="restJsonEndpointBehaviour">
<webHttp helpEnabled="true" defaultBodyStyle="Bare" defaultOutgoingResponseFormat="Json" automaticFormatSelectionEnabled="false" faultExceptionEnabled="true" />
</behavior>
<behavior name="restXmlEndpointBehaviour">
<webHttp helpEnabled="true" defaultBodyStyle="Bare" defaultOutgoingResponseFormat="Xml" automaticFormatSelectionEnabled="false" faultExceptionEnabled="true" />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
</behavior>
</serviceBehaviors>
</behaviors>
<!-- Hosting Environment Settings -->
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
</system.serviceModel>
Перейдите к / soap для мыла, / rest / json для restful json и / rest / xml для схемы-совместимого ish xml.