basicHttpBinding,webHttpBinding& mexHttpBinding端点在一个WCF服务中共存

时间:2011-11-30 09:54:20

标签: wcf mex basichttpbinding webhttpbinding

我有一个WCF服务(目标.NET 4),我需要公开4个端点:SOAP,POX,JSON& MEX。

我的配置文件如下所示:

<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[]&amp; outputs)
System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc)
System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp; rpc)
System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&amp; 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端点?

2 个答案:

答案 0 :(得分:3)

如果您的目标是为xml和json提供1种方法,我可以实现它,如下所示:

<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 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元素来执行上述操作

答案 1 :(得分:0)

嗯,简单地复制具有不同名称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" />
相关问题