简体   繁体   中英

IIS WCF Config: service only uses default behavior

I am learning about WCF deployment in IIS and I have found something strange. Basically my service only uses the default behavior regardless of how I set the behaviorConfiguration attribute of element in web.config.

So here's the relevant piece of my web.config:

<system.serviceModel>
<services>
  <service name="TableImport" behaviorConfiguration="MyServiceTypeBehaviors">
    <endpoint address="" binding="wsHttpBinding" />
  </service>
</services>

<behaviors>
  <serviceBehaviors>
    <behavior>
      <serviceMetadata httpGetEnabled="false" />
    </behavior>
    <behavior name="MyServiceTypeBehaviors" >
      <serviceMetadata httpGetEnabled="true" policyVersion="Policy15" />
    </behavior>
  </serviceBehaviors>
</behaviors>
</system.serviceModel>

As you can see the default serviceMetadata element has httpGetEnabled="false" whereas the MyServiceTypeBehaviors serviceMetadata element has httpGetEnabled="true". You can also see that my service's behaviorConfiguration attribute is set to "MyServiceTypeBehaviors".

The result should be that my service publishes metadata however through a browser and through Visual Studio "Add Service Reference" feature I get the same result: no metadata.

On the other hand if I enable metadata in the default behavior and disable it in "MyServiceTypeBehaviors" and continue to have my service use MyServiceTypeBehaviors then I get metadata both through the browser and through VS.

To me, these tests indicate that my service uses default behavior regardless of how I set up my config file... but at the same time I can change the default behavior through web.config so my web.config is in fact able to affect how the service works. Any ideas?

You don't specify a contract in your endpoint, so without the contract the endpoint is notgoing to know what service it's using.

If you're using .NET 4.0 or later (and based on the issues you described it sounds like you are), you're actually connecting to a default endpoint that is based on the address of the service. The default endpoint is provided by the framework.

As such, it will use the default service behavior. This matches your problem description:

-  When the default behavior's httpGetEnabled is set to false, you get no metadata.
-  When the default behavior's httpGetEnabled is set to true, you get the metadata.

The easiest solution in this situation is to simply add the contract to the endpoint you are trying to define:

<endpoint address="" binding="wsHttpBinding" contract="FullyQualified.IContractName" />

You need to add the "metadata" or "MEX" endpoint. Alter your services section of your config to look like:

     <services>
     <service name="TableImport" behaviorConfiguration="MyServiceTypeBehaviors">
        <endpoint address="" binding="wsHttpBinding" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
     </service>
   </services>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM