这是我建的测试服务调用另一个服务Windows窗体应用程序调用WCF服务和WCF服务调用另一个WCF服务
这是得到由其他服务调用的服务的示例项目
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”的默认端点元素。这可能是因为没有找到适用于您的应用程序的配置文件,或者因为在客户端元素中找不到匹配此合同的端点元素。
System.InvalidOperationException:(故障详细等于一个ExceptionDetail,可能是由IncludeExceptionDetailInFaults =真,其价值是创造找不到默认终结点元素引用合同“Proxy2.IStudent”在ServiceModel客户端配置节。这可能是因为没有为您的应用程序找到配置文件,或者因为在客户端元素中找不到匹配此合同的端点元素。 )
at System.ServiceModel.ChannelFactory.ApplyConfigurati上(字符串configurationName,配置结构)
在System.ServiceModel.ChannelFactory.ApplyConfiguration(字符串configurationName)
在System.ServiceModel.ChannelFactory.InitializeEndpoint(字符串configurationName,的EndpointAddress地址)
在System.ServiceModel.ChannelFactory`1。 。(字符串endpointConfigurationName,EndpointAddress remoteAddress)
at System.ServiceModel.Configu ...)。
我需要帮助,请
哪一个是抛出异常?客户? – Prajwal
是的。客户端无法找到Proxy2.IStudent服务合同,但客户端没有拨打该服务的主服务。 –