我有以下的XML配置
<system.serviceModel>
<services>
<service name="MyService.MyServiceREST" behaviorConfiguration="MyServiceTypeBehaviors">
<host>
<baseAddresses>
<add baseAddress="http://localhost:1234/MyService/xml"/>
</baseAddresses>
</host>
<endpoint address="" binding="webHttpBinding" behaviorConfiguration="xmlBehavior" contract="MyService.IMyService" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors" >
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="True"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="xmlBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
我想在C#代码中实现,而不是使用配置文件。
我无法弄清如何使用webHttp做EndPoint,将此服务公开为REST服务。
ServiceHost serviceHost = new ServiceHost(singletonInstance, "http://localhost:1234/MyService/xml");
// Create Meta Behavior
ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
behavior.HttpGetEnabled = true;
serviceHost.Description.Behaviors.Add(behavior);
Binding mexBinding = MetadataExchangeBindings.CreateMexHttpBinding();
serviceHost.AddServiceEndpoint(typeof(IMetadataExchange), mexBinding, "mex");
WSHttpBinding httpBinding = new WSHttpBinding(SecurityMode.None);
serviceHost.AddServiceEndpoint(typeof(MyService.IMyService), httpBinding, "rest");
restSEP.Behaviors.Add(new WebHttpBehavior());
,加上之后就可以了!谢谢! - Tono Nam