简体   繁体   中英

[WCF Duplex]Could not find endpoint element that references contract in the ServiceModel client configuration section

I have a publish-subscribe notification running with wcf duplex wsdualhttpbinding. But when I started to subscribe in client side, I got the message: "Could not find endpoint element that references contract 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"

App.config client

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <wsDualHttpBinding>
                <binding name="WSDualHttpBinding_IRecipeTemplateService" />
                <binding name="Basic1" />
            </wsDualHttpBinding>
        </bindings>
        <client>            
            <endpoint address="http://172.18.26.14/CentralService/Services/NotificationService.svc"
                binding="wsDualHttpBinding" bindingConfiguration="Basic1"
                contract="CentralNotificationService.INotificationService"
                name="Basic1">
                <identity>
                    <servicePrincipalName value="host/myclienthost" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>

Web.config

<?xml version="1.0"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.6.1" />
    <httpRuntime targetFramework="4.6.1"/>
  </system.web>
  <connectionStrings>
    <add name="RecipeDB" providerName="MySql.Data.MySqlClient"
         connectionString="Server=localhost; Port=3306; Database=recipe_db; Uid=root; Pwd=root" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
    <providers>
      <provider invariantName="MySql.Data.MySqlClient"
          type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.EntityFramework"/>
      <provider invariantName="System.Data.SqlClient"
          type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
    </providers>
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <remove invariant="MySql.Data.MySqlClient" />
      <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL"
           type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=8.0.28.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
  </system.data>
  <system.serviceModel>
    <services>
      <service behaviorConfiguration="mexBehavior" name="RecipeServer.Central.Service.Services.NotificationService">
        <endpoint address="" binding="wsDualHttpBinding"
          name="Basic" contract="RecipeServer.Central.Service.Interfaces.INotificationService" />
        <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration=""
          name="mexdata" contract="IMetadataExchange" />
      </service>     
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="mexBehavior">
          <serviceMetadata httpGetEnabled="true" />         
        </behavior>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="wsDualHttpBinding" scheme="http" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </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>
</configuration>

Code in client side

            InstanceContext site = new InstanceContext(new ServiceCallBack());           
            try
            {             
                client = new NotificationServiceClient(site);
            }
            catch (Exception e) {
                RecipeLogger.GetInstance.ErrorLog(e.Message);
                RecipeLogger.GetInstance.ErrorLog(e.StackTrace);
            }
            
            WSDualHttpBinding binding = (WSDualHttpBinding)client.Endpoint.Binding;
            RecipeLogger.GetInstance.DebugLog("2");

            binding.ClientBaseAddress = new Uri("http://localhost:8000/myClient/");
            string clientcallbackaddress = binding.ClientBaseAddress.AbsoluteUri;
            RecipeLogger.GetInstance.DebugLog(String.Format("Client baseadd {0}", clientcallbackaddress));
            clientcallbackaddress += Guid.NewGuid().ToString();
            binding.ClientBaseAddress = new Uri(clientcallbackaddress);
            RecipeLogger.GetInstance.DebugLog("Subscribe to central");
            client.Subscribe();
            client.Close();

After having fixed this issue for nearly a week, I realized that I have multiples project in a solution, and the initial project was service project, and it had a web.config, so when run the project, the VS read the web.config and ignored all app.config at others project.

So the solution here is I just moved all settings code in app.config into web.config

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