我正在尝试使用多个端点创建一个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>
我认为我声明<baseaddress>
的方式是错误的,(在服务配置文件中)
<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,我实施了他的建议并且它正在运作。
答案 0 :(得分:1)
所以除了指导你走错方向(对不起),你使用错误的客户端!您的端点中应该有另一个客户端叫(或接近)。
StockServiceSS2Client client = new StockServiceSS2Client();
它会像冠军一样运作。
发生了什么事情是您使用不同的合同指定和端点,然后是客户端类正在寻找的内容。
很抱歉这个混乱。