Search code examples
c#entity-frameworkwcfn-tier-architecture

Named connection with Entity Framework via WCF not found?


I'm new trying to build an N-tier app through WCF and Entity Framework. I'm new to all of these, so apologies if I sound ignorant.

I'm attempting to test, and I get this error when I attempt to query the database through my WCF service.

The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.

I just copy-pasted my connection string from the Entity Framework project to my WCF host web.config, so I'm not sure what's wrong.

Any ideas?

web.config

<?xml version="1.0"?>
<configuration>
    <connectionStrings>
      <add name="AerosPACE_CCADEEntities" connectionString="metadata=res://*/AerospaceModel.csdl|res://*/AerospaceModel.ssdl|res://*/AerospaceModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=****;initial catalog=AerosPACE_CCADE;persist security info=True;user id=****;password=****;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

    </connectionStrings>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>
    <system.serviceModel>
        <services>
            <service name="AerospaceCCADE.Server.Domain.Service.AerosPACE_CCADEDataService">
                <endpoint address="" binding="wsHttpBinding" contract="AerospaceCCADE.Common.Domain.Service.Contracts.IAerosPACE_CCADEDataService" bindingConfiguration="WS-AT" />
                <endpoint address="basic" binding="basicHttpBinding" contract="AerospaceCCADE.Common.Domain.Service.Contracts.IAerosPACE_CCADEDataService" />
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
            </service>
        </services>
        <bindings>
            <wsHttpBinding>
                <binding name="WS-AT" transactionFlow="true" />
            </wsHttpBinding>
        </bindings>
        <behaviors>
            <serviceBehaviors>
                <behavior>
                    <serviceMetadata httpGetEnabled="true"/>
                    <serviceDebug includeExceptionDetailInFaults="true" />
                    <serviceAuthorization impersonateCallerForAllOperations="false" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true" />
    </system.serviceModel>
    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
    </system.webServer>
</configuration>

And the client App.config:

<?xml version="1.0"?>
<configuration>
    <connectionStrings>
    </connectionStrings>
    <system.serviceModel>
        <bindings>
            <wsHttpBinding>
                <binding name="WSHttpBinding_DataService" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 
                         bypassProxyOnLocal="false" transactionFlow="true" hostNameComparisonMode="StrongWildcard" 
                         maxBufferPoolSize="524288" maxReceivedMessageSize="67108864" 
                         messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
                    <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false"/>
                    <security mode="Message">
                        <transport clientCredentialType="Windows" proxyCredentialType="None" realm=""/>
                        <message clientCredentialType="Windows" negotiateServiceCredential="true" algorithmSuite="Default"/>
                    </security>
                </binding>
            </wsHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:5482/AerosPACE_CCADEDataService.svc" 
                      binding="wsHttpBinding" 
                      bindingConfiguration="WSHttpBinding_DataService" 
                      contract="AerospaceCCADE.Common.Domain.Service.Contracts.IAerosPACE_CCADEDataService" 
                      name="AerosPACE_CCADEDataService"/>
        </client>
        <behaviors>
            <endpointBehaviors>
                <behavior>
                    <clientCredentials>
                         <!--allow server to use client credentials for impersonation--> 
                        <windows allowedImpersonationLevel="Impersonation" />
                    </clientCredentials>
                </behavior>
            </endpointBehaviors>
        </behaviors>
    </system.serviceModel>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
    </startup>
</configuration>

Solution

  • You also need the entityFramework section.

    <configSections>
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </configSections>
    <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
        <providers>
          <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
        </providers>
    </entityFramework>