【发布时间】:2017-04-09 09:43:56
【问题描述】:
这是我为测试服务调用另一个服务而构建的示例项目
这是被其他服务调用的服务
namespace WCFPub
{
[ServiceContract]
public interface IStudent
{
[OperationContract]
string getName(string name);
}
}
namespace WCFPub
{
public class Student : IStudent
{
public string getName(string name)
{
return "Your name is " + name;
}
}
}
承载上述服务的控制台应用程序
namespace WCFHost
{
class Program
{
static void Main(string[] args)
{
try
{
ServiceHost sh = new ServiceHost(typeof(WCFPub.Student));
ServiceMetadataBehavior serviceMetadataBehaviour = new ServiceMetadataBehavior()
{
HttpGetEnabled = true,
};
sh.Description.Behaviors.Add(serviceMetadataBehaviour);
sh.AddServiceEndpoint(typeof(WCFPub.IStudent), new WSDualHttpBinding(), "PS");
Console.WriteLine("Host Ready, Listening on 7060");
Console.WriteLine("Hit Enter to Stop..");
sh.Open();
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
调用第二个服务的服务
namespace WCFPub2
{
[ServiceContract]
public interface IMaster
{
[OperationContract]
string getNameFromStudent(string name);
}
}
namespace WCFPub2
{
public class Master : IMaster
{
public string getNameFromStudent(string name)
{
Proxy2.StudentClient client = new Proxy2.StudentClient();
return client.getName("ABdi");
}
}
}
托管上述服务的控制台应用
namespace WCFHost2
{
class Program
{
static void Main(string[] args)
{
try
{
ServiceHost sh = new ServiceHost(typeof(WCFPub2.Master));
ServiceMetadataBehavior serviceMetadataBehaviour = new ServiceMetadataBehavior()
{
HttpGetEnabled = true,
};
sh.Description.Behaviors.Add(serviceMetadataBehaviour);
sh.AddServiceEndpoint(typeof(WCFPub2.IMaster), new WSDualHttpBinding(), "PS");
Console.WriteLine("Host Ready, Listening on 7061");
Console.WriteLine("Hit Enter to Stop..");
sh.Open();
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
客户
namespace WCFClient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Proxy.MasterClient client = new Proxy.MasterClient();
MessageBox.Show(client.getNameFromStudent("ABdi"));
}
}
}
这不起作用并引发异常
System.ServiceModel.FaultException`1[System.ServiceModel.ExceptionDetail]:
在 ServiceModel 客户端配置部分中找不到引用合同“Proxy2.IStudent”的默认端点元素。这可能是因为没有为您的应用程序找到配置文件,或者因为在客户端元素中找不到与此协定匹配的端点元素。
(Fault Detail等于An ExceptionDetail,可能由IncludeExceptionDetailInFaults=true创建,其值为:
System.InvalidOperationException:在 ServiceModel 客户端配置部分中找不到引用合同“Proxy2.ISTudent”的默认端点元素。这可能是因为找不到您的应用程序的配置文件,或者因为在客户端元素中找不到与此协定匹配的端点元素。在 System.ServiceModel.Description.ConfigLoader.LoadChannelBehaviors(ServiceEndpoint serviceEndpoint, String configurationName)
在 System.ServiceModel.ChannelFactory.ApplyConfiguration(字符串配置名称,配置配置)
在 System.ServiceModel.ChannelFactory.ApplyConfiguration(String configurationName)
在 System.ServiceModel.ChannelFactory.InitializeEndpoint(String configurationName, EndpointAddress 地址)
在 System.ServiceModel.ChannelFactory`1..ctor(String endpointConfigurationName, EndpointAddress remoteAddress)
在 System.ServiceModel.Configu...)。
我需要帮助
【问题讨论】:
-
哪个抛出异常?客户?
-
是的。客户端找不到 Proxy2.IStudent 服务合同,但客户端没有调用 Master 服务所在的服务。