【问题标题】:Getting Exception while working with multiple endpoints使用多个端点时出现异常
【发布时间】:2010-09-30 23:01:56
【问题描述】:

我试图创建一个具有多个端点的 WCF 应用程序,但是在使用客户端(控制台应用程序)访问它时,我得到以下异常:

在 ServiceModel 客户端配置部分找不到名为“SS2”和合同“IStockService”的端点元素。这可能是因为找不到您的应用程序的配置文件,或者因为在客户端元素中找不到与此名称匹配的端点元素。

我做了什么: 服务端:

代码:

public interface IStockService1 {
    [OperationContract]
    string GetDataForSS1(int value); 
}

[ServiceContract]
public interface IStockService2 {
    [OperationContract]
    string GetDataForSS2(int value);   
}

[ServiceContract]
public interface IStockService:IStockService1,IStockService2 {
    [OperationContract]
    string GetDataForSS3(int value);   
}   

public class StockService : IStockService{
    public string GetDataForSS3(int value){
        return "SS3"+value.ToString();
    }

    public string GetDataForSS1(int value){
        return "SS1"+value.ToString();
    }

    public string GetDataForSS2(int value){
        return  "SS2"+ value.ToString();
    }
}

配置:

<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
      <services>
          <service name="MultipleEndpointsDemo.StockService">
              <host>
                  <baseAddresses>
                      <add baseAddress="http://localhost:1832/StockService.svc/"/>
                  </baseAddresses>
              </host>
              <endpoint name="StockServiceSS1" address="SS1" binding="basicHttpBinding" contract="MultipleEndpointsDemo.IStockService1"/>
              <endpoint name="StockServiceSS2" address="SS2" binding="basicHttpBinding" contract="MultipleEndpointsDemo.IStockService2"/>
              <endpoint name="StockService" address="all" binding="basicHttpBinding" contract="MultipleEndpointsDemo.IStockService"/>
      </service>

      </services>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

        }

现在我使用 svcutil.exe 创建了一个配置和代理类,然后将一个 App.config 添加到我的客户端控制台并复制粘贴该文件中的内容(来自 svcutil)并尝试访问该服务。

客户代码:

StockServiceClient proxy = new StockServiceClient("SS2");
Console.WriteLine(proxy.GetDataForSS2(15));
Console.ReadKey();

配置:

<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="StockServiceSS1" closeTimeout="00:01:00" openTimeout="00:01:00"
                    receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
                    bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="None">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
                <binding name="StockServiceSS2" closeTimeout="00:01:00" openTimeout="00:01:00"
                    receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
                    bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="None">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
                <binding name="StockService" closeTimeout="00:01:00" openTimeout="00:01:00"
                    receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
                    bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="None">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:1832/StockService.svc/SS1"
                binding="basicHttpBinding" bindingConfiguration="StockServiceSS1"
                contract="IStockService1" name="StockServiceSS1" />
            <endpoint address="http://localhost:1832/StockService.svc/SS2"
                binding="basicHttpBinding" bindingConfiguration="StockServiceSS2"
                contract="IStockService2" name="StockServiceSS2" />
            <endpoint address="http://localhost:1832/StockService.svc/all"
                binding="basicHttpBinding" bindingConfiguration="StockService"
                contract="IStockService" name="StockService" />
        </client>
    </system.serviceModel>
</configuration>

我认为我声明&lt;baseaddress&gt; 的方式是错误的,(在服务配置文件中)

<service name="MultipleEndpointsDemo.StockService">
  <host>
    <baseAddresses>
        <add baseAddress="http://localhost:1832/StockService.svc/"/>
    </baseAddresses>
  </host>
  <endpoint name="StockServiceSS1" address="SS1" binding="basicHttpBinding" contract="MultipleEndpointsDemo.IStockService1"/>
  <endpoint name="StockServiceSS2" address="SS2" binding="basicHttpBinding" contract="MultipleEndpointsDemo.IStockService2"/>
  <endpoint name="StockService" address="all" binding="basicHttpBinding" contract="MultipleEndpointsDemo.IStockService"/>
</service>

任何线索我哪里出错了

编辑:不知道为什么 Nix 删除了他的 ans,我实施了他的建议并且效果很好。

【问题讨论】:

标签: c# wcf


【解决方案1】:

因此,除了将您引向错误的方向(对不起)之外,您还使用了错误的客户端!您的端点中应该有另一个客户端称为(或接近)。

StockServiceSS2Client client = new StockServiceSS2Client();

它会像冠军一样工作。

发生的情况是您指定和端点与客户端类正在寻找的合同不同。

很抱歉给您带来了困惑。

【讨论】:

  • 哦,好吧..这是我这边的一个严重错误。感谢您的所有帮助和时间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-07
  • 1970-01-01
  • 1970-01-01
  • 2021-03-31
  • 1970-01-01
  • 2011-10-15
相关资源
最近更新 更多