我有一個WCF服務(目標.NET 4),我需要公開4個端點:SOAP,POX,JSON & MEX。如何basicHttpBinding,webHttpBinding和mexHttpBinding端點共存於一個WCF服務
我的配置文件看起來像這樣:
<system.serviceModel>
<!-- bindings -->
<bindings>
<basicHttpBinding>
<binding name ="soapBinding">
<security mode="None">
</security>
</binding>
</basicHttpBinding>
<webHttpBinding>
<binding name="webBinding">
</binding>
</webHttpBinding>
</bindings>
<!-- behaviors -->
<behaviors>
<endpointBehaviors>
<!-- plain old XML -->
<behavior name="poxBehavior">
<webHttp/>
</behavior>
<!-- JSON -->
<behavior name="jsonBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="defaultBehavior">
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="PubtranService.Service" behaviorConfiguration="defaultBehavior">
<host>
<baseAddresses>
<add baseAddress="http://192.168.1.102:8080/PT/"/>
</baseAddresses>
</host>
<endpoint name="soap"
address="soap"
binding="basicHttpBinding"
bindingConfiguration="soapBinding"
contract="PubtranService.IService" />
<endpoint name="pox"
address="pox"
binding="webHttpBinding"
bindingConfiguration="webBinding"
behaviorConfiguration="poxBehavior"
contract="PubtranService.IService" />
<endpoint name="json"
address="json"
binding="webHttpBinding"
bindingConfiguration="webBinding"
behaviorConfiguration="jsonBehavior"
contract="PubtranService.IService" />
<endpoint name="mex"
address="mex"
binding="mexHttpBinding"
bindingConfiguration=""
contract="IMetadataExchange"/>
</service>
</services>
</system.serviceModel>
當我運行該服務的WCF測試客戶端返回無法添加服務味精用System.NullReferenceException
- 對象引用未設置爲一個實例對象和堆棧跟蹤:
System.ServiceModel.Description.ServiceMetadataBehavior.MetadataExtensionInitializer.GenerateMetadata()
System.ServiceModel.Description.ServiceMetadataExtension.EnsureInitialized() System.ServiceModel.Description.ServiceMetadataExtension.WSMexImpl.GatherMetadata(String dialect, String identifier) System.ServiceModel.Description.ServiceMetadataExtension.WSMexImpl.Get(Message request)
SyncInvokeGet(Object , Object[] , Object[])
System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc)
System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
但JSON和POX端點都可以訪問和工作就好了。 另一方面,當我從配置中註釋掉它們時,WCF Test Client正確顯示SOAP服務。
目前只有一個在服務中實現簡單的方法:
[ServiceContract(Namespace="http://192.168.1.102:8080/")]
public interface IService
{
[OperationContract]
[WebGet]
Tram[] GetOverview(string id);
}
爲什麼我剎車加入痘和JSON端點MEX終結?
謝謝您的回答,但XML和JSON端點工作得很好,甚至在服務界面中甚至沒有一行代碼 - 響應類型自動正確確定。 我的問題是爲什麼mex端點停止工作,當我添加json和pox的時候。 – krzysztofkarolczak
我想我們不能同時使用WebHttpbinding和JHL和Xml端點,因爲您尚未在[WebGet]屬性中指定您的響應/請求類型。只要刪除其中一個,你就可以看到你的肥皂和休息服務將按預期開始工作。我認爲這一定是由於WebHttpBinding – Rajesh
所使用的ServiceHostFactory,Json和Xml同時工作得很好。但是如果我添加至少一個XML或JSON,則SOAP停止工作。 – krzysztofkarolczak