21
我有以下XML配置如何在代碼而不是配置中創建WCF EndPointBehaviors?
<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");
WebServiceHost工作。非常感謝!!! – 2010-01-26 21:49:48
我想添加行爲restSEP.Behaviors.Add(新的WebHttpBehavior());'它的工作!謝謝! – 2013-07-24 15:53:30