James Carter
James Carter

Reputation: 83

Web.config not being used in IIS for WCF

I have a WCF service running in IIS. I followed this tutorial to host it in IIS: https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-host-a-wcf-service-in-iis

I have a simple Web.config file that is not being recognized by the service:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.serviceModel>
    <services>
      <service name="MyApp.Service1">
        <endpoint address=""
              binding="wsHttpBinding"
              contract="MyApp.IService1" />

    <endpoint address="mex"
              binding="mexHttpBinding"
              contract="IMetadataExchange" />
  </service>
</services>
  </system.serviceModel>
</configuration>

When I run this line of code I get an error: Object not set to an instance of an object:

var test = ConfigurationManager.AppSettings["aspnet:UseTaskFriendlySynchronizationContext"];
var x = test.ToString();

I have the Web.config file in the same folder as the DLL. The whole service is in the wwwroot.

Is there some setting I'm missing to make the web.config actually work? Is there something in IIS I need to set for it to use the web.config? What do I do to get IIS to recognize my web.config?

Upvotes: 0

Views: 1191

Answers (1)

Abraham Qian
Abraham Qian

Reputation: 7532

The failure of your app.config files taking effect is the main reason for this problem. As mentioned in the example.

  • Create a new file named "service.svc" in the application directory

  • Create a file named "Web.config" in the application directory

If you want to host the wcf service library project in IIS, you should follow these steps.

You have to move your configuration code into the configuration file recognized by your hosting environment. And then you will be able to read the web.config correctly.

Yes, it is a service class library. Not sure what you mean by the config file of the application. Is that the app.config?

Here is official document about how to deploy the wcf service library.

https://learn.microsoft.com/en-us/dotnet/framework/wcf/deploying-a-wcf-library-project

Generally speaking, we use the app.config in the windows service hosting-environment for wcf.

Upvotes: 1

Related Questions