WCF使用2种不同的行为公开相同的服务合同

时间:2014-08-05 10:24:01

标签: wcf wcf-binding wcf-security

<service behaviorConfiguration="NonSecureBehavior" name="MyNamespace.Service1">
    <endpoint
              address="json"
              behaviorConfiguration="webHttp"
              binding="customBinding"
              bindingConfiguration="jsonpBinding"
              contract="MyNamespace.IService1">
        <identity>
            <dns value="localhost" />
        </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
<service behaviorConfiguration="SecureBehavior" name="MyNamespace.Service1">
    <endpoint
              address="soap"
              binding="customBinding"
              bindingConfiguration="secureBinding"
              contract="MyNamespace.IService1">
        <identity>
            <dns value="xyz" />
        </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>

这会引发错误,因为有2个服务具有相同的名称。 如果我更改其中任何一个名称,则会引发另一个错误,如发布here

1 个答案:

答案 0 :(得分:0)

这对我有用:

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="DefaultBinding"/>
      </basicHttpBinding>
      <webHttpBinding>
        <binding name="DefaultBinding" />
      </webHttpBinding>
    </bindings>
    <services>
      <service name="Services.IdentitiesService" behaviorConfiguration="DefaultBehavior">
        <endpoint address="soap" contract="Namespace.IIdentitiesService" binding="basicHttpBinding" bindingNamespace="http://schemas.example.com/identities" bindingConfiguration="DefaultBinding" />
        <endpoint address="json" contract="Namespace.IIdentitiesService" binding="webHttpBinding" bindingNamespace="http://schemas.example.com/identities" bindingConfiguration="DefaultBinding" behaviorConfiguration="JsonBehavior" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="DefaultBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="JsonBehavior">
          <webHttp helpEnabled="true" automaticFormatSelectionEnabled="true" />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>

json的方法应标有WebInvokeAttribute

[ServiceContract(Name = "IdentitiesService", Namespace = "http://schemas.example.com/identities")]
public interface IIdentitiesService
{
    [OperationContract]
    [WebInvoke]
    IEnumerable<string> TestMethod(string input);
}