Bob Clegg
Bob Clegg

Reputation: 563

ASP.Net Class Library project accessing WCF

Existing Asp.Net project was consuming WCF WebService. Working OK.

I decided to move the business logic into a class library. So now the Class library consumes the WCF web service and the Asp.net app has no reference to it.

On the first call into the class Library by the Asp.net web app (debugging) I get an error:

Could not find default endpoint element that references contract 'CouponParking.ICouponService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.

I have stared at the Class library app.config (which was created by the IDE when I first added a Service reference to the WCF service) and it looks OK to me.

Assuming it needs altering could someone please cast a critical eye over it and tell me what needs doing. My understanding of endpoints is rudimentary.

The asp.net web.config does have an empty servicemodel section. I assume this is correct as the service reference has been removed.

The class library app.config follows then the WCF web.config so you can see the other end.

The WCF has an additional JSON endpoint because it is also consumed by an Android device.

App.Config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="SoapEndPoint" />
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost:8707/CouponParking.svc/SOAP"
            binding="basicHttpBinding" bindingConfiguration="SoapEndPoint"
            contract="CouponParking.ICouponService" name="SoapEndPoint" />
    </client>
</system.serviceModel>
</configuration>

Web.Config:

<?xml version="1.0"?>
<configuration>

<configSections>
<sectionGroup name="applicationSettings"    type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxxxxxxx" >
  <section name="CouponParkingWCF.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="basicHttp" />
  </basicHttpBinding>
  <webHttpBinding>
    <binding name="JsonBinding" />
  </webHttpBinding>
 </bindings>
 <services>
  <service name="CouponParkingWCF.CouponService">
    <endpoint name ="SoapEndPoint"
              address="SOAP"
              binding="basicHttpBinding"
              contract="CouponParkingWCF.ICouponService" />
    <endpoint name="JSON"
              address="REST" behaviorConfiguration="JsonBehavior" binding="webHttpBinding"
      contract="CouponParkingWCF.ICouponService" />
   </service>
  </services>
  <behaviors>
  <endpointBehaviors>
    <behavior name="JsonBehavior">
      <webHttp />
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="">
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
  </behaviors>
  <protocolMapping>
    <add binding="basicHttpsBinding" scheme="https" />
  </protocolMapping>    
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="false" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
    To browse web app root directory during debugging, set the value below to true.
    Set to false before deployment to avoid disclosing web app folder information.
  -->
<directoryBrowse enabled="true"/>
</system.webServer>

<applicationSettings>
<CouponParkingWCF.Properties.Settings>
  <setting name="ServerIp" serializeAs="String">
    <value>192.168.0.224</value>
  </setting>
  <setting name="Database" serializeAs="String">
    <value>WgtnTickTrakTest</value>
  </setting>
</CouponParkingWCF.Properties.Settings>
</applicationSettings>
</configuration>

Upvotes: 0

Views: 1439

Answers (1)

Tim
Tim

Reputation: 28520

Class libraries use the config file of their consuming application - they do not use their own. So you need to move the system.serviceModel portion from the library's app.congif to the web.config of the ASP.NET application:

<system.serviceModel>
  <bindings>
    <basicHttpBinding>
      <binding name="SoapEndPoint" />
    </basicHttpBinding>
  </bindings>
  <client>
    <endpoint address="http://localhost:8707/CouponParking.svc/SOAP"
              binding="basicHttpBinding" 
              bindingConfiguration="SoapEndPoint"
              contract="CouponParking.ICouponService" 
              name="SoapEndPoint" />
  </client>
</system.serviceModel>

Now when you call into the class library from the ASP.NET application, it should pick up the binding and the client endpoint.

Upvotes: 2

Related Questions