C# 在WCF中寻址服务时出现问题
我正在使用wcf编写一个服务。 我创建了一个包含两个项目的解决方案:C# 在WCF中寻址服务时出现问题,c#,.net,wcf,service,C#,.net,Wcf,Service,我正在使用wcf编写一个服务。 我创建了一个包含两个项目的解决方案: 库:存储有关服务的文件(包含该服务的接口和相应实现)的项目。这个项目是一个图书馆项目 托管应用程序托管这些服务的项目(自托管)。由于这个原因,这个项目是一个可执行项目,它有一个配置文件,我在其中放置了配置服务所需的信息 我还编写了一个客户端来调用该服务。这将被称为客户端应用程序 我有服务。以下是接口和实现(库项目): 以下是可执行项目(托管应用程序)中的App.config: 但如果我这样做,就会出现不匹配错误: 这封信是
- 库:存储有关服务的文件(包含该服务的接口和相应实现)的项目。这个项目是一个图书馆项目
- 托管应用程序托管这些服务的项目(自托管)。由于这个原因,这个项目是一个可执行项目,它有一个配置文件,我在其中放置了配置服务所需的信息
App.config
:
但如果我这样做,就会出现不匹配错误:
这封信是写给你的
'http://localhost:8081/Service1/GoInto/Svc'
无法在接收器上处理,
由于地址筛选器在上不匹配
端点调度器。检查一下
发送方和接收方的
我同意
为什么不工作?必须在一个位置指定基址,要么在ServiceHost构造函数中,要么在元素中。如果您在这两个位置都有,WCF将抛出一个异常,说明您有两个相同方案(HTTP)的基址 可能发生的情况是,托管项目的app.config上的服务名称不匹配,因此无法获取配置(而您得到的是一个默认端点,其地址与基址相同)。尝试在宿主代码上添加foreach循环,您应该可以看到服务正在侦听的端点的地址
Console.WriteLine("Opening host...");
host.Open();
foreach (ServiceEndpoint endpoint in host.Description.Endpoints)
{
Console.WriteLine("Endpoint:");
Console.WriteLine(" Address: {0}", endpoint.Address.Uri);
Console.WriteLine(" Binding: {0}", endpoint.Binding);
Console.WriteLine(" Contract: {0}", endpoint.Contract.Name);
}
Console.WriteLine("Waiting...");
必须在ServiceHost构造函数或元素的一个位置指定基址。如果您在这两个位置都有,WCF将抛出一个异常,说明您有两个相同方案(HTTP)的基址 可能发生的情况是,托管项目的app.config上的服务名称不匹配,因此无法获取配置(而您得到的是一个默认端点,其地址与基址相同)。尝试在宿主代码上添加foreach循环,您应该可以看到服务正在侦听的端点的地址
Console.WriteLine("Opening host...");
host.Open();
foreach (ServiceEndpoint endpoint in host.Description.Endpoints)
{
Console.WriteLine("Endpoint:");
Console.WriteLine(" Address: {0}", endpoint.Address.Uri);
Console.WriteLine(" Binding: {0}", endpoint.Binding);
Console.WriteLine(" Contract: {0}", endpoint.Contract.Name);
}
Console.WriteLine("Waiting...");
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<services>
<service name="WcfServiceLibrary.Service1">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8081/Service1" />
</baseAddresses>
</host>
<!-- Service Endpoints -->
<!-- Unless fully qualified, address is relative to base address supplied above -->
<endpoint address="/GoInto/Svc"
binding="basicHttpBinding"
contract="WcfServiceLibrary.IService1">
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="/GoInto/Sav"
binding="basicHttpBinding"
contract="WcfServiceLibrary.IService1">
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<!-- Metadata Endpoints -->
<!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->
<!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
<endpoint address="GoInto/Mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<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>
</system.serviceModel>
</configuration>
namespace WcfServiceClient {
class Program {
static void Main(string[] args) {
ServiceEndpoint httpEndpoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(IService1)), new BasicHttpBinding(), new EndpointAddress("http://localhost:8081/Service1"));
ChannelFactory<IService1> channelFactory = new ChannelFactory<IService1>(httpEndpoint);
IService1 svc = channelFactory.CreateChannel();
Console.WriteLine(svc.GetData(121));
System.Threading.Thread.Sleep(10000);
}
}
}
namespace WcfServiceClient {
class Program {
static void Main(string[] args) {
ServiceEndpoint httpEndpoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(IService1)), new BasicHttpBinding(), new EndpointAddress("http://localhost:8081/Service1/GoInto/Svc"));
ChannelFactory<IService1> channelFactory = new ChannelFactory<IService1>(httpEndpoint);
IService1 svc = channelFactory.CreateChannel();
Console.WriteLine(svc.GetData(121));
System.Threading.Thread.Sleep(10000);
}
}
}
Console.WriteLine("Opening host...");
host.Open();
foreach (ServiceEndpoint endpoint in host.Description.Endpoints)
{
Console.WriteLine("Endpoint:");
Console.WriteLine(" Address: {0}", endpoint.Address.Uri);
Console.WriteLine(" Binding: {0}", endpoint.Binding);
Console.WriteLine(" Contract: {0}", endpoint.Contract.Name);
}
Console.WriteLine("Waiting...");