我有一个WCF服务(目标是。net 4),我需要公开4个端点:SOAP, POX, JSON &墨西哥人。
我的配置文件是这样的:
<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测试客户端返回未能添加服务 msg, 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测试客户端正确地显示了SOAP服务。
目前在服务中只实现了一个简单的方法:
[ServiceContract(Namespace="http://192.168.1.102:8080/")]
public interface IService
{
[OperationContract]
[WebGet]
Tram[] GetOverview(string id);
}
为什么我要通过添加pox和json端点来中断mex端点?
如果你的目标是有一个方法为xml和json服务,我可以实现它如下所示:
<service name ="XMLService.MultipleEndpointService">
<endpoint name="soap" address="soap" binding="basicHttpBinding" contract="XMLService.IMultipleEndpointService" />
<endpoint name="pox" address="pox" binding="webHttpBinding" behaviorConfiguration="web" contract="XMLService.IMultipleEndpointService" />
<endpoint name="mex" address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
现在在实现接口的类中,我做了以下操作:
public Tram[] GetOverview(string id)
{
IList<ContentType> acceptHeaderElements = WebOperationContext.Current.IncomingRequest.GetAcceptHeaderElements();
for (int x = 0; x < acceptHeaderElements.Count; x++)
{
string normalizedMediaType = acceptHeaderElements[x].MediaType.ToLowerInvariant();
switch (normalizedMediaType)
{
case "application/xml":
WebOperationContext.Current.OutgoingResponse.Format = WebMessageFormat.Xml;
break;
case "application/json":
WebOperationContext.Current.OutgoingResponse.Format = WebMessageFormat.Json;
break;
}
}
return new[] { new Tram() { TramName = "Tram 1" }, new Tram() { TramName = "Tram 2" } };
}
现在当我使用Fiddler对上述服务执行get时:
GET http://localhost/XMLService/multipleendpointservice.svc/pox/GetOverview HTTP/1.1
User-Agent: Fiddler
Host: localhost
Accept: application/json
我的回答是
HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 79
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Wed, 30 Nov 2011 11:39:16 GMT
[{"<TramName>k__BackingField":"Tram 1"},{"<TramName>k__BackingField":"Tram 2"}]
同样,当我执行:
GET http://localhost/XMLService/multipleendpointservice.svc/pox/GetOverview HTTP/1.1
User-Agent: Fiddler
Host: localhost
Accept: application/xml
我的回答是:
HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 334
Content-Type: application/xml; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Wed, 30 Nov 2011 11:39:09 GMT
<ArrayOfTram xmlns="http://schemas.datacontract.org/2004/07/XMLService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Tram><_x003C_TramName_x003E_k__BackingField>Tram 1</_x003C_TramName_x003E_k__BackingField></Tram><Tram><_x003C_TramName_x003E_k__BackingField>Tram 2</_x003C_TramName_x003E_k__BackingField></Tram></ArrayOfTram>
注意:Accept报头可以不是application/xml或application/json。在这种情况下,你需要处理它。
另一种选择是使用WebServiceHostFactory并使用具有automaticFormatSelection属性的webHttpEndpoint元素来执行上述操作
对绑定配置webBinding
进行简单的复制,使用不同的名称webBinding2
将允许暴露元数据。至少我尝试了一下,它工作了,尽管WCF的这种行为方式很奇怪。
<endpoint name="pox"
address="pox"
binding="webHttpBinding"
bindingConfiguration="webBinding"
behaviorConfiguration="poxBehavior"
contract="PubtranService.IService" />
<endpoint name="json"
address="json"
binding="webHttpBinding"
bindingConfiguration="webBinding2"
behaviorConfiguration="jsonBehavior"
contract="PubtranService.IService" />